[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

[NEWBIE] Help with class instance variables

Ittay Dror

7/14/2008 11:05:00 AM

Hello,

The code below prints:
#<Gem::Platform:0x..fdbdf0696 @cpu="x86", @os="linux", @version=nil>
--

that is, the @arch, @os and @classifier are empty.

(if i set these variables in the id method, they work fine)
(Gem::Platform.local is a class method that creates a new Gem::Platform
instance and returns it)

thank you for your help,
ittay

code below:

require 'rubygems'
require 'rubygems/version'
require 'rubygems/platform'

module Platform
class << self
attr_reader :arch
attr_reader :os
attr_reader :classifier
@arch = Gem::Platform.local.cpu
@os = Gem::Platform.local.os
@classifier = Gem::Platform.local.version

def id
"#{@arch}-#{@os}-#{@classifier}"
end
end
end

puts Gem::Platform.local.inspect
puts Platform.id
--
Posted via http://www.ruby-....

7 Answers

David A. Black

7/14/2008 11:17:00 AM

0

Hi --

On Mon, 14 Jul 2008, Ittay Dror wrote:

> Hello,
>
> The code below prints:
> #<Gem::Platform:0x..fdbdf0696 @cpu="x86", @os="linux", @version=nil>
> --
>
> that is, the @arch, @os and @classifier are empty.
>
> (if i set these variables in the id method, they work fine)
> (Gem::Platform.local is a class method that creates a new Gem::Platform
> instance and returns it)
>
> thank you for your help,
> ittay
>
> code below:
>
> require 'rubygems'
> require 'rubygems/version'
> require 'rubygems/platform'
>
> module Platform
> class << self
> attr_reader :arch
> attr_reader :os
> attr_reader :classifier
> @arch = Gem::Platform.local.cpu
> @os = Gem::Platform.local.os
> @classifier = Gem::Platform.local.version
>
> def id
> "#{@arch}-#{@os}-#{@classifier}"
> end
> end
> end
>
> puts Gem::Platform.local.inspect
> puts Platform.id

The problem is that the instance variables you're initializing belong
to the singleton class of Platform (class << Platform), not Platform.
Compare with this:

class C
class << self
attr_accessor :x
end

@x = 1 # or self.x = 1
end

puts C.x # 1


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails July 21-24 Edison, NJ
Advancing With Rails August 18-21 Edison, NJ
See http://www.r... for details and updates!

Ittay Dror

7/14/2008 11:30:00 AM

0

Hi,

David A. Black wrote:
> Hi --
>
>
> The problem is that the instance variables you're initializing belong
> to the singleton class of Platform (class << Platform), not Platform.
> Compare with this:
>
> class C
> class << self
> attr_accessor :x
> end
>
> @x = 1 # or self.x = 1
> end
>
> puts C.x # 1
>

Ok, thank you, that cleared things. I have two follow up questions:
1. If I want to add an instance variable to an object of class C I can
only do that in a method (not as part of the class definition)? In the
example above, how can I change it so that C.new will return an instance
of C where '@x' of the instance is 1?
2. Is there an analogous way to 'initialize' to write initialization of
class variables?

Thank you,
Ittay
>
> David
--
Posted via http://www.ruby-....

Rick DeNatale

7/14/2008 11:42:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

On Mon, Jul 14, 2008 at 7:30 AM, Ittay Dror <ittay.dror@gmail.com> wrote:

> Hi,
>
> David A. Black wrote:
> > Hi --
> >
> >
> > The problem is that the instance variables you're initializing belong
> > to the singleton class of Platform (class << Platform), not Platform.
> > Compare with this:
> >
> > class C
> > class << self
> > attr_accessor :x
> > end
> >
> > @x = 1 # or self.x = 1
> > end
> >
> > puts C.x # 1
> >
>
> Ok, thank you, that cleared things. I have two follow up questions:
> 1. If I want to add an instance variable to an object of class C I can
> only do that in a method (not as part of the class definition)? In the
> example above, how can I change it so that C.new will return an instance
> of C where '@x' of the instance is 1?


class C
def initialize
@x = 1
end
end

Perhaps this might help.
http://talklikeaduck.denh...articles/2008/02/08/whose-variable-is...



>
> 2. Is there an analogous way to 'initialize' to write initialization of
> class variables?
> / <http://www.ruby-foru....
>

You haven't used an class variables on this thread. Your initial code
created class instance variables, i.e. instance variables belonging to the
object which represents the class. Class variables in Ruby use a double @
prefix, are visible/shared between the class, any subclasses, and instances
of the same, have some interesting quirks in their semantics and are avoided
or at least used sparingly by many experienced Rubyists.


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

David A. Black

7/14/2008 11:44:00 AM

0

Hi --

On Mon, 14 Jul 2008, Ittay Dror wrote:

> Hi,
>
> David A. Black wrote:
>> Hi --
>>
>>
>> The problem is that the instance variables you're initializing belong
>> to the singleton class of Platform (class << Platform), not Platform.
>> Compare with this:
>>
>> class C
>> class << self
>> attr_accessor :x
>> end
>>
>> @x = 1 # or self.x = 1
>> end
>>
>> puts C.x # 1
>>
>
> Ok, thank you, that cleared things. I have two follow up questions:
> 1. If I want to add an instance variable to an object of class C I can
> only do that in a method (not as part of the class definition)? In the
> example above, how can I change it so that C.new will return an instance
> of C where '@x' of the instance is 1?

class C
def initialize
@x = 1
end
end

> 2. Is there an analogous way to 'initialize' to write initialization of
> class variables?

Change @x to @@x. Keep in mind, though, that in spite of the similar
appearance, class variables are completely different from instance
variables in just about every possible respect.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails July 21-24 Edison, NJ
Advancing With Rails August 18-21 Edison, NJ
See http://www.r... for details and updates!

Ittay Dror

7/14/2008 11:58:00 AM

0

Hi,

Rick Denatale wrote:

> You haven't used an class variables on this thread. Your initial code
> created class instance variables, i.e. instance variables belonging to
> the
> object which represents the class. Class variables in Ruby use a double
> @
> prefix, are visible/shared between the class, any subclasses, and
> instances
> of the same, have some interesting quirks in their semantics and are
> avoided
> or at least used sparingly by many experienced Rubyists.
>
>

Let me rephrase, trying to to use any terminology. Let's say I have a
class Person whose instances have variables like name and age. Now, the
Person class also has a cache for all persons, loaded from a file. So
this is a single variable shared by all instances and is initialized
once. How do I initialize it in Ruby?
--
Posted via http://www.ruby-....

Rick DeNatale

7/14/2008 2:47:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Mon, Jul 14, 2008 at 7:58 AM, Ittay Dror <ittay.dror@gmail.com> wrote:

> Hi,
>
> Rick Denatale wrote:
>
> > You haven't used an class variables on this thread. Your initial code
> > created class instance variables, i.e. instance variables belonging to
> > the
> > object which represents the class. Class variables in Ruby use a double
> > @
> > prefix, are visible/shared between the class, any subclasses, and
> > instances
> > of the same, have some interesting quirks in their semantics and are
> > avoided
> > or at least used sparingly by many experienced Rubyists.
> >
> >
>
> Let me rephrase, trying to to use any terminology. Let's say I have a
> class Person whose instances have variables like name and age. Now, the
> Person class also has a cache for all persons, loaded from a file. So
> this is a single variable shared by all instances and is initialized
> once. How do I initialize it in Ruby?
>

Well, it depends on just what you are doing with the cache.

class Person

def self.setup_cache
@cache = #... whatever you do to initialize the cache
end

setup_cache

# Now assuming we've got some kind of key to access the cache we probably
want a class method to get
# cached people

def self.person(cache_key)
@cache[cache_key]
end
end
--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Ittay Dror

7/14/2008 2:53:00 PM

0

Thank you very much!

Rick Denatale wrote:

>
> Well, it depends on just what you are doing with the cache.
[SNIP]
--
Posted via http://www.ruby-....