1a.) Why would one use a symbol instead of a string?
1b.) Why would someone use a string instead of a symbol?
2a.) What is the difference between a lambda, a block and a proc?
2b.) What does -> (a) {p a}["Hello world"]
do?
3.) If x = "hello"
, what is the difference between x += " world"
and x.concat "world"
?
4a.) Can you give me a function that takes as its input an array of any number of strings and gives as its output an array with the number of characters in each string of the input array?
def count_strings(arr)
arr.map { |str| str.length }
end
or
def count_strings(arr)
arr.map(&:length)
end
4b.) In Ruby code, you quite often see the trick of using an expression like array.map(&:method_name)
as a shorthand form of array.map { |element| element.method_name }
. How exactly does it work?
5.)
VAL = 'Global'
module Foo
VAL = 'Foo Local'
class Bar
def value1
puts VAL
end
end
end
class Foo::Bar
def value2
puts VAL
end
end
What will be the value of each of the following: Foo::Bar.new.value1
and Foo::Bar.new.value2
?
6.)
val1 = true and false
val2 = true && false
What is val1? What is val2? Why?