Ruby & Rails Quiz Answers

A collection of answers to the most popular Ruby & Rails quizzes we share on social media. Each entry comes with a clear explanation so you can dive deeper into tricky behaviors and sharpen your skills.

Dup vs clone cover

1396 views

We begin with a simple string instance and attach a singleton method to it. Only that instance...

Threequal ranges cover

1618 views

r = (1.0..3.0) r === 3 At first sight you might think that the comparison will...

Memoize false cover

1847 views

Let’s analyze this simple snippet: y = false y ||= 42 # => 42 ||= is...

Default args cover

3028 views

In Ruby, the ability to define default argument values for method parameters is a flexible...

yield and blocks cover

1563 views

A method that never yields ignores the block. Constants reveal the truth and make the behavior easy to see.

Single quoted heredoc cover

1427 views

Quoted delimiters turn off interpolation and escapes. Great for snippets that must be preserved exactly as written.

Conditional class reopening cover

1251 views

The guard prevented the class body from running, so to_s is the default. A quiet lesson about where to place conditions.

Kernel casts cover

1521 views

Some conversions are forgiving and others raise. Learn the exact behavior so your guards are tight and your code reads clean.

instance_of? cover

1254 views

instance_of? does not walk the hierarchy. For inheritance aware checks use is_a? and keep intent honest.

Operator precedence cover

1252 views

Ruby evaluates && before ||. The final string can surprise you. Master the order and your boolean logic reads well.

SortedSet cover

1667 views

Adding the same tag twice changes nothing. The set stays unique and sorted, which is perfect for tidy tag lists.

Ivar interpolation shorthand cover

1807 views

Ruby lets you interpolate instance variables without braces. A tiny to_s becomes clean and pleasant to read.

Later constant definitions cover

1453 views

Including a module brings its constants into scope until you define a new one with the same name. Then Ruby resolves to yours.

Bang methods and nil cover

1790 views

When a string is already uppercase, upcase! returns nil. Chaining fails. Learn when to choose the non bang version.

Block & outer vars cover

1251 views

A block assignment writes into the existing local variable. After the block, the outer value reflects the inner change.

Hash#invert cover

1562 views

invert swaps keys and values, then replace keeps the swap. Looking up by the old key will not work anymore.

Slice assignment cover

1720 views

Assigning into a zero length slice inserts without replacing. A precise way to transform arrays where they sit.

Self-referential hashes cover

1684 views

Assign a hash to itself and you get recursion. Index through it and watch the same object lead you back to the leaf value.

Duplicate when clauses cover

2000 views

case stops at the first match. Even identical branches do not stack. The first hit wins and the result is simple to reason about.

chomp! cover

1674 views

Bang methods may return nil when no change happens. Chaining then fails. Learn a tidy pattern that keeps your code robust.

self-referential arrays cover

2398 views

See how Ruby handles an array that contains itself. A curious case that reveals how flatten avoids infinite recursion with grace.

break can return a value cover

1811 views

Stop an iteration and surface the match in a single move. It is a tidy pattern that reads well and saves extra variables.

Undo the last commit cover

1356 views

Fix a message or regroup your work without losing progress. Learn the exact command that resets the commit while keeping files staged.

For loops cover

2377 views

Unlike each with a block, a for loop writes to the outer scope. After the loop, i holds the last value.

lambda and proc cover

1639 views

A lambda returns to its caller. A proc returns from the method that defined it. This contrast shapes control flow in real apps.

Hash default cover

1969 views

A shared default leaks across keys. Switch to a block default and each key gets its own fresh array. Clean and predictable.

Hash.new { [] } cover

1981 views

A block default returns a fresh array each time but you still need to store it. Learn the safe pattern that avoids leaks.

Float index on arrays cover

2013 views

Array indexing expects integers. A float index triggers coercion rules and a result that sharpens your sense of types.

compare_by_identity cover

2290 views

Identity hashes compare keys by object id. Two equal strings are not the same key unless they are the same object.

symbolize_keys cover

3687 views

When a symbol and a string key collide, the symbol wins and the last assignment stands. A crisp reminder of Rails hash semantics.

Stacked negations cover

1773 views

Lots of punctuation can hide a simple idea. Peel the not operators one by one and the final truth pops out.

Cycling enumerators cover

2048 views

cycle creates an endless playlist. After two steps the next call wraps around. Perfect for toggles and round robin flows.

Least significant first cover

1755 views

Ruby splits numbers from the right. A playful method that invites tiny math tricks and clear thinking about order.

select(&:itself) cover

1822 views

Use a tiny idiom to filter out nil and false while keeping zeros and empty strings. It feels like magic and makes your intent obvious.

Double render cover

1747 views

Rendering twice causes trouble. Halt after the first response or redirect to keep controller actions clear and correct.

Local variables cover

4465 views

The guarded assignment does not run, so the method call wins. A neat tour through name resolution inside methods.

Equality in Ruby cover

1431 views

Discover why 1.0 == 1 is true while eql? and equal? are not. Learn how Ruby separates identity, strict equality, and value to keep intent clear.

Hash destructuring cover

2590 views

Freezing does not change how hashes enumerate. Splat a hash and you get tidy key value pairs ready to use.

gsub with block cover

3411 views

When you pass both a replacement and a block, Ruby picks the string. A small rule that explains big surprises in real code.

Deep structures cover

1662 views

Hop through arrays and strings in one chain. It feels like spelunking through data until you land on a single character.

Parallel assignment cover

1599 views

Ruby swaps variables without a temp. It is clean, expressive, and a joy to teach. One line and you are done.

Array union cover

3990 views

Union preserves order and uniqueness from the left side. Watch duplicates vanish while the first appearance sets the tone.

Alias and inheritance cover

1254 views

Alias at the class level can capture a method before overrides apply. Timing changes which behavior a new name points to.

Numeric not operator trick cover

1853 views

Operators are methods. Even ! can be invoked in sequence on numbers. It looks odd, but it teaches real lookup rules.

Rails stats cover

1428 views

Generate a quick snapshot of your codebase from the command line. It is a fast ritual before refactors and releases.

Join arrays cover

1778 views

The star operator repeats an array or joins it with a string. A tiny move that builds readable output fast.

Double yields cover

2451 views

A block may run twice or more. Plan for side effects and write blocks that behave well when invoked again.

Conditional methods cover

1941 views

Methods inside a false guard never exist. The call fails because the class body did not run. A gentle reminder to keep guards outside.

Lambda composition cover

1556 views

Compose tiny functions like building blocks. Follow the data as it flows through dec, dbl, and inc for a satisfying result.

+= on arrays cover

1621 views

Pass a frozen array without fear. The += operator builds a new array, so the original stays untouched and safe.

Lazy map cover

2231 views

Side effects happen on demand with lazy enumerators. Trace each step and feel the evaluation order click into place.

Frozen strings cover

5635 views

Appending to a frozen string creates a new object. Learn how Ruby protects immutability while keeping code pleasant to write.

RubyCademy ©