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)
instance and returns it)
module Platform
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:
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:
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?
Is there an analogous way to ‘initialize’ to write initialization of
class variables?
class C
Ok, thank you, that cleared things. I have two follow up questions:
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
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.
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?
class C
Ok, thank you, that cleared things. I have two follow up questions:
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?
Is there an analogous way to ‘initialize’ to write initialization of
class variables?
/ http://www.ruby-forum.com/.
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.