LaunchKit

Ruby · Rails · Git

Ruby & Rails Quiz Answers

Answers and in-depth explanations for the Ruby and Rails quizzes we share on social media.

48
Questions
def method1
  yield
end
Ruby

Constants assigned inside blocks leak out

26 views
a = <<'"}{|_(_&^+$^)$_$345_$#%$^%^+'
visit ukod.me
"}{|_(_&^+$^)$_$345_$#%$^%^+
Ruby

Heredoc delimiters can be almost anything

26 views
class MyClass
  def self.to_s
    "MyClass is cool"
  end
Ruby

A class definition behind if false

32 views
nil.to_a   # => []
Array(nil) # => []

nil.to_h  # => {}
Ruby

Float(nil) refuses what nil.to_f forgives

27 views
class Example; end

example = Example.new
Ruby

instance_of? ignores inheritance

30 views
x = nil
y = "Ruby"

x || y && "Rails" # => ???
Ruby

&& binds tighter than ||

27 views
require 'set'

tags = SortedSet.new
Ruby

SortedSet keeps values unique and ordered

24 views
class Hi
  def initialize(name)
    @name = name
  end
Ruby

The braceless #@ivar interpolation shorthand

31 views
require 'net/http'

[Net::HTTP, Net::HTTP.class] # => [Net::HTTP, Class]
Ruby

Shadowing Net::HTTP with your own module

34 views
"Euruko".upcase!.chars # => ["E", "U", "R", "U", "K", "O"]

"EURUKO".upcase!.chars # => ???
Ruby

When upcase! returns nil mid-chain

212 views
a = "outer"
1.times do
  a = "inner"
end
Ruby

Blocks reach outer local variables

29 views
h = { a: 1, b: 2 }
h.replace(h.invert)

h[:a] # => ???
Ruby

Why h.replace(h.invert) loses your keys

25 views
numbers = [1, 2, 3]
numbers[1, 0] = [4]

numbers.join("x") # => ???
Ruby

Inserting with numbers[1, 0] = [4]

27 views
hash = {a: 1}
hash[:b] = hash

hash[:b][:b][:b][:a] # => ???
Ruby

A hash that contains itself under :b

28 views
a   = [1, 2]
res = []

case a.size
Ruby

Two identical when clauses in one case

24 views
str = "hello\n"
str.chomp!.reverse # => "olleh"

str = "hello"
Ruby

chomp! returns nil when nothing changes

26 views
array = [1, [2]]
array[1] << array

array.flatten # => ???
Ruby

Flattening an array that contains itself

26 views
(1..10).each { |n| break n if n == 5 } # => ???
Ruby

Giving break a return value

29 views
i = 42

for i in (1...10)
  puts i
Ruby

Why for i in (1...10) clobbers your i

23 views
def procs_return
  -> { return :lambda }.call

  proc { return :proc }.call
Ruby

return inside a lambda vs inside a proc

42 views
h = Hash.new { [] }

h[:a] << 42
h[:b] << 84
Ruby

Why Hash.new { [] } stays empty after <<

30 views
h = Hash.new([])

h[:a] << 42
Ruby

The shared default array in Hash.new([])

26 views
a = [21, 42, 84]

a[1.2] # => ???
Ruby

Indexing an Array with a Float

34 views
h = {}.compare_by_identity

h["key"] = 42
h["key"] = 84
Ruby

Duplicate keys with Hash#compare_by_identity

27 views
var = !(!!!())

var # => ???
Ruby

!(!!!()) and the value of empty parentheses

29 views
toggler = %w(start stop).cycle

toggler.next
toggler.next
Ruby

Toggling values with Array#cycle and next

26 views
12345.digits # => ???
Ruby

Integer#digits returns the digits in reverse

24 views
a = [1, 2, nil, 2, "", false, 3]

a.select(&:itself) # => ???
Ruby

Filtering truthy values with select(&:itself)

28 views
class Person
  def firstname = "Jon"
  def lastname = "Jones"
Ruby

An assignment behind if false still shadows your method

27 views
1.0.eql?(1)   # => false
1.0.equal?(1) # => false

1.0 == 1      # => ???
Ruby

1.0 == 1 versus eql? and equal?

28 views
h = { a: 21, b: 42 }.freeze

[*h] # => ???
Ruby

Splatting a frozen Hash with [*h]

25 views
"aaoo".gsub("a") {|c| c.upcase} # => "AAoo"

"aaoo".gsub("a", "o") {|c| c.upcase} # => ???
Ruby

When gsub gets both a replacement and a block

25 views
words = [["abc", ["def"]], "ghi"]

words[0][0][0][1] # => ???
Ruby

Chained [0][0][0][1] ends inside a String

26 views
Swapping two variables with `a, b = b, a`
Ruby

Swapping two variables with a, b = b, a

36 views
[1, 3, 2, 1, 2] | [] # => ???
Ruby

Deduplicating with Array#| and an empty array

24 views
class Device
  def description
    'I\'m a device'
  end
Ruby

alias vs alias_method across inheritance

30 views
(42 + 0.42).!@.!@ # => ???
Ruby

!! is two calls to BasicObject#!

32 views
["a", *["b", "c"], "d"] * ?: # => ???
Ruby

Splat, ?:, and Array#* in one line

21 views
def sum
  n = 0

  n += yield
Ruby

Two yields, one block

32 views
class Greeting
  def self.hello = "hello" if false
end
Ruby

Endless methods meet if false

23 views
dec = ->(n) {n-1}
inc = ->(n) {n+1}
dbl = ->(n) {n*2}
Ruby

Composing procs with << and >>

24 views
def my_method(a)
  a += [4]
end
Ruby

Reassigning a method argument never touches the caller

23 views
a = []
upcase   = ->(c) { a << c; c.upcase }
downcase = ->(c) { a << c; c.downcase }
Ruby

Lazy enumerators run the whole chain per element

27 views
str = "abc".freeze

str += "def"
Ruby

Calling += on a frozen String

288 views