Ruby

Golfing basics with Ruby

Want to start golfing?

Sébastien Faure
Sébastien Faure

except / slice

Keys to deal with? 🔑

Alexandre Borderes
Alexandre Borderes

Date.parse

The default current century 🤯

Alexandre Borderes
Alexandre Borderes

compact

Filter nil values

Alexandre Borderes
Alexandre Borderes

lazy

Take a break!

Alexandre Borderes
Alexandre Borderes

do...while

The do...while loop in Ruby exists

Alexandre Borderes
Alexandre Borderes

_

Underscore

Alexandre Borderes
Alexandre Borderes

Rightward assignment

The new assignment operator =>

Alexandre Borderes
Alexandre Borderes

tally

Count the number of occurrences

Alexandre Borderes
Alexandre Borderes

"a" + "b" << "c#{"d"}"

String concatenation

Alexandre Borderes
Alexandre Borderes

== vs === vs eql? vs equal?

Why so many methods of comparison?

Alexandre Borderes
Alexandre Borderes

Array and multiplication

The Array class redefines the `*` operator

Alexandre Borderes
Alexandre Borderes

flip-flop

The obscure conditional expression

Alexandre Borderes
Alexandre Borderes

pp

For "pretty print"

Alexandre Borderes
Alexandre Borderes

percent literal

Yes, this piece of code is perfectly correct!

Alexandre Borderes
Alexandre Borderes

cycle 🔄

You'll go round in circles!

Alexandre Borderes
Alexandre Borderes

nonzero?

The opposite of zero?... Well, not exactly!

Alexandre Borderes
Alexandre Borderes

dig ⛏️

Need a pick?

Alexandre Borderes
Alexandre Borderes

split

A special case to be aware of

Alexandre Borderes
Alexandre Borderes

clamp

A bounded method!

Alexandre Borderes
Alexandre Borderes

binding.irb

IRB for Interactive RuBy

Alexandre Borderes
Alexandre Borderes

none? one? any? all?

How they really work

Alexandre Borderes
Alexandre Borderes

command-line

Ruby is also very comfortable in scripting mode. It can replace `sed`, `grep`, `awk`, ...

Alexandre Borderes
Alexandre Borderes

RbConfig

A little-documented module

Alexandre Borderes
Alexandre Borderes

grep

Yes, we're talking about Ruby, not the command-line utility!

Alexandre Borderes
Alexandre Borderes

minMAX

We thought we knew them, but we discovered some subtleties!

Alexandre Borderes
Alexandre Borderes

"s l i c e"

String manipulation in Ruby is very advanced. With the `[]` method or its alias `slice`, it's easy to extract a complex part!

Alexandre Borderes
Alexandre Borderes

*[splat operator]

The splat operator is a handy way of manipulating arrays and arguments.

Alexandre Borderes
Alexandre Borderes

Float precision

Float precision is not infinite. Depending on your use case, you need to be very careful.

Alexandre Borderes
Alexandre Borderes

round(half:)

Rounding in Ruby doesn't work like it does in Python, but you can customize it to change that behavior

Alexandre Borderes
Alexandre Borderes