[MacRuby-devel] Weird behaviour for a weird line of code

Eric Christopherson echristopherson at gmail.com
Tue Nov 16 14:14:56 PST 2010


On Mon, Nov 15, 2010 at 3:50 PM, Ryan Davis <ryand-ruby at zenspider.com> wrote:
>
> On Nov 14, 2010, at 18:37 , Mark Rada wrote:
>
>> Now, when I try this out in macirb: (Case #3)
>>
>>   require 'uri'
>>   test = URI.parse url unless (url = 'http://macruby.org/').nil?  # error
>>   test = URI.parse url unless (url = 'http://wikipedia.org/').nil? # works
>>
>> If it doesn't work in the second case, why does it start working in the third case?
>
> First off, I hate this style of coding. If you didn't assign in a conditional you'd avoid all of this crap to begin with. Assigning in conditionals is just a sloppy and error prone way of coding and you should avoid it. This has been a known anti-pattern in any algol-esque language since at least the 80s.

I'm not sure I understand what you mean. Are you saying it's bad to do
the *first* assignment to a variable inside a conditional? Or it's bad
to assign inside a conditional in any case? I can understand the
first, but I'm not sure how you would work around the second, unless
you used a more functional style like

x = (n > 2 ? true : false)

or

x = (if n > 2; true; else false; end)


More information about the MacRuby-devel mailing list