Ruby Interview Questions
There are three ways to invoke a method in ruby. Can you give me at least two?
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Explain this ruby idiom: a ||= b
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
What is a module? Can you tell me the difference between classes and modules?
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
What is a class?
A text-book answer: classes are a blue-print for constructing computer models for real or virtual objects... boring.
In reality: classes hold data, have methods that interact with that data, and are used to instantiate objects.
Like this.class WhatAreClasses def initialize @data = "I''m instance data of this object. Hello." end def method puts @data.gsub("instance", "altered") end end object = WhatAreClasses.new object.method #=> I''m altered data of this object. Hello.
Can you tell me the three levels of method access control for classes and modules? What do they imply about the method?
All methods, no matter the access control, can be accessed within the class. But what about outside callers? Public methods enforce no access control -- they can be called in any scope. Protected methods are only accessible to other objects of the same class. Private methods are only accessible within the context of the current object.
class AccessLevel def something_interesting another = AccessLevel.new another.public_method another.protected_method another.private_method end def public_method puts "Public method. Nice to meet you." end protected def protected_method puts "Protected method. Sweet!" end private def private_method puts "Incoming exception!" end end AccessLevel.new.something_interesting #=> Public method. Nice to meet you. #=> Protected method. Sweet! #=> NoMethodError: private method ‘private_method’ called for #=> #
What does self mean?
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
What is an object?
An instance of a class.
To some, it''s also the root class in ruby (Object).
Classes themselves descend from the Object root class. (Kudos to Ezra)
What is a Proc?
Everyone usually confuses procs with blocks, but the strongest rubyist can grok the true meaning of the question.
Essentially, Procs are anonymous methods (or nameless functions) containing code. They can be placed inside a variable and passed around like any other object or scalar value. They are created by Proc.new, lambda, and blocks (invoked by the yield keyword).
Note: Procs and lambdas do have subtle, but important, differences in ruby v1.8.6. However, I wouldn''t expect a candidate talk about these nitty-gritty details during an interview. (Kudos to Noah Thorp)# wants a proc, a lambda, AND a block def three_ways(proc, lambda, &block) proc.call lambda.call yield # like block.call puts "#{proc.inspect} #{lambda.inspect} #{block.inspect}" end anonymous = Proc.new { puts "I''m a Proc for sure." } nameless = lambda { puts "But what about me?" } three_ways(anonymous, nameless) do puts "I''m a block, but could it be???" end #=> I''m a Proc for sure. #=> But what about me? #=> I''m a block, but could it be??? #=> # # #
Difference between a proc and lambda
Lambdas check the number of arguments, while procs do not
lam = lambda { |x| puts x } # creates a lambda that takes 1 argument lam.call(2) # prints out 2 lam.call # ArgumentError: wrong number of arguments (0 for 1) lam.call(1,2,3) # ArgumentError: wrong number of arguments (3 for 1)
In contrast, procs don’t care if they are passed the wrong number of arguments.proc = Proc.new { |x| puts x } # creates a proc that takes 1 argument proc.call(2) # prints out 2 proc.call # returns nil proc.call(1,2,3) # prints out 1 and forgets about the extra arguments
Lambdas and procs treat the ‘return’ keyword differently
‘return’ inside of a lambda triggers the code right outside of the lambda codedef lambda_test lam = lambda { return } lam.call puts "Hello world" end lambda_test # calling lambda_test prints ''Hello World''
`return` inside of a proc triggers the code outside of the method where the proc is being executeddef proc_test proc = Proc.new { return } proc.call puts "Hello world" end proc_test # calling proc_test prints nothing
gem vs engine vs plugin
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.