[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Class variable confusion

Keith Salisbury

4/23/2009 1:55:00 PM

Hi,

Could someone explain how i get this to work please:

module Keith
def self.greeting
@greeting ||= "hello"
end
def self.greeting=(value)
puts "updating knowledge"
@greeting = value
end
def self.speak
puts greeting
end
def self.learn_ruby
self.greeting = "ruby"
end
def self.learn_french
learn_language(greeting, "bonjour")
end
def self.learn_language(prop, value)
puts prop.inspect
prop = value
puts prop.inspect
#p =
value
end
end

Keith.speak
Keith.learn_ruby
Keith.learn_french
Keith.speak



This outputs:

hello
updating knowledge
"ruby"
"bonjour"
ruby


I can't for the life of me get it to work...


many thanks
keith
--
Posted via http://www.ruby-....

8 Answers

Keith Salisbury

4/23/2009 2:01:00 PM

0

Or more simply, i would like this to update the variable @greeting, when
i call Keith.learn_french:

module Keith
def self.greeting
@greeting ||= "hello"
end
def self.greeting=(value)
puts "updating knowledge"
@greeting = value
end
def self.speak
puts greeting
end
def self.learn_french
learn_language(greeting, "bonjour")
end
def self.learn_language(prop, value)
prop = value
end
end

Keith.speak
Keith.learn_french
Keith.speak

But the only way i can get it to work is using this:

module Keith
def self.greeting
@greeting ||= "hello"
end
def self.greeting=(value)
puts "updating knowledge"
@greeting = value
end
def self.speak
puts greeting
end
def self.learn_french
self.greeting = learn_language(greeting, "bonjour")
end
def self.learn_language(prop, value)
prop = value
end
end

Keith.speak
Keith.learn_french
Keith.speak


Which is basically the same as:

module Keith
def self.greeting
@greeting ||= "hello"
end
def self.greeting=(value)
puts "updating knowledge"
@greeting = value
end
def self.speak
puts greeting
end
def self.learn_french
self.greeting = "bonjour"
end
end

Keith.speak
Keith.learn_french
Keith.speak


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

Jesús Gabriel y Galán

4/23/2009 2:22:00 PM

0

On Thu, Apr 23, 2009 at 4:01 PM, Keith Salisbury
<keithsalisbury@gmail.com> wrote:
> Or more simply, i would like this to update the variable @greeting, when
> i call Keith.learn_french:
>
> module Keith
> =A0def self.greeting
> =A0 =A0@greeting ||=3D "hello"
> =A0end
> =A0def self.greeting=3D(value)
> =A0 =A0puts "updating knowledge"
> =A0 =A0@greeting =3D value
> =A0end
> =A0def self.speak
> =A0 =A0puts greeting
> =A0end
> =A0def self.learn_french
> =A0 =A0learn_language(greeting, "bonjour")

Here, you are calling the greeting method (which returns "hello")

> =A0end
> =A0def self.learn_language(prop, value)
> =A0 =A0prop =3D value

Here you are reassigning a local variable, which goes out of scope
when the method ends.
Try this:


jesus@jesus-laptop:~/temp/ruby$ ruby class_variables.rb
hello
bonjour

jesus@jesus-laptop:~/temp/ruby$ cat class_variables.rb
module Keith
def self.greeting
@greeting ||=3D "hello"
end
def self.greeting=3D(value)
puts "updating knowledge"
@greeting =3D value
end
def self.speak
puts greeting
end
def self.learn_french
learn_language(:@greeting, "bonjour")
end
def self.learn_language(prop, value)
instance_variable_set(prop, value)
end
end

Keith.speak
Keith.learn_french
Keith.speak


or if you want to call the self.greeting=3D method, try this variation:

def self.learn_french
learn_language(:greeting, "bonjour")
end
def self.learn_language(prop, value)
send("#{prop}=3D", value)
end

Hope this helps,

Jesus.

Keith Salisbury

4/23/2009 2:39:00 PM

0

Jesús Gabriel y Galán wrote:
> Hope this helps,
>
> Jesus.

Legend

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

David A. Black

4/23/2009 2:57:00 PM

0

Hi --

Keith Salisbury wrote:
> Jesús Gabriel y Galán wrote:
>> Hope this helps,
>>
>> Jesus.
>
> Legend
>
> Thankyou!!

It's worth mentioning that none of the code in this thread contains any
class variables. What you've got are instance variables (belonging to a
class object). Class variables look like @@this.


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.r...
Now out in PDF: The Well-Grounded Rubyist (http://manning....)
"Ruby 1.9: What You Need To Know" Envycasts with David A. Black
http://www.env...



Rob Biedenharn

4/23/2009 5:05:00 PM

0

On Apr 23, 2009, at 10:01 AM, Keith Salisbury wrote:

> Or more simply, i would like this to update the variable @greeting,
> when
> i call Keith.learn_french:
>
> module Keith
> def self.greeting
> @greeting ||= "hello"
> end
> def self.greeting=(value)
> puts "updating knowledge"
> @greeting = value
> end
> def self.speak
> puts greeting
> end
> def self.learn_french
> learn_language(greeting, "bonjour")
> end
> def self.learn_language(prop, value)
> prop = value
> end
> end
>
> Keith.speak
> Keith.learn_french
> Keith.speak
>
> But the only way i can get it to work is using this:
>
> module Keith
> def self.greeting
> @greeting ||= "hello"
> end
> def self.greeting=(value)
> puts "updating knowledge"
> @greeting = value
> end
> def self.speak
> puts greeting
> end
> def self.learn_french
> self.greeting = learn_language(greeting, "bonjour")
> end
> def self.learn_language(prop, value)
> prop = value
> end
> end
>
> Keith.speak
> Keith.learn_french
> Keith.speak
>
>
> Which is basically the same as:
>
> module Keith
> def self.greeting
> @greeting ||= "hello"
> end
> def self.greeting=(value)
> puts "updating knowledge"
> @greeting = value
> end
> def self.speak
> puts greeting
> end
> def self.learn_french
> self.greeting = "bonjour"
> end
> end
>
> Keith.speak
> Keith.learn_french
> Keith.speak
> --
> Posted via http://www.ruby-....


Keeping the other methods the same:

module Keith
def self.learn_french
learn_language(:greeting, "bonjour")
end
def self.learn_language(prop, value)
self.send("#{prop}=", value)
end
end


irb> Keith.speak
hello
=> nil
irb> Keith.learn_french
updating knowledge
=> "bonjour"
irb> Keith.speak
bonjour
=> nil

Although I'd think that you'd be better with a Speaker class and do:
Keith = Speaker.new
Then you'd be dealing with instance variables on instances of the
Speaker class rather than instance variables on the Keith module.
Contrary to what you might think, you don't have class variables in
your code. (Those would be @@greeting)

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com



Dave Kelly

11/13/2011 10:11:00 PM

0


"iLL_Schizo" <caljamscott@yahoo.com> hallucinated in message

> That is what you "heard."

What I ALSO "heard" is that you're a schizophrenic.
Whats the name of your mental health case worker?
I think we need to do an intervention with you....
you have stage 3 cancer....you're mentally unbalanced
....and obviously off your meds.
Why spend your last days sitting around your residency hotel
room watching scratchy VHS tapes of "George "The Animal" Steel
and Captain Lou Albano when you could do something productive....
like apologizing to the 624 internet chatgroups you harrass on
a daily basis...and to the librarians down at the Bronx public library
who spend most of they shifts trying to keep you out of the bathroom
when children are using it...they DO have books to re-shelf ya know.
We will collectively dance on your shallow, unmarked grave, Ree-Tod.


wereo_boy

11/14/2011 2:45:00 AM

0

"sweetbac" <sweetbac@sbcglobal.net> wrote in message
news:j9pfa6$rv1$1@dont-email.me...
>
> "iLL_Schizo" <caljamscott@yahoo.com> hallucinated in message
>
>> That is what you "heard."
>
> What I ALSO "heard" is that you're a schizophrenic.
> Whats the name of your mental health case worker?
> I think we need to do an intervention with you....
> you have stage 3 cancer....you're mentally unbalanced
> ...and obviously off your meds.
> Why spend your last days sitting around your residency hotel
> room watching scratchy VHS tapes of "George "The Animal" Steel
> and Captain Lou Albano when you could do something productive....
> like apologizing to the 624 internet chatgroups you harrass on
> a daily basis...and to the librarians down at the Bronx public library
> who spend most of they shifts trying to keep you out of the bathroom
> when children are using it...they DO have books to re-shelf ya know.
> We will collectively dance on your shallow, unmarked grave, Ree-Tod.
>

I don't think the 2 1/2 million are going to do that.


James Pablos

11/14/2011 3:48:00 AM

0

In article <j9pvb3$poh$1@dont-email.me>,
"WeReo_BoY" <invaild12345@aol.com> wrote:

> "sweetbac" <sweetbac@sbcglobal.net> wrote in message
> news:j9pfa6$rv1$1@dont-email.me...
> >
> > "iLL_Schizo" <caljamscott@yahoo.com> hallucinated in message
> >
> >> That is what you "heard."
> >
> > What I ALSO "heard" is that you're a schizophrenic.
> > Whats the name of your mental health case worker?
> > I think we need to do an intervention with you....
> > you have stage 3 cancer....you're mentally unbalanced
> > ...and obviously off your meds.
> > Why spend your last days sitting around your residency hotel
> > room watching scratchy VHS tapes of "George "The Animal" Steel
> > and Captain Lou Albano when you could do something productive....
> > like apologizing to the 624 internet chatgroups you harrass on
> > a daily basis...and to the librarians down at the Bronx public library
> > who spend most of they shifts trying to keep you out of the bathroom
> > when children are using it...they DO have books to re-shelf ya know.
> > We will collectively dance on your shallow, unmarked grave, Ree-Tod.
> >
>
> I don't think the 2 1/2 million are going to do that.

WE will ALL be fighting for the lead position PiGGy u DISEASE'd PERVERT