Ruby Flashcards
What does a less_than equal greater_than b return? (Urgh!)
-1 if a less than b
0 if a equals b
1 if a greater than b
This is three way comparison and the operator is called the spaceship
Naming rule for Classes
Start with a capital letter and typically use PascalCase
Naming rule for instance variables
Start with @ and use lowercase_snake_case
Naming rule for class variables
Start with @@
Naming rule for method names
use lowercase_snake_case
Naming rule for constants
use SCREAMING_SNAKE_CASE
What is a Ruby iterator?
Simply a method that can invoke a block of code.
What is a Ruby block?
1) may only appear adjacent to a method call.
2) it is not executed at the time it is encountered. Instead, the block can be invoked from within the method using the yield statement.
Describe the action of the yield statement in a method?
When a yield is executed, it invokes the code block. When the block exits, control picks back up immediately after the yield.
Describe block_given?
Returns true if a block is associated with the current method. With block_given, a method can have two different behaviours dependent on whether or not it is called with a block.
How do you define a method so that it can receive a code block?
If the last parameter in a method definition is prefixed with an ampersand, Ruby looks for a code block whenever that method is called. That code block is converted to an object of class Proc and assigned to the parameter. You can then treat the parameter as any other variable. For example, it can be assigned to an instance variable. The Proc#call method on that object to invoke the block.
Thing to remember when coding a numerical literal with an exponential?
You must follow the decimal point with a digit. 1.e3 tries to invoke the method e3 in class fixnum.
What string method converts a string to an integer?
String.to_i
How do you can substitute the value of any Ruby expression into a string?
Using the sequence #{ expr }. If the expression is just a global variable, a class variable, or an instance variable, you can omit the braces. E.g.
“Seconds/day: #{246060}” » Seconds/day: 86400
“#{‘Ho! ‘*3}Merry Christmas” » Ho! Ho! Ho! Merry Christmas
“This is line #$.” » This is line 3