2 Ruby basics Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What standard Ruby classes do you know? What useful methods have you learned for each class?

A
  • String (.new, interpolation, concatenation .capitalize, .downcase, .chars, .index(), .insert(), .match(), .partition(), .reverse, .split(‘ ‘))
  • Array (.new, .at(), [], [..], .take(), .push, «, .unshift, .insert, pop, .delete_at(), .delete(), .length, .count, .size, .empty?, .any?, .include?(), .each, .each_index, .each_with_index, .map)
  • Symbol (.object_id)
  • Hash (.new, [], .delete, .delete_if, .shift, .each, .each_key, .each_value, .key?(), .value?(), .keys, .values, .values_at(), .length, .merge, .select, .dig)
  • Integer and Float (.class)
  • Range (.to_a)
  • Time (.new(), .at(), .year, .month, .day, .wday, .yday, .hour, .min, .sec, .zone, .strftime())
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between .each and .map enumerable methods?

A

.each simply iterates over the given enumerable, running the block for each value. It discards the return value of the block, and each simply returns the original object it was called on.
.map, however, iterates over each element, using the return value of the block to populate a new array at each respective index and return that new array.
Neither each nor map themselves modify the original collection.

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

What is the difference between single and double quotes in the context of the string class?

A

Single quotes are more preferable. When we work with interpolation we must use double quotes.

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

What scope for variables do you know?

A

Local, Instance (@), Global ($), Constants (AN)

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

What is the difference between logical && and ’and’?

A

=> nil

one = :one
two = nil
t = one and two
# => nil
t
# => :one
t = one && two
# => nil
t​
# => nil
t = (one and two)​
# => nil
t​
# => nil
(t = one) && two​
# => nil
t​
# => :one

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

What is the difference between procs and lambdas?

A

=> “Proc.new”

“return” inside procs makes code leave the block the proc is in.
def proc_return
Proc.new { return ‘Proc.new’ }.call
return ‘proc_return return’
end
def lambda_return
lambda { return ‘lambda’ }.call
return ‘lambda_return return’
end
proc_return
# => “Proc.new”
lambda_return
# => “lambda_return return”

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

How do you pass a block to a method? How do you ‘invoke’ that block in a certain place inside the method?

A

def try
if block_given?
yield
else
‘no block’
end
end
try
# => “no block”
try { ‘hello’ }
# => “hello”
try do ‘hello’ end
# => “hello”
or
def thrice
yield
yield
yield
end
x = 5
puts “value of x before: #{x}”
# => “value of x before: 5”
thrice { x += 1 }
puts “value of x after: #{x}”
# => “value of x after: 8”

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

What is the difference between methods with ’!’ and ‘?’ at the end of the name?

A

Methods with ‘!’ change given to method values after method work. ‘?’ means that method returns bool value.

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