Ruby

Golfing basics with Ruby

Want to start golfing?

Sébastien Faure
Sébastien Faure

Goruby

Shorter Ruby!

Alexandre Borderes
Alexandre Borderes

casecmp

Case insensitive comparison

Alexandre Borderes
Alexandre Borderes

||= vs defined?

Cache false/nil

Alexandre Borderes
Alexandre Borderes

Local variable

A detail on how they work

Alexandre Borderes
Alexandre Borderes

tap 👏

For method chaining

Alexandre Borderes
Alexandre Borderes

**{double splat operator}

The double splat operator for Hash

Alexandre Borderes
Alexandre Borderes

benchmark

Need to speed up?

Alexandre Borderes
Alexandre Borderes

rescue ... else

A case to handle if there are no exceptions?

Alexandre Borderes
Alexandre Borderes

flat_map vs flatten

Flatten your arrays!

Alexandre Borderes
Alexandre Borderes

to_h

Convert to Hash

Alexandre Borderes
Alexandre Borderes

super

Handle arguments

Alexandre Borderes
Alexandre Borderes

Endless method

Methods without "end"

Alexandre Borderes
Alexandre Borderes

String#delete

Delete characters

Alexandre Borderes
Alexandre Borderes

group_by

Classify your data

Alexandre Borderes
Alexandre Borderes

for loop

Ruby also has a for loop!

Alexandre Borderes
Alexandre Borderes

fill

To fill an array

Alexandre Borderes
Alexandre Borderes

&.

Reduce the « 1 billion dollar mistake »!

Alexandre Borderes
Alexandre Borderes

_1, _2, ... it?

Numbered parameters

Alexandre Borderes
Alexandre Borderes

yield_self / then

For method chaining

Alexandre Borderes
Alexandre Borderes

+"string"

Mutable string

Alexandre Borderes
Alexandre Borderes

__END__

A little more than a comment

Alexandre Borderes
Alexandre Borderes

IRB assembly

Ruby in your browser

Alexandre Borderes
Alexandre Borderes

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