For example:

`foo.nil?? “Ahoy” : “Matey”`

@f0x It’s a joke on my part: The first ? is part of the method name (nil?); ruby doesn’t care whether you put a space between that and the following ?…: operator, which works like all other languages :blobcatgiggle:

@f0x Ruby has a few corners of “wtf‽” that emerge from some of its more convenient features. For instance, this:

a = a

will initialize `a` to nil if it’s not already declared (and leave it unchanged otherwise)

@f0x The semantics are: Any variable assignment (statements of the form `<word> = <expression>`) results in the following steps (roughly):

1. Identify that variable in the enclosing scope(s). (`a`)
2. If it doesn’t exist, allocate it in the current scope, initialized to nil. (`a = nil`)
3. Evaluate the expression (`a`, which is currently nil).
4. Perform the assignment.

This results in some other interesting things, like

foo = “bar” if false

will init foo to nil, as before, but skip evaluating `"bar"` *and* skip assigning it. So depending on whether ‘foo’ was already in scope, this will either set it to nil, or leave it unchanged.

Sign in to participate in the conversation
Pixietown

Small server part of the pixie.town infrastructure. Registration is closed.