Saturday, April 16, 2011

Ruby Programming Language Book Notes

1)

minimum = if x < y then x else y end

can also be written as :

minimum = x < y ? x : y

2)

class Fixnum
def inclusive(element)
self..element
end

def exclusive(element)
self...element
end
end

This eliminates the mental mapping from .. and ... to the behaviour of the methods.

3) This does not work:

class Fixnum
alias inclusive ..
alias exclusive ...
end

gives syntax error, unexpected tDOT2

So how do you redefine the .. and ...? If you do ri Fixnum, it shows the instance methods. The section on Operators has discussion on operators that can be re-defined.