What this round is for
Ruby interviews reward people who understand blocks deeply, because nearly everything distinctive about the language is built on them. Expect the round to test that, then modules and mixins, then whether you can separate the language from Rails.
Several answers below are about conventions rather than rules - the bang suffix, the question-mark suffix, method aliases. Knowing they are conventions, and that the language relies on them being followed, is itself part of the answer.
The questions
Answer each one out loud before you open it. An answer you have read is not an answer you can
give under pressure, which is the only kind that counts.
Q1
What does it mean that everything in Ruby is an object?
Show the answer
Hide the answer
Integers, nil, true, classes and even operators are objects with methods.
There is no separate category of primitive values with different rules, so 2 + 3 is a method call on 2.
What it is really testing.
Whether you know operators are methods, which is what makes operator overloading natural.
Q2
What is a block?
Show the answer
Hide the answer
A chunk of code passed to a method, written with braces or do/end.
It is how iteration, resource management and callbacks are all expressed. A method receives it implicitly and runs it with yield.
What it is really testing.
Whether you can name uses beyond each. Blocks underpin most of what is distinctive about Ruby.
Q3
What is the difference between a proc and a lambda?
Show the answer
Hide the answer
A lambda checks argument count and returns from itself; a proc does neither.
return inside a proc returns from the enclosing method, which is usually a surprise. Prefer lambdas unless you need proc behaviour.
What it is really testing.
Whether you know the return semantics, which is the difference that actually bites.
Q4
What is yield?
Show the answer
Hide the answer
It calls the block that was passed to the method.
block_given? checks whether there was one, so a method can behave differently with and without. This is how File.open guarantees the file closes.
What it is really testing.
Whether you can write a method that takes a block rather than only consuming them.
Q5
What is the difference between a symbol and a string?
Show the answer
Hide the answer
A symbol is immutable and the same object every time; a string is mutable and new each time.
Use a string for data the user sees and a symbol for a name your program uses - hash keys, method names, states.
What it is really testing.
Whether you have a rule rather than copying whichever appears in examples.
Q6
What are the two jobs of a module?
Show the answer
Hide the answer
Namespacing related classes, and mixing shared methods into a class with include.
The mixin role is how Ruby gets shared behaviour without multiple inheritance. Enumerable is the classic example.
What it is really testing.
Whether you know both. Most answers cover only one.
Q7
How does Enumerable work?
Show the answer
Hide the answer
Define each and include Enumerable, and you get map, select, sort_by and dozens more free.
It is the clearest demonstration in the language that a small contract can buy a large capability.
What it is really testing.
Whether you can explain the mechanism rather than just listing methods.
Q8
How does duck typing work in Ruby?
Show the answer
Hide the answer
An object's suitability is decided by the methods it responds to, not its class.
It makes test doubles trivial - any object with the right methods works. The cost is that a missing method fails at run time.
What it is really testing.
Whether you name the run-time cost as well as the flexibility.
Q9
What is the difference between include, extend and prepend?
Show the answer
Hide the answer
include adds instance methods, extend adds them as class methods, prepend inserts ahead of the class.
prepend lets a module override a method and call super back into the original, which is how clean decoration is done.
What it is really testing.
Whether you know prepend exists. Most answers cover only the first two.
Q10
What is method_missing and what does it cost?
Show the answer
Hide the answer
A hook called when an undefined method is invoked.
It powers flexible library interfaces and makes code unsearchable - the method does not exist anywhere in the source. Define respond_to_missing? alongside it.
What it is really testing.
Whether you mention respond_to_missing?. Omitting it breaks introspection.
Q11
What is monkey patching and when is it acceptable?
Show the answer
Hide the answer
Reopening a class you do not own to change it.
It can break a library three dependencies away in ways that take a day to trace. Refinements scope the change to one file, which is the safer form.
What it is really testing.
Whether you know refinements exist as the scoped alternative.
Q12
What does attr_accessor generate?
Show the answer
Hide the answer
A reader and a writer method for an instance variable.
attr_reader and attr_writer give one each. They are ordinary methods generated at class definition time, not special syntax.
What it is really testing.
Whether you know they are generated methods rather than special syntax.
Q13
What is the difference between public, private and protected?
Show the answer
Hide the answer
public is callable anywhere, private only on the implicit self, protected on another instance of the same class.
protected is rare and exists for comparing two objects of the same class without exposing the value publicly.
What it is really testing.
Whether you can explain protected, which almost nobody uses correctly.
Q14
How does Ruby handle exceptions?
Show the answer
Hide the answer
begin, rescue, ensure and else - with begin implicit inside a method definition.
retry restarts the block, which suits transient network failures. Rescue the narrowest class, because bare rescue catches StandardError broadly.
What it is really testing.
Whether you know retry exists and where it is appropriate.
Q15
What is the difference between raise and throw?
Show the answer
Hide the answer
raise signals an error; throw and catch are a non-local jump for control flow, not errors.
Most code never needs throw. Confusing it with other languages' throw is a common trip-up.
What it is really testing.
Whether you know throw is not an error mechanism in Ruby.
Q16
What does the ! suffix on a method mean?
Show the answer
Hide the answer
By convention, that it is the more dangerous of a pair - usually mutating in place.
It is a convention, not enforced. sort! mutates, sort returns a new array, and save! raises where save returns false.
What it is really testing.
Whether you know it is convention rather than something the language enforces.
Q17
What does the ? suffix mean?
Show the answer
Hide the answer
By convention, that the method returns a boolean.
Again a convention rather than a rule, but followed consistently enough that you can predict behaviour from a method name.
What it is really testing.
Whether you know conventions are what make method names predictable.
Q18
What is the difference between nil and false?
Show the answer
Hide the answer
They are different objects; nil means absence, false is a boolean.
Both are falsy and everything else is truthy - including 0 and an empty string, which differs from several other languages.
What it is really testing.
Whether you know only nil and false are falsy.
Q19
Is 0 truthy in Ruby?
Show the answer
Hide the answer
Yes. Only nil and false are falsy.
This catches people arriving from C, Python or JavaScript, where zero and empty collections are falsy.
What it is really testing.
Whether this surprises you. It catches people from almost every other language.
Q20
What does the safe navigation operator do?
Show the answer
Hide the answer
&. calls a method only if the receiver is not nil.
It replaces the and-chains used to guard against nil, with the same caution that it can hide a nil that should have been an error.
What it is really testing.
Whether you know when hiding a nil is the wrong choice.
Q21
What is the difference between each and map?
Show the answer
Hide the answer
each is for side effects and returns the original; map returns a new array of results.
Using each and building an array up by hand is the usual sign that map was the method you wanted.
What it is really testing.
Whether you pick based on the return value rather than habit.
Q22
What does reduce do in Ruby, and why is it also called inject?
Show the answer
Hide the answer
Collapses a collection to one value using an accumulator.
inject is the same method under another name - one of several aliases Ruby provides so the sentence reads well in context.
What it is really testing.
Whether you know inject is the same method, which confuses newcomers.
Q23
Why does Ruby have aliases like map and collect?
Show the answer
Hide the answer
Because different words read better in different sentences.
A language optimising for the machine would never duplicate methods. One optimising for readability does it deliberately.
What it is really testing.
Whether you see it as a deliberate design choice rather than redundancy.
Q24
What is a Struct?
Show the answer
Hide the answer
A quick way to define a class with named attributes and value equality.
It is useful for small value objects without writing a full class. Data, added more recently, is the immutable version.
What it is really testing.
Whether you know Data exists as the immutable version.
Q25
How does Ruby's object model handle class methods?
Show the answer
Hide the answer
They are methods on the singleton class of the class object.
Because classes are objects, defining self.method is defining an instance method on that object's singleton class - which is why the model is uniform.
What it is really testing.
Whether you can explain singleton classes, which is the heart of the object model.
Q26
What is the difference between load and require?
Show the answer
Hide the answer
require loads once and tracks it; load re-executes every time.
require is almost always correct. load exists for re-reading a file deliberately, such as in a console.
What it is really testing.
Whether you know require tracks what it has loaded.
Q27
What is Bundler for?
Show the answer
Hide the answer
Resolving and locking gem versions so an application runs with the same dependencies everywhere.
The lock file is the point - without it, two machines installing the same Gemfile can get different versions.
What it is really testing.
Whether you can explain why the lock file matters.
Q28
What is the difference between Ruby and Rails?
Show the answer
Hide the answer
Ruby is the language; Rails is a web framework with strong conventions built on it.
Many opinions about Ruby are really opinions about Rails. Learning the language first produces a better developer.
What it is really testing.
Whether you separate them. Conflating the two is extremely common.
Q29
What is convention over configuration?
Show the answer
Hide the answer
Defaults that work if you follow naming conventions, so most projects need little setup.
It makes the first week fast and the second year slower, because behaviour you did not write is harder to locate when it misbehaves.
What it is really testing.
Whether you can name the cost as well as the benefit.
Q30
What is ActiveRecord and what is its main pitfall?
Show the answer
Hide the answer
An object-relational mapper - and the N+1 query is its main pitfall.
A loop over records issuing one query each turns a hundred rows into a hundred and one queries. includes solves it by eager loading.
What it is really testing.
Whether you recognise N+1 as the dominant performance issue in Rails.
Q31
What are strong parameters for?
Show the answer
Hide the answer
Whitelisting which request parameters may be assigned to a model.
Without them, a crafted request could set any attribute, including ones like an admin flag that no form exposes.
What it is really testing.
Whether you know the attack they prevent - mass assignment.
Q32
How does garbage collection work in Ruby?
Show the answer
Hide the answer
A generational mark-and-sweep collector reclaims unreferenced objects.
Objects surviving several collections are promoted and scanned less often. You rarely tune it, but knowing it exists explains memory behaviour.
What it is really testing.
Whether you know generational collection exists, which explains memory patterns.
Q33
What is frozen_string_literal and why use it?
Show the answer
Hide the answer
A magic comment making string literals immutable, so repeated literals share one object.
It reduces allocations noticeably in hot code and catches accidental mutation of a literal.
What it is really testing.
Whether you know it reduces allocations rather than being purely stylistic.
Q34
What is the difference between dup and clone?
Show the answer
Hide the answer
Both copy shallowly; clone also copies the frozen state and singleton methods.
Neither is deep, so nested objects remain shared - the same trap as shallow copies elsewhere.
What it is really testing.
Whether you know neither is deep, which is the part that causes bugs.
Q35
How do you test Ruby code?
Show the answer
Hide the answer
RSpec or Minitest, with doubles that are simply objects responding to the right methods.
Testing discipline matters more here than in a compiled language, because there is no compiler to catch a misspelled method name.
What it is really testing.
Whether you connect testing discipline to the absence of a compiler.
Q36
What is metaprogramming good and bad at?
Show the answer
Hide the answer
Good for building a pleasant library interface; bad for application code nobody else will read.
The question to ask is whether you are saving more reading than you are costing. For a library with thousands of users the answer is often yes.
What it is really testing.
Whether you weigh readers saved against confusion caused.
Q37
What is define_method used for?
Show the answer
Hide the answer
Defining methods at run time, often from data.
It is how an attribute macro generates accessors. Unlike method_missing the methods actually exist, so they respond to respond_to? and appear in introspection.
What it is really testing.
Whether you know the methods genuinely exist, unlike with method_missing.
Q38
How does Ruby's performance compare, honestly?
Show the answer
Hide the answer
Slower than compiled languages; a dynamic language resolving calls at run time cannot avoid that.
Recent versions narrowed the gap with a JIT. For web work the database and network usually dominate, which is why it matters less than it sounds.
What it is really testing.
Whether you can be honest about it rather than defensive.
Q39
What is the difference between a class variable and a class instance variable?
Show the answer
Hide the answer
A class variable with two at-signs is shared with subclasses; a class instance variable is not.
Class variables being shared down the hierarchy surprises people and causes bugs, which is why many style guides discourage them.
What it is really testing.
Whether you know class variables are shared with subclasses.
Q40
What makes Ruby code idiomatic?
Show the answer
Hide the answer
Using blocks and Enumerable rather than manual loops, and choosing the method whose name reads correctly.
The language offers several ways to say most things specifically so the right sentence exists for each context.
What it is really testing.
Whether you can articulate a principle rather than list features.