Ruby · Rails · Git
Answers and in-depth explanations for the Ruby and Rails quizzes we share on social media.
def method1 yield end
a = <<'"}{|_(_&^+$^)$_$345_$#%$^%^+'
visit ukod.me
"}{|_(_&^+$^)$_$345_$#%$^%^+
class MyClass
def self.to_s
"MyClass is cool"
end
class definition behind if falsenil.to_a # => []
Array(nil) # => []
nil.to_h # => {}
Float(nil) refuses what nil.to_f forgivesx = nil y = "Ruby" x || y && "Rails" # => ???
&& binds tighter than ||class Example; end example = Example.new
instance_of? ignores inheritancerequire 'set' tags = SortedSet.new
SortedSet keeps values unique and orderedclass Hi
def initialize(name)
@name = name
end
#@ivar interpolation shorthandrequire 'net/http' [Net::HTTP, Net::HTTP.class] # => [Net::HTTP, Class]
Net::HTTP with your own module"Euruko".upcase!.chars # => ["E", "U", "R", "U", "K", "O"] "EURUKO".upcase!.chars # => ???
upcase! returns nil mid-chaina = "outer" 1.times do a = "inner" end
h = { a: 1, b: 2 }
h.replace(h.invert)
h[:a] # => ???
h.replace(h.invert) loses your keysnumbers = [1, 2, 3]
numbers[1, 0] = [4]
numbers.join("x") # => ???
numbers[1, 0] = [4]hash = {a: 1}
hash[:b] = hash
hash[:b][:b][:b][:a] # => ???
:ba = [1, 2] res = [] case a.size
when clauses in one casestr = "hello\n" str.chomp!.reverse # => "olleh" str = "hello"
chomp! returns nil when nothing changesarray = [1, [2]] array[1] << array array.flatten # => ???
(1..10).each { |n| break n if n == 5 } # => ???
break a return valuegit reset --softi = 42 for i in (1...10) puts i
for i in (1...10) clobbers your ih = Hash.new { [] }
h[:a] << 42
h[:b] << 84
Hash.new { [] } stays empty after <<def procs_return
-> { return :lambda }.call
proc { return :proc }.call
return inside a lambda vs inside a proch = Hash.new([]) h[:a] << 42
Hash.new([])a = [21, 42, 84] a[1.2] # => ???
Floath = {}.compare_by_identity
h["key"] = 42
h["key"] = 84
Hash#compare_by_identityh = { key: 42, 'key' => 84 }
h.symbolize_keys # => ???
symbolize_keys collides :key and "key"var = !(!!!()) var # => ???
!(!!!()) and the value of empty parenthesestoggler = %w(start stop).cycle toggler.next toggler.next
Array#cycle and next12345.digits # => ???
Integer#digits returns the digits in reversea = [1, 2, nil, 2, "", false, 3] a.select(&:itself) # => ???
select(&:itself)def show @event = Event.find(params[:id]) @event.remote? # => true
render calls in one Rails actionclass Person def firstname = "Jon" def lastname = "Jones"
if false still shadows your method1.0.eql?(1) # => false 1.0.equal?(1) # => false 1.0 == 1 # => ???
1.0 == 1 versus eql? and equal?h = { a: 21, b: 42 }.freeze
[*h] # => ???
[*h]"aaoo".gsub("a") {|c| c.upcase} # => "AAoo"
"aaoo".gsub("a", "o") {|c| c.upcase} # => ???
gsub gets both a replacement and a blockwords = [["abc", ["def"]], "ghi"] words[0][0][0][1] # => ???
[0][0][0][1] ends inside a Stringa, b = b, a[1, 3, 2, 1, 2] | [] # => ???
Array#| and an empty arrayclass Device
def description
'I\'m a device'
end
alias vs alias_method across inheritance(42 + 0.42).!@.!@ # => ???
!! is two calls to BasicObject#!rake stats["a", *["b", "c"], "d"] * ?: # => ???
?:, and Array#* in one linedef sum n = 0 n += yield
yields, one blockclass Greeting def self.hello = "hello" if false end
if falsedec = ->(n) {n-1}
inc = ->(n) {n+1}
dbl = ->(n) {n*2}
<< and >>def my_method(a) a += [4] end
a = []
upcase = ->(c) { a << c; c.upcase }
downcase = ->(c) { a << c; c.downcase }
str = "abc".freeze str += "def"
+= on a frozen String