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 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is ‘self’ in ruby?

A

Its a keyword that gives you access to the current object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly