

Ruby Tips Part 1 - Peroni
http://globaldev.co.uk/2013/09/ruby-tips-part-1/

======
GrinningFool
Sorry, offtopic mini-rant coming: this site (and so many others) become much
more readable if you disable the body text CSS. Actually this specific site
does quite well when you disable all CSS.

What is with the trend of mid-range gray on white or light gray these days?
Does _anyone_ find that easy to read?

~~~
fedesilva
Maybe it's old age but I find that annoying too. Looks pretty but is
unreadable.

------
hk__2
Is this:

    
    
        def fibonacci(max=Float::INFINITY)
        # ...
          while i < max
            # ...
          end
        end
    

better than this:

    
    
        def fibonacci(max=nil)
        # ...
          while max && i < max
            # ...
          end
        end
    

? (genuine question)

~~~
Xylakant
yes. The first one does what it's intended to, the second plain fails :)

    
    
        Voice-of-Evening:~ $ irb
        irb(main):001:0> max=Float::INFINITY
        => Infinity
        irb(main):002:0> i = 0
        => 0
        irb(main):003:0> i < max
        => true
        irb(main):004:0> max = nil
        => nil
        irb(main):005:0> max && i < max
        => nil
    

The second version will only provide any result when a specific max value was
given, while the first one will loop into infinity.

~~~
hk__2
Yes, I messed up. I meant `!max && i < max`.

~~~
Xylakant
Then the code is functionally equivalent, but the first version is much more
readable. As pointed out on the parent comment, the first version does one
test less, a minor performance benefit.

------
ldubinets
Ruby is neat! Except for this part:

    
    
        return nil unless other.is_a?(self.class)

~~~
cheald
Post conditions are odd if you're not used to them, but once you start to
mentally allow for them, they're really quite nice.

~~~
ldubinets
Ah, seems everybody misunderstood my comment (not hard to see why). It's not
the post conditions that bother me, its the dynamic typing! In my opinion,
ruby's duck typing really only works until you actually need it.

