Ruby Flashcards
1
Q
What is the difference between map and collect?
A
There is none, collect is an alias for map in ruby. They both invoke a block once for each element in ‘self’. Eg:
xyz.collect { |x| x * 2 }
2
Q
Whats the ternary operator in rails?
A
Its a one line ifelse statement statement
@item.property = params[:property] ? true : false
for example
var = 5;
var_is_greater_than_three = (var > 3 ? true : false);
puts var_is_greater_than_three
The values true and false can be replaced with strings, numbers, or other data types.
3
Q
What is ‘self’ in ruby?
A
Its a keyword that gives you access to the current object.