[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Avoiding scope pitfall

gga

4/5/2007 10:37:00 AM

Here's a simple snippet of code of a ruby scope pitfall (bug?) that I
keep stumbling upon.
Here a "global" function is invoked in two different contexts and
returns different behavior due to self being set differently.

---
#!/usr/bin/env ruby

@x = 'hello global'

def global_func
puts "global_func: class:#{self.class} x=#@x"
end

class A
def test_scope
global_func
end
end

global_func

a = A.new
a.test_scope
---

I'm wondering if this is intended behavior in the language. If
intended, what's the best approach to avoid running into this? I'm
currently porting code from other languages with different (more
correct to me) scoping, and this keeps tripping me.

6 Answers

Robert Klemme

4/5/2007 11:19:00 AM

0

On 05.04.2007 12:37, gga wrote:
> Here's a simple snippet of code of a ruby scope pitfall (bug?) that I
> keep stumbling upon.
> Here a "global" function is invoked in two different contexts and
> returns different behavior due to self being set differently.
>
> ---
> #!/usr/bin/env ruby
>
> @x = 'hello global'
>
> def global_func
> puts "global_func: class:#{self.class} x=#@x"
> end
>
> class A
> def test_scope
> global_func
> end
> end
>
> global_func
>
> a = A.new
> a.test_scope
> ---
>
> I'm wondering if this is intended behavior in the language.

It is intended:

13:16:34 [Temp]: ./scope.rb
#<Method: Object#global_func>
global_func: class:Object x=hello global
global_func: class:A x=
13:16:50 [Temp]: cat scope.rb
#!/usr/bin/env ruby

@x = 'hello global'

def global_func
puts "global_func: class:#{self.class} x=#@x"
end

p method( :global_func )

class A
def test_scope
global_func
end
end

global_func

a = A.new
a.test_scope
13:16:53 [Temp]:

Every top level method is defined in class Object and thus inherited by
all classes.

> If
> intended, what's the best approach to avoid running into this? I'm
> currently porting code from other languages with different (more
> correct to me) scoping, and this keeps tripping me.

Do not use instance variables in global *functions*. As simple as that.

Regards

robert

tsela.cg

4/5/2007 11:33:00 AM

0

On Apr 5, 12:37 pm, "gga" <GGarram...@aol.com> wrote:
> Here's a simple snippet of code of a ruby scope pitfall (bug?) that I
> keep stumbling upon.
> Here a "global" function is invoked in two different contexts and
> returns different behavior due to self being set differently.
>
> ---
> #!/usr/bin/env ruby
>
> @x = 'hello global'
>
> def global_func
> puts "global_func: class:#{self.class} x=#@x"
> end
>
> class A
> def test_scope
> global_func
> end
> end
>
> global_func
>
> a = A.new
> a.test_scope

I don't know why you call that a pitfall. I tested the behaviour and
it did exactly what I expected.

I think you are confused by what you call "global functions". You'd
better forget them: Ruby doesn't have global functions. It only has
methods. When you define a so-called "global function", you're just
defining a method at the top level, which is in an object of class
Oject, called main. Due to the scope rules at the top level, defining
a method there adds it to the class Object, as an instance method.
Nothing strange there so far.

Now when you define class A, it becomes automatically a subclass of
Object, as you probably know, since Object is the root of the class
hierarchy. As a subclass of Object, it gains all Object's instance
methods, including the one you just defined: "global_func" (you can
check that by running a.methods). So when you call "global_func" in
the test_scope method, you are actually calling an inherited method,
which will run then in the scope of the a object you created, *as one
would expect from inherited methods*! So when you run a.test_scope,
global_func will run in the scope of a, where self.class is A and @x
hasn't been assigned and thus returns nil. This is perfectly normal,
given the rules of the Ruby language.

> ---
>
> I'm wondering if this is intended behavior in the language. If
> intended, what's the best approach to avoid running into this? I'm
> currently porting code from other languages with different (more
> correct to me) scoping, and this keeps tripping me.

Well, the answer is that yes, it is the intended behaviour in the
language, in the sense that it is completely valid behaviour according
to the rules of the language. Just forget about global functions: they
are just methods of the class Object, which become available to all
objects only due to inheritance, and thus follow *all* the rules of
inherited methods, included the rule that when called in an instance
of a subclass, they run in the scope of that instance. Normally
functions defined on the top level don't care about this, as their
behaviour normally does not depend on the state of the object they're
called in (like "puts", which will output to $stdout anyway, since
$stdout is a true global variable which refers to the same IO object
everywhere). For others, we just don't expect them to behave
differently (no one would expect the "class" method to return anything
but the class of the object they're in).

A way to remember that behaviour might be to remember that methods
defined on the top level act as if they had a "class Object ... end"
surrounding their definition. That might help you remember that they
are inherited to other classes, rather than simply have a global
scope.

And if you don't want that behaviour, just have your top-level methods
never rely on the scope they're running in. That can be done for
instance by only have them use global variables (but watch your thread
safety go bye-bye if you write threaded code :) ) and local variables
(which are local to the scope of the method anyway, wherever that
method's used).

Hopefully with this you should understand why this behaviour is
actually *correct*, when you think clearly about how the Ruby language
works (i.e. it's internally consistent). One thing I've noticed and
liked about Ruby is how consistent it is, and how few exceptions and
little rules you need to learn when you understand its basic
principles. You just need to be consistent yourself in how you are
applying those basic principles to your programs.

Cheers,

Christophe.

Tracey12

4/20/2011 11:54:00 PM

0

On Apr 20, 6:21 pm, Pepe Le Jew <Peps...@zionet.com> wrote:
> In article <sd3uq6pv6obomfvolp0djlkgnr518re...@4ax.com>,
>  Alan Ferris <hairy.fer...@yahoo.co.uk> wrote:
>
> > On Wed, 20 Apr 2011 04:53:38 -0700 (PDT), Tracey12
> > <tracey12em...@gmail.com> wrote:
>
> > >1. The president was not even born in the USA.
>
> > FFS!  you really have to be fucking dumb to believe crap like that.
>
> His granny must be pretty dumb then.

I'm starting to wonder about state controlled media. I don't like to
think about things like that. And, I sure do not like to think that
in America, our media is highly influenced by our government, but in
the past few weeks, we have seen EVERY media outlet in the USA portray
that highly toxic, radioactive disaster in Japan as nothing more than
a flight from LA to NYNY, to Ann Coulte suggesting that it was
actually good for people to have a certain exposure level. People
aren't that dumb in the media.

Obama's grandma says he was born in Kenya, yet the national media
suggests that anyone who questions the birthplace of Obama is a
Christian Fundamentalist Right Wing Radical Tea Party Member. What
happened to the investigative media in America? Where has their
curiosity gone to? Maybe now that there are about five basic owners
of US media, they get their marching orders from a higher source.

Pepe Le Jew

4/21/2011 12:28:00 AM

0

In article
<93ad8ed0-568d-4036-9c4f-7c7143299887@k6g2000prn.googlegroups.com>,
Tracey12 <tracey12email@gmail.com> wrote:

> On Apr 20, 6:21?pm, Pepe Le Jew <Peps...@zionet.com> wrote:
> > In article <sd3uq6pv6obomfvolp0djlkgnr518re...@4ax.com>,
> > ?Alan Ferris <hairy.fer...@yahoo.co.uk> wrote:
> >
> > > On Wed, 20 Apr 2011 04:53:38 -0700 (PDT), Tracey12
> > > <tracey12em...@gmail.com> wrote:
> >
> > > >1. The president was not even born in the USA.
> >
> > > FFS! ?you really have to be fucking dumb to believe crap like that.
> >
> > His granny must be pretty dumb then.
>
> I'm starting to wonder about state controlled media. I don't like to
> think about things like that. And, I sure do not like to think that
> in America, our media is highly influenced by our government, but in
> the past few weeks, we have seen EVERY media outlet in the USA portray
> that highly toxic, radioactive disaster in Japan as nothing more than
> a flight from LA to NYNY, to Ann Coulte suggesting that it was
> actually good for people to have a certain exposure level. People
> aren't that dumb in the media.
>
> Obama's grandma says he was born in Kenya, yet the national media
> suggests that anyone who questions the birthplace of Obama is a
> Christian Fundamentalist Right Wing Radical Tea Party Member. What
> happened to the investigative media in America? Where has their
> curiosity gone to? Maybe now that there are about five basic owners
> of US media, they get their marching orders from a higher source.


Curiosity, integrity and professionalism are not as vital to the nation
as support for Dear Leader.

Leftists yearn for a handsome, brilliant totalitarian, they think Obama
is it.

Well, next to Fidel, of course, but he's taken.

Alan Ferris

4/21/2011 4:20:00 PM

0

On Wed, 20 Apr 2011 16:54:25 -0700 (PDT), Tracey12
<tracey12email@gmail.com> wrote:

>On Apr 20, 6:21?pm, Pepe Le Jew <Peps...@zionet.com> wrote:
>> In article <sd3uq6pv6obomfvolp0djlkgnr518re...@4ax.com>,
>> ?Alan Ferris <hairy.fer...@yahoo.co.uk> wrote:
>>
>> > On Wed, 20 Apr 2011 04:53:38 -0700 (PDT), Tracey12
>> > <tracey12em...@gmail.com> wrote:
>>
>> > >1. The president was not even born in the USA.
>>
>> > FFS! ?you really have to be fucking dumb to believe crap like that.
>>
>> His granny must be pretty dumb then.
>
>I'm starting to wonder about state controlled media. I don't like to
>think about things like that. And, I sure do not like to think that
>in America, our media is highly influenced by our government, but in
>the past few weeks, we have seen EVERY media outlet in the USA portray
>that highly toxic, radioactive disaster in Japan as nothing more than
>a flight from LA to NYNY, to Ann Coulte suggesting that it was
>actually good for people to have a certain exposure level. People
>aren't that dumb in the media.
>
>Obama's grandma says he was born in Kenya, yet the national media
>suggests that anyone who questions the birthplace of Obama is a
>Christian Fundamentalist Right Wing Radical Tea Party Member. What
>happened to the investigative media in America? Where has their
>curiosity gone to? Maybe now that there are about five basic owners
>of US media, they get their marching orders from a higher source.

They investigated, they found the facts but poor racists nutjobs just
cannot accept the facts and keep making up crap and wondering why
everybody laughs at them.

--
Ferrit

()'.'.'()
( (T) )
( ) . ( )
(")_(")

Lee Curtis

4/21/2011 4:59:00 PM

0

Tracey12 wrote:

> On Apr 20, 6:21?pm, Pepe Le Jew <Peps...@zionet.com> wrote:
> > In article <sd3uq6pv6obomfvolp0djlkgnr518re...@4ax.com>,
> > ?Alan Ferris <hairy.fer...@yahoo.co.uk> wrote:
> >
> > > On Wed, 20 Apr 2011 04:53:38 -0700 (PDT), Tracey12
> > > <tracey12em...@gmail.com> wrote:
> >
> > > > 1. The president was not even born in the USA.
> >
> > > FFS! ?you really have to be fucking dumb to believe crap like
> > > that.
> >
> > His granny must be pretty dumb then.
>
> I'm starting to wonder about state controlled media. I don't like to
> think about things like that. And, I sure do not like to think that
> in America, our media is highly influenced by our government, but in
> the past few weeks, we have seen EVERY media outlet in the USA portray
> that highly toxic, radioactive disaster in Japan as nothing more than
> a flight from LA to NYNY, to Ann Coulte suggesting that it was
> actually good for people to have a certain exposure level. People
> aren't that dumb in the media.
>
> Obama's grandma says he was born in Kenya, yet the national media
> suggests that anyone who questions the birthplace of Obama is a
> Christian Fundamentalist Right Wing Radical Tea Party Member. What
> happened to the investigative media in America?


Yes, why AREN'T rightwing "investigators" like
Geraldo and Stossel and their friends at Fox News
looking into this?

Could it be they already did - and found nothing?