Ruby Tips Part 1
globaldev.co.uk
globaldev.co.uk
What is with the trend of mid-range gray on white or light gray these days? Does anyone find that easy to read?
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) 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. return nil unless other.is_a?(self.class) while true do
puts 'lol'
end unless true
but seriously, I like postconditions. they're also handy for encoding intent, if you have a pattern of the left-hand-side being the primary logic flow. other.is_a?(self.class) || return
if you prefer that.