[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: local variable and closure

Robert Klemme

1/30/2006 4:17:00 PM

Schüle Daniel wrote:
> Hello,
>
> below is one little script that fails at ### <<< line
> the output is
>
> Animal has properties
> Animal will be speakable
> Dog has properties
> Dog will be speakable
> Duck has properties
> Duck will be speakable
> ./dort.rb:16:in `speak': undefined local variable or method `msg' for
> #<Dog:0x401d8da0> (NameError)
> from ./dort.rb:44
>
> I would think that (passed to function) local variable msg would be
> "closed" in self.class_eval {} closure and therefor be accessable
>
> can someone point out why it doesn't work?
>
> Regards, Daniel
>
>
> #!/usr/bin/env ruby
>
> $prop = {}
>
> def prop
> puts "#{self.name} has properties"
> self.class_eval do
> $prop[self] = []
> end
> end
>
> def speakable(msg)
> puts "#{self.name} will be speakable"
> self.class_eval do
> $prop[self] << "I can speak"
> def speak;msg;end ### <<<
> end
> end
>
> class Animal
> prop
> speakable "..."
> def whatCanIDo;$prop[self.class];end
> def self.whatCanIDo;$prop[self];end
> end
>
> class Dog < Animal
> prop
> speakable "wuff"
> end
>
> class Duck < Animal
> prop
> speakable "quak"
> end
>
> a = Dog.new
> b = Duck.new
>
> puts a.speak, b.speak

Without looking too close at your code: def has special scoping rules.
You better try to use define_method:

def speakable(msg)
class_eval do
# ...
define_method(:speak) msg end
end
end

HTH

Btw, what are you trying to accomplish? Somehow I have the feeling that
your code might be more complex than needed. Especially the use of a
global variable irritates me.

Kind regards

robert

5 Answers

Robert Klemme

1/30/2006 5:43:00 PM

0

Schüle Daniel wrote:
> Hello Robert,
>
>> Btw, what are you trying to accomplish? Somehow I have the feeling
>> that your code might be more complex than needed. Especially the
>> use of a global variable irritates me.
>
> complexity was the intention of my code :)

:-)

> no really, I am playing with metaprogramming, also I wanted to know
> how to implement own functions like "attr_accessor"

One way:

class Module
def i_am_in_class_scope(*a)
p a
end
end

>> class Foo
>> i_am_in_class_scope :foo, :bar
>> end
[:foo, :bar]
=> nil


> btw is there special name for this kind of functions?
> functions that are executed in the class scope and can refer with
> "self" to the class they are inside.

I'd call them "class instance methods".

Kind regards

robert

Daniel Schüle

1/30/2006 6:23:00 PM

0

Hello Robert,

> Btw, what are you trying to accomplish? Somehow I have the feeling that
> your code might be more complex than needed. Especially the use of a
> global variable irritates me.

complexity was the intention of my code :)
no really, I am playing with metaprogramming, also I wanted to know how
to implement own functions like "attr_accessor"
btw is there special name for this kind of functions?
functions that are executed in the class scope and can refer with "self"
to the class they are inside.

Regards, Daniel

Werner

6/22/2012 7:10:00 PM

0

On Friday, June 22, 2012 2:48:43 PM UTC-4, Islander wrote:
> On 6/22/2012 10:20 AM, Werner wrote:
> > On Friday, June 22, 2012 1:04:12 PM UTC-4...
> >
> >
> > Are you suggesting politics trumps law in the nation of laws?
> >
> After years of packing federal courts with judges that are conservative,
> I would have to say that yes, the courts have become political.
>


But packing them with your political judges is not political. How do you live with yourself?
http://www....

GLOBALIST

6/22/2012 7:28:00 PM

0


> But packing them with your political judges is not political. How do you live with yourself?
> http://www....
===========================
Obama packed this court and it is going to come
back to haunt him because they are not going
to dance to his tune.

Islander

6/22/2012 9:51:00 PM

0

On 6/22/2012 12:09 PM, Werner wrote:
> On Friday, June 22, 2012 2:48:43 PM UTC-4, Islander wrote:
>> On 6/22/2012 10:20 AM, Werner wrote:
>>> On Friday, June 22, 2012 1:04:12 PM UTC-4...
>>>
>>>
>>> Are you suggesting politics trumps law in the nation of laws?
>>>
>> After years of packing federal courts with judges that are conservative,
>> I would have to say that yes, the courts have become political.
>>
>
>
> But packing them with your political judges is not political. How do you live with yourself?
> http://www....
>
That, of course, is not what I said. You conveniently deleted my
comments from your response. Shame on you!

>> After years of packing federal courts with judges that are
conservative,
>> I would have to say that yes, the courts have become political.
>> Personally, I think that political appointments to the bench and to
>> federal prosecuting attorney positions should be eliminated.
>>
>> But then, we have just seen a total incompetent elected to the
judgeship
>> in San Diego. Kreep beat Peed by about 1000 votes.
>>
>> Perhaps we should limit nominations to those individuals advanced by
the
>> Bar Associations who seem to be the only body that can judge
>> qualifications.
>>
>> Christ, Dubya even attempted to install his cleaning lady - and I mean
>> that in the most respectful way possible!
>>