What does dynamic typing mean?
Dynamic typing means that you don't have the declare the type, or class, of a variable before using it.
What does static typing mean?
Static typing means that you do have to declare the type of every variable.
Which kind of language is Ruby?
Ruby is a dynamically typed language.
What is string interpolation?
String interpolation is the process of inserting the value of a variable into a string that is defined with double quotes.
For example:
name = 'Glenn'
puts "My name is #{name}." # My name is Glenn.
How do you increment numbers in Ruby?
You increment numbers with +=. You can do analogous actions with -=, *= and /=.
For example:
x = 10
x += 2
puts x # writes 12 to the console
What's the difference between a Fixnum and a Bignum?
Finums are relatively small integers and Bignums relatively big ones.
For example:
puts 10.class # Fixnum
puts 10000000000.class # Bignum
How do you convert from a number to an string?
Use the to_s method on the number.
For example:
puts 10.to_s.class # String
How do you convert from a string to a number?
Use the to_i method.
For example:
puts '10'.to_i.class # Fixnum
If you try this on a string that can't be converted to a number, you get 0.
puts 'cat'.to_i # 0
What are three ways to loop in Ruby?
while, until and for.
What is the Ruby syntax for if-then-else?
x = 10
if x < 10
   puts 'x < 10'
elsif x > 10
   puts 'x > 10'
else
   puts 'x == 10'
end
Can you write an if-then on a single line?
Yes.
x = 10
puts 'x == 10' if x == 10 # x == 10
if x == 10 then puts 'x == 10' end # x == 10
The first example is far more common in Ruby.
Can you write an if-then-else on a single line?
Yes, by using the ternary operator.
x = 10
x == 10 ? (puts 'x == 10') : (puts 'x != 10') # x == 10
How do you take a slice out of a string?
puts 'Glenn'.slice(1, 2) # le
puts 'Glenn'.slice(-1, 1) # n
How do you reverse a string?
puts 'Glenn'.reverse # nnelG
What does the term 'receiver' mean?
The object on which the method is called.
What does the term 'destructive method' mean?
A method that changes the value of the receiver.
Are all methods that end in a '!' destructive methods?
No. But usually that is the case. This is a Ruby convention. It is not enforced by the Ruby interpretor.
Name the 2 Boolean objects.
The 2 Ruby Boolean objects are true and false.
What method does the puts method call on every parameter passed to it?
puts always calls the to_s method on objects that are passed to it.
What is the syntax of a Ruby case?
x = 10
case x
   when 5 then puts 'x == 5'
   when 15 then puts 'x == 15'
   when 10 then puts 'x == 10'
   else puts "I don't know what x is equal to."
end
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment