Forum: Ruby Class variable confusion

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1b7fef03e1f15f2cfd76eee5b0830d3f?d=identicon&s=25 Keith Salisbury (ktec)
on 2009-04-23 15:54
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
1b7fef03e1f15f2cfd76eee5b0830d3f?d=identicon&s=25 Keith Salisbury (ktec)
on 2009-04-23 16:00
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
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 Jesús Gabriel y Galán (Guest)
on 2009-04-23 16:23
(Received via mailing list)
On Thu, Apr 23, 2009 at 4:01 PM, Keith Salisbury
<keithsalisbury@gmail.com> wrote:
>  end
>  def self.speak
>    puts greeting
>  end
>  def self.learn_french
>    learn_language(greeting, "bonjour")

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

>  end
>  def self.learn_language(prop, value)
>    prop = 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 ||= "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)
   instance_variable_set(prop, value)
 end
end

Keith.speak
Keith.learn_french
Keith.speak


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

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

Hope this helps,

Jesus.
1b7fef03e1f15f2cfd76eee5b0830d3f?d=identicon&s=25 Keith Salisbury (ktec)
on 2009-04-23 16:38
Jesús Gabriel y Galán wrote:
> Hope this helps,
>
> Jesus.

Legend

Thankyou!!
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-04-23 16:57
(Received via mailing list)
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
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2009-04-23 19:05
(Received via mailing list)
On Apr 23, 2009, at 10:01 AM, Keith Salisbury wrote:

>    @greeting = value
> end
>  end
>  def self.learn_language(prop, value)
>
>  end
>  def self.learn_french
>    self.greeting = "bonjour"
>  end
> end
>
> Keith.speak
> Keith.learn_french
> Keith.speak
> --
> Posted via http://www.ruby-forum.com/.


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://agileconsultingllc.com
Rob@AgileConsultingLLC.com
This topic is locked and can not be replied to.