Skip to content

Instantly share code, notes, and snippets.

@peterhurford
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhurford/7bdb3fa9ef7228b2667e to your computer and use it in GitHub Desktop.
Save peterhurford/7bdb3fa9ef7228b2667e to your computer and use it in GitHub Desktop.
Rails Interview

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment