[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

local vars clobbered by un-run code

Trans

6/19/2007 2:21:00 AM

Err...

irb(main):001:0> def x; 1; end
=> nil
irb(main):002:0> p x
1
=> nil
irb(main):003:0> if false
irb(main):004:1> x = x() + 1
irb(main):005:1> end
=> nil
irb(main):006:0> x
=> nil

Don't tell me, it's some quirk of the way Ruby works. Sure looks like
a bug though. Just in case:

ruby 1.8.4 (2005-12-24) [i486-linux]

Hmm... I just got a dejavu. Maybe I came across this before.

T.


4 Answers

Ian Whitlock

6/19/2007 8:03:00 PM

0

Trans wrote:

>
> Don't tell me, it's some quirk of the way Ruby works. Sure looks like
> a bug though. Just in case:
>
> ruby 1.8.4 (2005-12-24) [i486-linux]
>
> Hmm... I just got a dejavu. Maybe I came across this before.
>
> T.

T all you showed is that sometimes the programmer must be responsible
for knowing whether he wants to reference a function or a variable.

Consider:

def x
1
end

p x #recognize function call without parens

if false
x = x() + 1 #variable x is created when code parsed
end

p x #reference the variable x which is nil because no assignment was
made
p x() #if the above were the function, how would you reference the
variable?

Result:

1
nil
1

Ian

--
Posted via http://www.ruby-....

Robert Dober

6/19/2007 8:27:00 PM

0

On 6/19/07, Ian Whitlock <iw1junk@comcast.net> wrote:
> Trans wrote:
Tom it just strikes me, your thread and mine about Singleton classes
not being in ancestors and small other things I always see the same
pattern. Rick's blog entry is another example, always the same
pattern, it is the pattern of imperfection which is the *only* thing
that slightly bothers me in Ruby:


Consistency.

Maybe, probably I am wrong, but I feel that when it comes to these
subtle things Ruby is lacking consistency.

There just seems no reason for some behavior and your example above is
a classical example. You ask yourself why and you cannot find an
answer.

And worst of all, you have to remember!!

Still a great, great language of course.

Robert

--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Trans

6/19/2007 9:24:00 PM

0



On Jun 19, 4:02 pm, Ian Whitlock <iw1j...@comcast.net> wrote:
> Trans wrote:
>
> > Don't tell me, it's some quirk of the way Ruby works. Sure looks like
> > a bug though. Just in case:
>
> > ruby 1.8.4 (2005-12-24) [i486-linux]
>
> > Hmm... I just got a dejavu. Maybe I came across this before.
>
> > T.
>
> T all you showed is that sometimes the programmer must be responsible
> for knowing whether he wants to reference a function or a variable.

One of things I always liked about Ruby is the fact the local vars and
methods are, in a certain sens of the word, polymorphic. Where a
method is being called, I can easily change it to be a variable. I
don't have to go thru the code and remove ()s or vice versa.

def chip
"%s chip"
end

def good
chip = "forget the %s chip"
puts chip % "good"
end

So if you can do that, it would seem reasonable that one could do so
conditionally as well. I find it odd that x is being setup as a local
var before ever being assigned as such. That's quite declarative for a
language that tends to shuns such things.

T.


Trans

6/19/2007 9:30:00 PM

0



On Jun 19, 4:27 pm, "Robert Dober" <robert.do...@gmail.com> wrote:
> On 6/19/07, Ian Whitlock <iw1j...@comcast.net> wrote:> Trans wrote:
>
> Tom it just strikes me, your thread and mine about Singleton classes
> not being in ancestors and small other things I always see the same
> pattern. Rick's blog entry is another example, always the same
> pattern, it is the pattern of imperfection which is the *only* thing
> that slightly bothers me in Ruby:
>
> Consistency.
>
> Maybe, probably I am wrong, but I feel that when it comes to these
> subtle things Ruby is lacking consistency.
>
> There just seems no reason for some behavior and your example above is
> a classical example. You ask yourself why and you cannot find an
> answer.
>
> And worst of all, you have to remember!!
>
> Still a great, great language of course.

That is true. Ruby does have some rough edges. Though I chalk much of
it up to the fact that convenience and flexibility often comes with a
price of a few quirks.

T.