Week 03 Ruby Part 1 - Code-the-Dream-School/rails-guidebook GitHub Wiki
Week | Topic | Learning Objectives | Key Resources |
---|---|---|---|
3 | Ruby Part 1 |
|
Lesson Materials Coding Assignment |
This series of three lessons will bring JavaScript developers up to speed on Ruby, focusing on the differences and unique features of Ruby as compared to JavaScript.
- Ruby Syntax Basics
- Ruby is genuinely object-oriented.
- Ruby supports multithreading, unlike JavaScript.
- Differences in variable declaration and naming conventions.
- Using puts and print for output, gets and gets.chomp for input.
- String interpolation with #{} within double quotes.
- Comments start with #.
- First Ruby Program
- Writing a simple Ruby script.
- Error handling and method calls.
- Understanding method chaining.
- Ruby Symbols and Hashes
- Symbols as lightweight, immutable strings.
- Creating and manipulating hashes.
- Understanding Ruby's object-oriented nature.
- Using irb (Interactive Ruby) to explore Ruby classes and methods.
- Percent Notation
- Creating arrays and strings using percent notation.
- Advantages of percent notation for readability and convenience.
- Experimenting with percent notation in irb.
Program: second.rb
# Initialize an empty hash for the person
person = {}
# Prompt for and collect first name
puts "Enter your first name:"
person[:first_name] = gets.chomp
# Prompt for and collect last name
puts "Enter your last name:"
person[:last_name] = gets.chomp
# Prompt for and collect age
puts "Enter your age:"
person[:age] = gets.chomp.to_i
# Prompt for and collect street address
puts "Enter your street address:"
person[:street_address] = gets.chomp
# Prompt for and collect city
puts "Enter your city:"
person[:city] = gets.chomp
# Prompt for and collect state
puts "Enter your state:"
person[:state] = gets.chomp
# Output the hash
puts "Hash content:"
puts person
# Output the keys of the hash
puts "Keys:"
puts person.keys
# Output the values of the hash
puts "Values:"
puts person.values
# Capitalize the first name, last name, and city
person[:first_name] = person[:first_name].capitalize
person[:last_name] = person[:last_name].capitalize
person[:city] = person[:city].capitalize
# Change the state to upper case
person[:state] = person[:state].upcase
# Output the modified hash
puts "Modified hash content:"
puts person
Program: find_number.rb
def play_game
secret_number = rand(1..100)
puts "I have generated a random number between 1 and 100. Try to guess it!"
loop do
puts "Enter your guess:"
guess = gets.chomp.to_i
if guess == 0
puts "Invalid guess. Please enter a number."
elsif guess < secret_number
puts "Your guess is too low."
elsif guess > secret_number
puts "Your guess is too high."
else
puts "Congratulations! You guessed the correct number."
break
end
end
puts "Do you want to play again? (yes/no)"
response = gets.chomp.downcase
if response == "yes"
play_game
else
puts "Thank you for playing!"
end
end
play_game
Program: guesser.rb
def play_game
low = 1
high = 100
puts "Think of a number between 1 and 100, and I'll try to guess it."
loop do
guess = (low + high) / 2
puts "Is your number #{guess}? (too low/too high/correct)"
response = gets.chomp.downcase
case response
when "too low"
low = guess + 1
when "too high"
high = guess - 1
when "correct"
puts "I guessed it! Your number is #{guess}."
break
else
puts "Invalid response. Please reply with 'too low', 'too high', or 'correct'."
end
if low > high
puts "It seems like there might be a mistake. Are you sure you were being honest? Let's start over."
break
end
end
puts "Do you want to play again? (yes/no)"
response = gets.chomp.downcase
if response == "yes"
play_game
else
puts "Thank you for playing!"
end
end
play_game