Engineering, Design, Marketing, and More

Ruby interview questions

The most popular questions

Use our complementary questions and answers to filter and hire the best. Questions crowdsourced by our clients, answers by Punch. We provide these complementary questions to help our clients more quickly create tests for potential hires at their organizations.

Get a question answered
Punch offers four divisions of services: design, engineering, staffing, and demand

Interview questions for your next interview

Question
Does Ruby support namespaces?
Answer
A namespace provides a container to hold things like functions, classes and constants as a way to group them together logically and to help avoid conflicts with functions and classes with the same name that have been written by someone else.

In Ruby this is achieved using modules.
Question
How can I invoke a method in Ruby?
Answer
There are three ways to invoke a method in Ruby - dot operator (or period operator), the Object#send method or method(:foo).call:object = Object.new
puts object.object_id

puts object.send(:object_id)

puts object.method(:object_id).call

Find developers today

Hire a Punch engineer

Punch offers four divisions of services: design, engineering, staffing, and demand. Our four divisions form the core of our People Forward Approach.

Contact us
Find developers today
Question
What does self mean in Ruby?
Answer
It is receiver object of the current method. In Ruby language for method invocation it's prefered to say "message passing". So in other words, self is the object to which the message (or method) has been sent. For example, in the following code reflect is an instance method. It belongs to the object we created via Ghost.new. So self points to that object. class Ghost
def reflect
self
end
end

g = Ghost.new
g.reflect == g # => true
Question
Explain how you define an Instance Variable, Global Variable and Class Variable in Ruby?
Answer
  • Ruby Instance variable begins with — @
  • Ruby Class variables begin with — @@
  • Ruby Global variables begin with — $
Question
Explain how can you declare a block in Ruby?
Answer
In Ruby, the code in the block is always enclosed within braces ({}). You can invoke a block by using “yield statement”.
Question
What is Interpolation in Ruby?
Answer
Ruby Interpolation is the process of inserting a string into a literal. By placing a Hash (#) within {} open and close brackets, one can interpolate a string into the literal. For example:name = "Alex"
puts "Hello, #{name}!"
Question
What is a lambda?
Answer
In Ruby a lambda is just an anonymous function. Lambdas in Ruby are also objects, just like everything else. Lambda can return a value. Example:l = labda do |name|
return "Hello, #{name}!"
end
puts l.call("Alex")
Question
What are levels of method access control for classes in Ruby?
Answer
There are three levels of method access control for classes:
  • Public methods - can be called by all objects and subclasses of the class in which they are defined in.
  • Protected methods - only accessible to objects within the same class.
  • Private methods - are only accessible within the same instance.
Question
How would you create getter and setter methods in Ruby?
Answer
Of course you can create them manually. But the more elegant way is to call the attr_accessor method which generates it's for you.class Person
attr_accessor :name
end
You can also generate just getter or setter individuall using attr_reader and attr_writer moethods appropriately.
Question
How does a symbol differ from a string?
Answer
The main difference is that multiple symbols representing a single value are identical whereas this is not true with strings. It is also faster to compare symbols for equality since they are the same object.

Ask a question

Ask a question, and one of our engineers will answer it.

We keep our questions nice and simple to be useful for everyone, and we may not answer or publish every question.

Your number is stored privately and never shared.
By submitting a question, you agree to our terms.
Request sent. Thank you!
Send another one