Common Ruby interview questions

To evaluate your skill level and experience in Ruby, there are some questions that are frequently asked. It’s important that you understand the fundamental concepts behind them.

Whether you want to prepare for an interview or looking to improve your knowledge on the language, knowing the answer to the below questions will put you on the right track.

Ruby

  • What are some differences between ruby and other programming languages?

  • What does private do in Ruby?

  • There are many ways to invoke a method in ruby. Can you give me at least two?

  • Explain this ruby idiom: a ||= b

  • What does self mean?

  • What is a Proc?

  • What is the difference between classes and modules?

  • How are include, extend and prepend different?

  • What is a metaclass? What is it for?

  • What is a gem?

Object Oriented Design

  • What is a class? What is an object?

  • What is a module?

  • Can you describe the OO paradigm?

  • How is OO different from other paradigms (structured/functional)?

  • Define the following OO principles (encapsulation, abstraction, inheritance, polymorphism)

  • What is cohesion and coupling?

  • What is duck typing?

  • Can you give an example of a design pattern in action?

  • How do you define composition or inheritance? Give examples what cases do they work best for.

  • How do you organize multiple objects that you want to adhere to a particular behavior in ruby?

1 Like

My favorite :slight_smile:

Explain this ruby idiom: a ||= b

Yes, but not work for nil and false :grinning_face:

Interesting. I had to look up prepend.

Not sure on “4 ways to invoke a method”. I can give 2:

  • implicit/direct receiver, e.g. obj.method_to_call
  • using send, e.g. obj.send :method_to_call

But I can’t think what could be the other 2?

Same here about the other two…

I can think of eval("obj.some_method") and obj.method(:some_method).call (or the equivalent obj.method(:some_method)[]) but not sure if they qualify as “different” ways though :thinking:

These are all reasonable. Also :joy: private as a concept in Ruby; this language is so very open and meta, you can always get inside, nice for conceptual organization though.

If anyone tries to grill you on the flipflop operator in an interview, consider if that is really a place you want to work. The most esoteric things are gotcha questions rather than useful measures of language knowledge.

@pcl I found this article with 12 ways to call a method in Ruby, I didn’t knew there were that many!

@andynu You’re right about the private concept/question, but I do think it’s a good way to talk. about tradeoffs and design decisions. I also agree that questions can also help the interviewee to get more information about what is valuable for the company. When I do an interview I like to use questions as a way to guide the conversation more than looking for right/wrong answers, but I also there are others who do more confrontational interviews.

3 Likes

there are up to 14 depending how you count, see calling_methods - Documentation for Ruby 4.1

foo
foo()
obj.foo
self.foo
send(:foo)
public_send(:foo)
m = obj.method(:foo); m.call

1 Like

Just so @javier.cervantes ; And perhaps I’m a sensitive soul, but I want to work in collaborative and kind environments. I don’t personally align with confrontational interviews regardless of which side of the interview I’m on.

I don’t mind private being a hint more than a rule. And the only thing I miss from java is a slightly stricter concept of interfaces. So I offer a related interview question: How do you organize multiple objects that you want to adhere to a particular behavior in ruby? (Areas to consider: mixins, abstract classes, duck typing, warnings/exceptions, contracts)

I don’t think there are those many ways…

At least I interpreted “There are many ways to invoke a method in ruby. Can you give me at least two?” referring to what’s cooked into the language explicitly to that end, not how many clever ways you can indirectly call a method. ( IMO, looking at point 10 in the article, defining method_missing to invoke another method it’s just indirection… under that light, calling an endpoint in a server that returns the name of the method, parsing the response and invoking that method would count too; ok, that was exaggerated :joy:)

100% agree, confrontation is not good for anyone. I also updated the list with your question (I’m hoping that’s okay)

1 Like

Oh but Charlie, for the joy of it:

Greeter::hello (weird old not-often used double colon syntax for class methods)
Greeter.class_eval(“hello”)

:smile: Ruby; How do I love thee? Let me count the ways.

How about arr.map(&:foo) ? :face_with_peeking_eye:

1 Like

I’m currently seeking new job opportunities and reviewing/learning some concepts as part of the process.

@javier.cervantes, is it okay for me to post my own answers to some of these questions here so we can discuss them?

That’s a great idea! But my suggestion would be to open a new topic in the Help category so we can keep the discussion independent (and allow for more people to participate too).

When you create the topic, you can add a link to this topic and I believe it show here as “related” automatically.

1 Like