Re:Ruby Tips Part 1
by GrinningFool on Monday September 02, 2013 @03:44PM
(#6316769)
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?
Re:Ruby Tips Part 1
by nilved on Monday September 02, 2013 @04:36PM
(#6317069)
What you meant is `while !max || max < i`, but the answer is the same: the former will perform better because it only makes one comparison for each iteration of the loop. The latter needs to additionally check the existence of max.
Re:Ruby Tips Part 1
by Xylakant on Monday September 02, 2013 @12:46PM
(#6315914)
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.
Re:Ruby Tips Part 1
by Xylakant on Tuesday September 03, 2013 @02:38AM
(#6319378)
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.
Re:Ruby Tips Part 1
by Groxx on Monday September 02, 2013 @04:31PM
(#6317032)
I'm a fan of:
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.
Re:Ruby Tips Part 1
by xentronium on Monday September 02, 2013 @03:08PM
(#6316593)
Is it a statement-modifier 'unless' that bothers you? `return ... unless ...` is kind of idiomatic way of returning early in the function. You get used to this shortcut ridiculously fast.
Re:Ruby Tips Part 1
by klibertp on Monday September 02, 2013 @02:41PM
(#6316470)
Wait, why? Try to read it aloud and it will make sense. I've seen lot's of hard and confusing syntax but this is not one of them. And I'm not a Ruby developer.
Re:Ruby Tips Part 1
by ldubinets on Monday September 16, 2013 @02:13AM
(#6391898)
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.
What is with the trend of mid-range gray on white or light gray these days? Does anyone find that easy to read?