Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
How was last week's homework? Do you have any questions or concepts that you'd like to discuss?
irb> puts "hello"
hello
=> nil
irb> "2".to_i
=> 2
irb> 2.to_s
=> "2"
irb> "2" / 5
NoMethodError: undefined method `/' for "2":String
Put the code below in a file and save it as name.rb
puts 'Hello there, and what\'s your name?'
name = gets
puts 'Your name is ' + name.chomp! + '? What a lovely name!'
puts 'Pleased to meet you, ' + name + '. :)'
Run your program from the command line:
ruby name.rb
#1 dog year = 7 human years
user_age = gets.to_i
A boolean is a basic data type. It can have only two values: true or false.
Code that compares values and returns True or False is called a Boolean expression
a == b | a is equal to b |
a != b | a does not equal b |
a < b | a is less than b |
a > b | a is greater than b |
a <= b | a is less than or equal to b |
a >= b | a is greater than or equal to b |
Learn more about logical operators in Ruby here.
a = 3
b = 4
puts a != b
puts a <= 3
puts a >= 4
a = 5
b = 5
puts a == b
c = a == b # Combine comparison and assignment
puts c
puts 3 < 5
Remember: Equals does not equal "equals equals"
Further reading on boolean expressions...
A boolean expression evaluates to true or false. It can also have multiple parts, joined by AND (&&) or OR (||).
EXPRESSION | EVALUATES TO |
---|---|
true && true | true |
true && false | false |
false && false | false |
true || true | true |
true || false | true |
false || false | false |
not (true && false) | true |
Further practice on boolean expressions...
Take a few minutes and experiment with boolean expressions in IRB. You can start with the examples below.
true && false
1 == 1 && 2 > 37
"boop" == "bip" || 7 == 8
false || true
89 > 88 || 89 < 90
true || not(1 == 1 || 2 == 65)
So, what's the value of knowing if a statement is true or false? Often, you'll use that to control whether a piece of code will execute or not.
user_guess = gets.chomp.to_i
secret_number = 312
if user_guess < secret_number
puts "Too low!"
elsif user_guess > secret_number
puts "Too high!"
else
puts "You guessed it. Wow maybe you're psychic...."
end
Can you tell what this code does?
When we want different code to execute depending on certain criteria, we use a conditional
We achieve this using if statements and boolean expressions.
if x == 5
puts 'x is equal to 5'
end
We often want a different block to execute if the statement is false. This can be accomplished using else.
if x == 5
puts 'x is equal to 5'
else
puts 'x is not equal to 5'
end
The following shows some examples of conditionals with more complex boolean expressions:
# And
if x > 3 && y > 3
puts 'Both values are greater than 3'
end
# Or
if x != 0 || y != 0
puts 'The point x,y is not on the x or y axis'
end
# Not
if not(x > y)
puts 'x is less than y'
end
Conditionals can also be chained.
Chained conditionals use elsif to test if additional statements are true. The single 'else' action will only happen if all preceding conditions are false.
For example:
if x > 10
puts "x is greater than 10"
elsif x <= 10 && x > 0
puts "x is a number between 1 and 10"
else
puts "Wow, don't be so negative, dude"
end
Write a program in your text editor that uses conditionals and user input to allow the user to play an adventure game. For example, you can "puts" story and options to the command line, and user can input their choices.
Remember, to do math with "gets", convert it to an integer with the ".to_i" method
input = gets.chomp.to_i
Run your program by calling it with Ruby from the command line.
$ ruby program_name.rb
# adventure.rb
puts "A vicious dragon is chasing you!"
puts "Options:"
puts "1 - Hide in a cave"
puts "2 - Climb a tree"
input = gets.chomp
if input == '1'
puts "You hide in a cave. The dragon finds you and asks if you'd like to play Scrabble. Maybe it's not so vicious after all!"
elsif input == '2'
puts "You climb a tree. The dragon can't find you."
else
puts "That's not a valid option."
end
#command line
ruby dragon.rb
It is often useful to perform a task and to repeat the process until a certain point is reached.
The repeated execution of a set of statements is called iteration, or, more commonly, a loop.
One way to achieve this, is with the while loop.
x = 10
while x > 0
puts "Loop number #{x}"
x = x - 1
end
puts 'Done'
x = 10
while x > 0
puts "Loop number #{x}"
x = x - 1
end
The while statement takes a condition, and as long as it evaluates to true, the code block beneath it is repeated. This creates a loop.
Without the `x = x - 1` statement, to increment the value of x, this would be an infinite loop :( :( :(
Consider the following example that uses a while loop to sing you a song.
num_bottles = 99
while num_bottles > 0
puts "#{num_bottles} bottles of beer on the wall,
#{num_bottles} bottles of beer, take one down, pass it
around, #{num_bottles - 1} bottles of beer on the wall!"
num_bottles = num_bottles - 1
end
"#{num_bottles}" is an example of string interpolation
# loopy.rb
loopy = true
while loopy == true
puts "Are we having fun yet?"
puts "Answer 'true' or 'false'."
user_input = gets.chomp
if user_input == 'false'
loopy = false
end
end
Learn more about loops in Ruby here.
The most commonly used type of loop in Ruby is an each loop. It uses the .each method to iterate over a collection of elements, doing work to each one.
First, we need a collection. Let's use a range of numbers to loop over.
(0..5).each do |i|
puts "Value of i is #{i}"
end
(0..5).each do |i|
puts "Value of i is #{i}"
end
The loop has three parts:
We will revisit the each loop when we have a better understanding of collections.
Practice: Read Chapters 5 and 6 of Learn to Program.