- Ruby fundamentals
- Variables
- Classes: strings and integers
- User input
- Flow control
- Collections: Arrays and Hashes
- Looping
- Pseudocoding
- Challenge 1: Guessing Game
- Challenge 2: Choose your own adventure
- Super Fizz Buzz
- Fibonacci sequence
- Luhn algorithm
- Gradual release of responsibility
- Teaching to fish (use your resources, ask a peer, ask an instructor)
- Emotional intelligence: managing the discomfort of not knowing
- Technical strategy and communication
A variable is a name that we can use to reference stored data value.
=> "This is a string"
2.4.0 :002 > my_var
=> "This is a string"
2.4.0 :003 > my_var.class
=> String
2.4.0 :004 > another_var = 5
=> 5
2.4.0 :005 > another_var
=> 5
2.4.0 :006 > another_var.class
=> Integer
We can use a command called gets
to retrieve user input in our console.
puts "Enter your name: "
name = gets.chomp
puts "Hello, #{name}."
Prompt the user for their age and respond with the statement: "Your age is x."
We can use if...else...end
blocks to do different things depending on user input. Let's set this up in an actual Ruby file this time: `ruby_playground.rb".
puts "Enter your favorite food: "
fav_food = gets.chomp
if fav_food == "pizza"
puts "Pizza. Classic"
elsif fav_food == "ice cream"
puts "Ice cream: a solid choice."
else
puts "I have some concerns about your tastes."
end
Prompt the user for a word. If the word begins with "A", respond with the statement: "Your word has x characters." If the word begins with any other letter, respond with "Your fantabulous word has x characters."
We work with two types of collections most commonly in Ruby: arrays and hashes. Arrays are lists where the order matters, and hashes are pairs of keys
and values
that we can reference through their keys.
2.4.0 :033 > my_array = ["fish", "cat", "dog"]
=> ["fish", "cat", "dog"]
2.4.0 :034 > my_array
=> ["fish", "cat", "dog"]
2.4.0 :035 > my_hash = {best_pet: "dog", most_attitude: "cat", easiest: "fish"}
=> {:best_pet=>"dog", :most_attitude=>"cat", :easiest=>"fish"}
2.4.0 :036 > my_hash
=> {:best_pet=>"dog", :most_attitude=>"cat", :easiest=>"fish"}
2.4.0 :037 > my_hash[:best_pet]
=> "dog"
Create an array of strings of 5 celebrities and set it to the variable celebrity
. Create a hash of 5 people and their (imaginary) phone numbers.
Once we have arrays, we can use looping to go through those list and do different things with the input.
my_array = ["fish", "cat", "dog"]
my_array.each do |animal|
puts "I love my #{animal}."
end
Create an array of student names and write a loop that prints out the statement "StudentX is the most hard-working student."
What is pseudocoding and why does it matter?
Your challenge is to write a guessing game. Your program will pick a random number (that's one piece you'll have to figure out how to do), then prompt your user to guess the number. If their guess is too high, tell them "Too high. Please guess again." Same if their number is too low. When they guess the right number, output "Congratulations. X is the right number!" and exit the game (X would be the number they guessed correctly).
We will tackle this in two steps. The first is to pseudocode how you will tackle this problem. Once the instructor has signed off on your pseudocode, you can move onto coding in Ruby.
Pick one of the challenges below to apply your new Ruby skills.
Ask a user to give a number. If the number is divisible by 3, respond with "Super." If the number is divisible by 5, respond with "Fizz." If the number is divisible by 7, respond with "Buzz." If a number is divisible by more than one number, combine your responses. For example: the response to 35 is "FizzBuzz."
The Fibonacci sequence is a sequence of numbers where each value is the sum of the previous two numbers in the sequence. Read more about it here. Write a program that prompts a user for a number between 1 and 20, and returns the Fibonacci sequence with the same number of elements as the user provides.
The Luhn algorithm is used to determine if a credit card number is valid or not. Read about how the Luhn algorithm works and use ruby to write a program that takes in a credit card number and returns either "valid" or "invalid" depending on whether it adheres to your logic.