Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Weird behaviour for a weird line of code
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  14 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Mark Rada  
View profile  
 More options Nov 14 2010, 9:37 pm
From: Mark Rada <mr...@marketcircle.com>
Date: Sun, 14 Nov 2010 21:37:47 -0500
Local: Sun, Nov 14 2010 9:37 pm
Subject: [MacRuby-devel] Weird behaviour for a weird line of code
Hey,

I wrote a line of code that I thought was correct (but maybe not so pretty) and in one case it was giving me a weird error. I was wondering if someone could explain to me what is wrong with it; I suspect there is some behaviour of MacRuby that I am not understanding. Here are a few examples that are close to what I had:

A working case: (Case #1)

        require 'uri'
        test = URI.parse url unless (url = nil).nil? # works, as I expected, url is nil and the left side is not evaluated

Ok, try again, but without a nil value for url: (Case #2)

        require 'uri'
         test = URI.parse url unless (url = 'http://macruby.org/').nil?  # error about url not being defined

Now, when I try this out in macirb: (Case #3)

        require 'uri'
         test = URI.parse url unless (url = 'http://macruby.org/').nil?  # error again

         test = URI.parse url unless (url = 'http://wikipedia.org/').nil? # works fine this time

        puts test.to_s  # gives me the wikipedia value

If it doesn't work in the second case, why does it start working in the third case?

Thanks,
        Mark

_______________________________________________
MacRuby-devel mailing list
MacRuby-de...@lists.macosforge.org
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Neat MacRuby app, HotCocoa, and more!" by Leigh Caplan
Leigh Caplan  
View profile  
 More options Nov 15 2010, 12:29 am
From: Leigh Caplan <tex...@gmail.com>
Date: Sun, 14 Nov 2010 21:29:50 -0800
Local: Mon, Nov 15 2010 12:29 am
Subject: Re: [MacRuby-devel] Neat MacRuby app, HotCocoa, and more!
I had digest mode turned on, so this reply might be orphaned. Sorry in advance if that's the case.

Thanks Matt for showing me your HTTP wrapper... there are some good ideas there.

In reply to Josh, what would be the advantages of using NSURLConnection as the transport for the net/* libraries? I can see that having access to two different built-in mechanisms for dealing with the network may be confusing; I used NSURLConnection as an example, but there are many facets of Cocoa which don't have corresponding support in Ruby, and that's where I see a great opportunity for a wrapper library. I just find it way easier to reason about my code if async callbacks are defined inline using procs and the like.

I can also see disadvantages to this approach, such as extra indirection whilst debugging, and potential for more memory usage if the implementers (read: me) aren't careful about making sure that stored procs eventually go out of scope.

Additionally, it probably wouldn't make sense to wrap everything in all the frameworks– we could probably identify logical groups of classes where having more Ruby-like conventions would be a good fit. I'll go over Foundation.framework in the next week and see if I can do just that.

Leigh
_______________________________________________
MacRuby-devel mailing list
MacRuby-de...@lists.macosforge.org
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Weird behaviour for a weird line of code" by Ryan Davis
Ryan Davis  
View profile  
 More options Nov 15 2010, 4:50 pm
From: Ryan Davis <ryand-r...@zenspider.com>
Date: Mon, 15 Nov 2010 13:50:49 -0800
Local: Mon, Nov 15 2010 4:50 pm
Subject: Re: [MacRuby-devel] Weird behaviour for a weird line of code

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.

That said... This actually has nothing to do with macruby and is an effect of the ruby parser. It is about variable visibility.

In case 3:

+ The first line has "URI.parse url" followed by "unless (url = ...).nil?"
  + The first 'url' is actually parsed as a method call because the word has never been seen before and wasn't added to the variable table. The second url DOES add url to the variable table.
+ The second line has the same thing, but by now, "url" has been added to the variable table. This lets everything be parsed as a variable at parse time.

Here is how you should code it:

>   require 'uri'
>   url  = "http://macruby.org/"
>   test = URI.parse url if url
>   url  = "http://wikipedia.org/"
>   test = URI.parse url if url

no variable parse ambiguity. no testing for nil specifically (.nil? is almost NEVER needed, just test for truthiness--imagine what happens when someone sets url to false with your code). clean. readable.

Your code all it's nerdiness:

s(:block,
 s(:if, s(:call, s(:lasgn, :url, s(:str, "http://macruby.org/")),
          :nil?, s(:arglist)),
   nil,
   s(:lasgn, :test,
    s(:call, s(:const, :URI), :parse,
      s(:arglist, s(:call, nil, :url, s(:arglist)))))), # HERE IT IS A CALL
 s(:if,
   s(:call, s(:lasgn, :url, s(:str, "http://wikipedia.org/")), :nil?,
     s(:arglist)),
   nil,
   s(:lasgn, :test,
    s(:call, s(:const, :URI), :parse, s(:arglist, s(:lvar, :url))))))

_______________________________________________
MacRuby-devel mailing list
MacRuby-de...@lists.macosforge.org
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark Rada  
View profile  
 More options Nov 16 2010, 12:25 am
From: Mark Rada <mr...@marketcircle.com>
Date: Tue, 16 Nov 2010 00:25:06 -0500
Local: Tues, Nov 16 2010 12:25 am
Subject: Re: [MacRuby-devel] Weird behaviour for a weird line of code

On 2010-11-15, at 4:50 PM, Ryan Davis 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.

> That said... This actually has nothing to do with macruby and is an effect of the ruby parser. It is about variable visibility.

Ah, that is what I was not understanding. Thanks for explaining.

> In case 3:

> + The first line has "URI.parse url" followed by "unless (url = ...).nil?"
>  + The first 'url' is actually parsed as a method call because the word has never been seen before and wasn't added to the variable table. The second url DOES add url to the variable table.
> + The second line has the same thing, but by now, "url" has been added to the variable table. This lets everything be parsed as a variable at parse time.

_______________________________________________
MacRuby-devel mailing list
MacRuby-de...@lists.macosforge.org
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Christopherson  
View profile  
 More options Nov 16 2010, 5:14 pm
From: Eric Christopherson <echristopher...@gmail.com>
Date: Tue, 16 Nov 2010 16:14:56 -0600
Local: Tues, Nov 16 2010 5:14 pm
Subject: Re: [MacRuby-devel] Weird behaviour for a weird line of code

On Mon, Nov 15, 2010 at 3:50 PM, Ryan Davis <ryand-r...@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)
_______________________________________________
MacRuby-devel mailing list
MacRuby-de...@lists.macosforge.org
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeff Cohen  
View profile  
 More options Nov 16 2010, 5:55 pm
From: Jeff Cohen <cohen.j...@gmail.com>
Date: Tue, 16 Nov 2010 16:55:22 -0600
Local: Tues, Nov 16 2010 5:55 pm
Subject: Re: [MacRuby-devel] Weird behaviour for a weird line of code

On Tue, Nov 16, 2010 at 4:14 PM, Eric Christopherson <

echristopher...@gmail.com> wrote:
> On Mon, Nov 15, 2010 at 3:50 PM, Ryan Davis <ryand-r...@zenspider.com>
> wrote:
> > 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?

It's bad to assign inside a condition 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)

Um... just do:

x = n > 2

Jeff

purpleworkshops.com

_______________________________________________

_______________________________________________
MacRuby-devel mailing list
MacRuby-de...@lists.macosforge.org
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ryan Davis  
View profile  
 More options Nov 16 2010, 6:22 pm
From: Ryan Davis <ryand-r...@zenspider.com>
Date: Tue, 16 Nov 2010 15:22:06 -0800
Local: Tues, Nov 16 2010 6:22 pm
Subject: Re: [MacRuby-devel] Weird behaviour for a weird line of code

On Nov 16, 2010, at 14:14 , Eric Christopherson wrote:

> 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)

this is not an assignment inside a conditional. It is a conditional inside an assignment. And as Jeff pointed out, while just an example, it is a very contrived example (that hits one of my biggest pet peeves).

an assignment inside a conditional is

do_something(x) if x = logical_statement

or

if x = logical_statement then
  do_something(x)
else
  do_something_else
end

and whether you're coding in ruby, C/++, or whatever... it is almost always considered bad form. Avoid it not only for the reasons I mentioned before, but also to avoid the beat downs you'll get whenever you ask for our community's help. I cannot guarantee your safety otherwise.

_______________________________________________
MacRuby-devel mailing list
MacRuby-de...@lists.macosforge.org
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Caio Chassot  
View profile  
 More options Nov 16 2010, 7:20 pm
From: Caio Chassot <li...@caiochassot.com>
Date: Tue, 16 Nov 2010 22:20:36 -0200
Local: Tues, Nov 16 2010 7:20 pm
Subject: Re: [MacRuby-devel] Weird behaviour for a weird line of code
On 2010-11-16, at 21:22 , Ryan Davis wrote:

> an assignment inside a conditional is

> do_something(x) if x = logical_statement

I'm particularly fond of:

  x = logical_statement and do_something(x)

Hate me now.

_______________________________________________
MacRuby-devel mailing list
MacRuby-de...@lists.macosforge.org
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eloy Duran  
View profile  
 More options Nov 18 2010, 5:02 am
From: Eloy Duran <eloy.de.en...@gmail.com>
Date: Thu, 18 Nov 2010 11:02:35 +0100
Local: Thurs, Nov 18 2010 5:02 am
Subject: Re: [MacRuby-devel] Weird behaviour for a weird line of code

> if x = logical_statement then
>  do_something(x)
> else
>  do_something_else
> end

> and whether you're coding in ruby, C/++, or whatever... it is almost always considered bad form. Avoid it not only for the reasons I mentioned before, but also to avoid the beat downs you'll get whenever you ask for our community's help. I cannot guarantee your safety otherwise.

I see no problem with this, as long as you know what you’re doing (which is with almost anything). Nor have I witnessed a community where ‘beat downs’ are acceptable, it’s certainly not this ML. YMMV.
_______________________________________________
MacRuby-devel mailing list
MacRuby-de...@lists.macosforge.org
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Scott Ribe  
View profile  
 More options Nov 18 2010, 9:48 am
From: Scott Ribe <scott_r...@killerbytes.com>
Date: Thu, 18 Nov 2010 07:48:03 -0700
Local: Thurs, Nov 18 2010 9:48 am
Subject: Re: [MacRuby-devel] Weird behaviour for a weird line of code
On Nov 18, 2010, at 3:02 AM, Eloy Duran wrote:

> I see no problem with this, as long as you know what you’re doing (which is with almost anything). Nor have I witnessed a community where ‘beat downs’ are acceptable, it’s certainly not this ML. YMMV.

While "beat down" is clearly an exaggeration, the assignment inside condition form is certainly strongly discourage in many communities, because it's so easy to type by accident, and sometimes surprisingly hard to notice ;-)

--
Scott Ribe
scott_r...@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice

_______________________________________________
MacRuby-devel mailing list
MacRuby-de...@lists.macosforge.org
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eloy Duran  
View profile  
 More options Nov 18 2010, 11:01 am
From: Eloy Duran <eloy.de.en...@gmail.com>
Date: Thu, 18 Nov 2010 17:01:00 +0100
Subject: Re: [MacRuby-devel] Weird behaviour for a weird line of code
I don’t want to discuss this at length, but “clearly an exaggeration” is not necessarily true. This is an international mailing-list, i.e. not all native English speakers. As such it's better to refrain from it and instead use clear language such as your “strongly discouraged, because…”. To me it was clear that those specific words were an exaggeration, but there definitely was some judgement being passed in name of ‘our community’, something which I am not happy to have on this list.

The gist of it is: anyone should be able to ask any type of question and they should not have any fear doing so. Something that newbies can have a hard time with.

Cheers,
Eloy

On Nov 18, 2010, at 3:48 PM, Scott Ribe wrote:

_______________________________________________
MacRuby-devel mailing list
MacRuby-de...@lists.macosforge.org
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matt Aimonetti  
View profile  
 More options Nov 18 2010, 11:19 am
From: Matt Aimonetti <mattaimone...@gmail.com>
Date: Thu, 18 Nov 2010 08:19:50 -0800
Local: Thurs, Nov 18 2010 11:19 am
Subject: Re: [MacRuby-devel] Weird behaviour for a weird line of code

Alright chill out people, there is no need for a drama thread.

For people who don't know Ryan Davis, Ryan is a well known personality of
the Ruby community.
He's a great engineer recognized for his talent and contributions to the
community. However, he is also known to be very passionate about what he
believes in and could be sometimes seen as having a little problem with the
form and the content ;)

In this case, I do agree with Ryan about the content and I think that his
arguments are right. The final argument being that even if you disagree, you
will find others who strongly believe that this kind of syntax is wrong and
you might find yourself in long arguments with passionate people.
Programming isn't yet a religion, so feel free to disagree and hate Ryan as
much as you want. The point being that now you know that some people see
assigning values in conditionals as major programming sin & hopefully you
understand their view point.

Let's not get lost in the semantic and/or the potentially trolling form ;)

Back to hacking and learning!

- Matt

_______________________________________________
MacRuby-devel mailing list
MacRuby-de...@lists.macosforge.org
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eloy Duran  
View profile  
 More options Nov 18 2010, 11:52 am
From: Eloy Duran <eloy.de.en...@gmail.com>
Date: Thu, 18 Nov 2010 17:52:15 +0100
Local: Thurs, Nov 18 2010 11:52 am
Subject: Re: [MacRuby-devel] Weird behaviour for a weird line of code

You’re right, thanks :) Back to hacking and learning!

On 18 nov 2010, at 17:19, Matt Aimonetti wrote:

_______________________________________________
MacRuby-devel mailing list
MacRuby-de...@lists.macosforge.org
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mario Steele  
View profile  
 More options Nov 19 2010, 4:41 am
From: Mario Steele <ma...@ruby-im.net>
Date: Fri, 19 Nov 2010 04:41:10 -0500
Local: Fri, Nov 19 2010 4:41 am
Subject: Re: [MacRuby-devel] Weird behaviour for a weird line of code

Wait....

You mean, Programming isn't a Religion?  Seems to me, I'm always sacrificing
to the Coding Goddess for working code, no matter the language. lol

Just to lighten the mood, and in agreement, Hack and Slash those bits!

Mario

On Thu, Nov 18, 2010 at 11:52 AM, Eloy Duran <eloy.de.en...@gmail.com>wrote:

--
Mario Steele
Lieutenant Commander 3
XO - Geo 99
XO - STO IFT Fleet
http://www.trekfederation.com
http://geo99.ruby-im.net

_______________________________________________
MacRuby-devel mailing list
MacRuby-de...@lists.macosforge.org
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »