Idiosyncratic Ruby

Quiz


@JanLelis β€” December 2018 β€” 𝕽𝖀𝕲::𝕭

idiosyncratic-ruby.com

Documenting lesser-known features in Ruby


Ruby 2.6's Around the Corner

Section A


??..





(endless..ranges)

Endless Range Examples


(9..).take(3)
# => [9, 10, 11]


(?a..?z).zip(?a.ord..)
# => [["a", 97], ["b", 98], ["c", 99], …

Poking around with ranges (1)


(nil..)
# =>
nil..

(nil..).cover?(nil)
# =>
true

(nil..).include?(nil)
# =>
TypeError (can't iterate from NilClass)

Poking around with ranges (2)


(9..).cover?(1/0.0)
# =>
true

(9...).cover?(1/0.0)
# =>
true

(9..) == (9...)
# =>
false

Poking around with ranges (3)


"Rails vs Hanami"[9..]
and
"Rails vs Hanami"[9..-1]
# =>
"Hanami"

(9..).size
# =>
Infinity

(9..-1).size
# =>
0

Section B

The Ruby standard library is currently
getting divided into individual gems

As of the current Ruby version,
how many standard gems do we have?

30

23 default gems
7 bundled gems


json, openssl, bigdecimal, fileutils, …

How many will be added in 2.6?

15


logger, irb, matrix, fileutils, …and bundler

How many libraries does the stdlib contain altogether?

107

With Ruby 2.6 we will be at 43% gemification

Info & tables at stdgems.org

Section C

Misc fun stuff

How to produce this output? (1)


[1,2].____ { |x| [x, x**2] }
# => {1 => 1, 2 => 4}


How to produce this output? (1)


[1,2].to_h { |x| [x, x**2] }
# => {1 => 1, 2 => 4}


Before, we had to use Hash[]

How to produce this output? (2)


[*(1..20)_3]
#=> [1, 4, 7, 10, 13, 16, 19]


How to produce this output? (2)


[*(1..20)%3]
#=> [1, 4, 7, 10, 13, 16, 19]


The new Range#% is an alias to Range#step

How to produce this output? (3)


a = (7..9).each
b = Prime.each
a._____(b).take(7)
#=> [7, 8, 9, 2, 3, 5, 7]


How to produce this output? (3)


a = (7..9).each
b = Prime.each
a.chain(b).take(7)
#=> [7, 8, 9, 2, 3, 5, 7]


Merge multiple enumerators with Enumerable#chain / +

How to charge your Ruby with superpowers?


$ ruby -S rails server
# Rails is slow…


How to charge your Ruby with superpowers?


$ ruby --jit -S rails server
# Rails is around 2.6x faster!
(optimistic guess)


Powered by the new Just-In-Time compiler

Final Section


Ruby 2.6 allows constants to begin with all uppercased letters!

WHICH ONE OF THE CLASS NAMES IS VALID? (1)

class β„»
end
class Γ„tsch
end
Syntax OK


$ uniscribe "β„»"
213B β”œβ”€ β„» β”œβ”€ FACSIMILE SIGN

WHICH ONE OF THE CLASS NAMES IS VALID? (2)

class ΔΏogger
end
class κ“‘ogger
end
Syntax OK


$ uniscribe "ΔΏκ“‘"
013F β”œβ”€ ΔΏ β”œβ”€ LATIN CAPITAL LETTER L WITH MIDDLE DOT
A4E1 β”œβ”€ κ“‘ β”œβ”€ LISU LETTER LA

WHICH ONE OF THE CLASS NAMES IS VALID? (3)

class Ξ”
end
class πŸœ‚
end
Syntax OK


$ uniscribe "Ξ”πŸœ‚"
 0394 β”œβ”€ Ξ” β”œβ”€ GREEK CAPITAL LETTER DELTA
1F702 β”œβ”€ πŸœ‚ β”œβ”€ ALCHEMICAL SYMBOL FOR FIRE

WHICH ONE OF THE CLASS NAMES IS VALID? (4)

class πŸ…†
end
class πŸ…
end
Syntax OK


$ uniscribe "πŸ…†πŸ…"
1F146 β”œβ”€ πŸ…† β”œβ”€ SQUARED LATIN CAPITAL LETTER W
1F14F β”œβ”€ πŸ… β”œβ”€ SQUARED WC

WHICH ONE OF THE CLASS NAMES IS VALID? (5)

class πŸ‘“
end
class 𐲄
end
Syntax OK


$ uniscribe "πŸ‘“π²„"
1F853 β”œβ”€ πŸ‘“ β”œβ”€ DOWNWARDS SANS-SERIF ARROW
10C84 β”œβ”€ 𐲄 β”œβ”€ OLD HUNGARIAN CAPITAL LETTER EC

That was: Recreational Ruby

idiosyncratic-ruby.com/quiz

@JanLelis