CodeAcademy Ruby Basics Flashcards
Types of data in Ruby?
String, Numbers, Booleans
What is a Modulo, and what is its sign?
Modulo (%), Modulo returns the remainder of division. For example, 25 % 7 would be 4, since 7 goes into 25 3 times with 4 left over.
Use if, else, and print
if 3>2 print "3 is big!" else print "We're retarded." end
Where's the Error (1): If 3>2 print "3 is big!" else print "We're retarded." end
“If” is capitalized; it needs to be “if”
Use elsif to add more than two options to an if/else statement: think greater than, equal, and less than.
if x>y print "X rocks." elsif y>x print "Y rocks." else print "Y equals X!" end
Find Error (1): if x>y print "X rocks." elseif y>x print "Y rocks." else print "Y equals X!" end
“elseif” doesn’t exist, it’s “elsif”
Let’s say you don’t want to eat unless you’re hungry. That is, while you’re not hungry, you write programs, but if you are hungry, you eat. How would you write a program that a) assumes you’re not hungry, b) if you’re not hungry states “Writing code!”, c) if you are hungry states “Eating, fools!”
hungry = false unless hungry puts "Writing code!" else puts "Eating, fools!" end
Set the variable my_num to the value 100
my_num = 100
If you wanted the words Hello to show up only if 3>2, then what would you write?
if 3>2
Print “Hello.”
end
What is equals to in Ruby?
==
What is not equal to in Ruby?
!=
Less than or equal to and greater than or equal to?
=
What does && mean and what are the four options?
The boolean operator and, &&, only results in true when both expression on either side of && are true. Here’s how && works:
true && true # => true
true && false # => false
false && true # => false
false && false # => false
For example, 1 < 2 && 2 < 3 is true because it’s true that one is less than two and that two is less than three.
What does || mean and what are the four options?
Ruby also has the or operator (||). Ruby’s || is called an inclusive or because it evaluates to true when one or the other or both expressions are true. Check it out:
true || true # => true
true || false # => true
false || true # => true
false || false # => false
What does ! mean and what are the four options?
Finally, Ruby has the boolean operator not (!). ! makes true values false, and vice-versa.
!true # => false
!false # => true
How do you get input from the reader; like getting their name?
print “What is your name?”
So, you asked a question, like “What’s your first name?”, but how do we declare that information as the variable first_name?
print “What’s your name?”
first_name = gets.chomp
What does “gets” accomplish?
gets is the Ruby method that gets input from the user.
Why does the method .chomp accomplish?
When getting input, Ruby automatically adds a blank line (or newline) after each bit of input; chomp removes that extra line.
So, let’s say you’ve gathered two pieces of info: first name and where they were born. How do we state at the end of this gathering process, “Your name is Morgan, and you were born in Florida.”?
print "What's your name?" first_name = gets.chomp print "Where were you born?" birthplace = gets.chomp print "Your name is #{first_name}, and you were born in #{birthplace}".
String interpolation. What does it look like and what does it accomplish?
#{VARIABLE, will be replaced with value of variable} NOTICE BRACKETS If you define a variable monkey that's equal to the string "Curious George", and then you have a string that says "I took #{monkey} to the zoo", Ruby will do something called string interpolation and replace the #{monkey} bit with the value of monkey—that is, it will print "I took Curious George to the zoo".
What should you think when you see ! after a method?
Whenever you see !, think: “This method may be dangerous and do something I don’t expect!” In this case, the unexpected part is that the original string gets changed, not a copy.
What is the method to capitalize something?
.capitalize
Let’s say you were trying to reproduce daffy duck’s impediment on someone’s first name; what would you do?
print "What's your first name?" first_name = gets.chomp if first_name.include? "s" first_name.gsub!(/s/, "th") else puts "Nothing to be done here!" end puts "Your name is #{first_name}."