Idiosyncratic Ruby

Quiz


@JanLelisJanuary 2021Zoom ℝ𝕌𝔾 𝔹𝕖𝕣𝕝𝕚𝕟

idiosyncratic-ruby.com

Documenting lesser-known features in Ruby


2021: "Stay at home to learn Ruby 3.0"

Ruby's Magic Comments


Also known as:
interpreter instructions or compiler directives


Setting a source file's encoding:

# encOding: bInary
p "".encoding # => #<Encoding:ASCII-8BIT>

Magic Comments

How many different "magic" comments (a.k.a interpreter instructions) do exist in Ruby 3.0?


1


2


3


4


5


6

Magic Comments

How many different "magic" comments (a.k.a interpreter instructions) do exist in Ruby 3.0?


4


# encoding: …

# frozen_string_literal: …

# warn_indent: …

# shareable_constant_value: … ← NEW

# warn_past_scope: … ← NOT COUNTED

Magic Comments

What happens when you specify
# shareable_constant_value: literal
as your magic comment?


It deep-freezes every literal assigned to a constant
or

It deactivates the warning when you are re-assigning a constant
or

It calls Object#share on every literal assigned to a constant
or

It literally allows you to share the constant's source code on github.com

Magic Comments

What happens when you specify
# shareable_constant_value: literal
as your magic comment?


It deep-freezes every literal assigned to a constant


X = [{foo: []}]
# => same as [{foo: [].freeze}.freeze].freeze

Other modes of operation:


- experimental_everything
- experimental_copy

Magic Comments

Where can you place your
# shareable_constant_value
magic comment?


Only in the first line of the file
or

In the first line of the file, but when its taken by a Unix shebang (#!/bin/ruby), or another magic comment, it is possible to put in a subsequent line
or

Anywhere in the file

Magic Comments

Where can you place your
# shareable_constant_value
magic comment?


Anywhere in the file


Changes the behavior of constants below the magic comment within the current module-scope




Magic Comments - Resources


Grammar Definition:
github.com/ruby/ruby/blob/v3_0_0/parse.y#L8085


Official Documentation:
ruby-doc.org/core-3.0.0/doc/syntax/comments_rdoc.html


More Examples:
idiosyncratic-ruby.com/58-magic-instructions.html




Random 3.0 News

Random 3.0 News

What does ... (three dots) do in Ruby 3.0?


Calls a method without any arguments three times or

Forwards a method's arguments to another method or

It is a begin- and endless range (-Infinity to Infinity) or

Separates the RBS type signature from the actual method body

Random 3.0 News

What does ... (three dots) do in Ruby 3.0?


Forwards a method's arguments
to another method


Originally introduced in 2.7,
it has become useful in 3.0:

def repeat(method_to_call, count, ...)
    puts "Will call #{method_to_call} #{count} times"
    count.times {
      public_send(method_to_call, ...)
    }
  end

Random 3.0 News

How many of Ruby's 90 default gems are officially unmaintained?


None (all have a maintainer)


1-5


6-10


11-20


21-30

Random 3.0 News

How many of Ruby's 90 default gems are officially unmaintained?


17


benchmark    date    dbm    debug    delegate    english    fileutils    getoptlong    net-pop    net-protocol    net-smtp    observer    open3    pstore    tempfile    tmpdir    weakref


stdgems.org/unmaintained

Random 3.0 News

Which global variable was removed from Ruby 3.0?


$-0


$-F


$-I


$-K

Random 3.0 News

Which global variable was removed from Ruby 3.0?


$-K


Removed together with $KCODE

Used to change the behavior of default encodings

Mirrored the -K CLI option (which is still present)

Random 3.0 News

Which one of the following statements is not valid Ruby 3.0?


def * = 0


def Time.now = 1970


def !(o) = !!(o)


def a x = x ** 2

Random 3.0 News

Which one of the following statements is not valid Ruby 3.0?


def a x = x ** 2


Endless method definitions with arguments require parenthesis:


def a(x) = x ** 2

Random 3.0 News

What is the output of the following Ruby 3.0 code:

a = 1
2 => a
def a = 3

p a


1

2

3

Random 3.0 News

What is the output of the following Ruby 3.0 code:

a = 1
2 => a
def a = 3

p a


2


Local variable a has precedence over method a

Ruby 3.0 Resources


Things I did not cover:
Keyword argument changes, typing with rbs and typeprof, Ractor for concurrency, ...


Checkout Ruby Changes 3.0
at the Ruby Reference by @zverok




Single-line Pattern Matching /
Right Hand Assignment


Warning[:experimental] = false


# Check if a is true and b is false
if [a, b] in [true, false]

# Separate an array into head an tail
array => [first, *rest]

Single-line Pattern Matching

Which one of the following statements returns false?


2 in 1...3


2 in [1,2,3]


BasicObject in Object


Object in BasicObject




Single-line Pattern Matching

Which one of the following statements returns false?


2 in [1,2,3] # => false

2 => [1,2,3] # NoMatchingPatternError (2)


Single-line Pattern Matching

Which one of the following one-liners does not work?


1 => Numeric => x


2 => a => b => c => d


3 => Integer => Z


4 => -> o { p o } => o




Single-line Pattern Matching

Which one of the following one-liners does not work?


3 => Integer => Z


syntax error, unexpected constant,
expecting local variable or method




Single-line Pattern Matching

Which one of the following statements will raise an error?


0 => 0


0 => 0 => o


0 => 0.0 => o


0 => 0...0 => o




Single-line Pattern Matching

Which one of the following statements will raise an error?


0 => 0...0 => o

# NoMatchingPatternError (0)


0 => 0..0 => o

# => 0




Single-line Pattern Matching

Which of the following new syntaxes is just super neat and awesome?


{ key: 'value' } in { key: }


Returns true if all specified keys were found

Object destructuring: It assigns the specified keys' values to local variables with the keys' names




Ruby got better!
So let's make 2021 better, too ;)


@JanLelis - idiosyncratic-ruby.com/quiz