I have a class:
class Foo
def self.test
#test
end
def foo
#test = 1
bar
end
private
def bar
#test = 2
end
end
object = Foo.new.foo
Foo.test
# => nil
The only way I could get it to output '2' is by making #test a class variable. Is there any other way around using the instance variable and being able to display it with Foo.test?
It's not really clear to me what you want to achieve, and why. Here's an example with a "class instance variable". It might be what you're looking for:
class Foo
class << self
attr_accessor :test
end
attr_accessor :test
def foo
#test = 1
bar
end
private
def bar
Foo.test = 2
end
end
foo = Foo.new
foo.foo
p foo.test
#=> 1
p Foo.test
#=> 2
Related
When inheriting a class, I want to save class variables to the inheriting class instead of the parent class... like so...
class Foo
def self.inherited klass
klass.title = klass.to_s
end
def self.title= title
##title = title
end
def self.title
##title
end
end
class Bar < Foo
end
class Baz < Foo
end
Bar.title # returns `Baz` instead of `Bar`
Baz.title
This is a contrived example because after playing around with it, I more want to just understand than anything.
You need to change the class variable to instance variable.
class Foo
def self.inherited klass
klass.title = klass.to_s
end
def self.title= title
#title = title
end
def self.title
#title
end
end
class Bar < Foo
end
class Baz < Foo
end
Bar.title # => "Bar"
Baz.title # => "Baz"
##title class variable is a shared variable. Baz, Bar and Foo all are using the same class variable ##title. When you did class Baz < Foo ;end, then ##title has been updated to the name of Baz, thus all call to the getter method #title is giving currently updated value of the variable #title.
Follow the code below :-
class Foo
def self.inherited klass
klass.title = klass.to_s
end
def self.title= title
##title = title
end
def self.title
##title
end
end
class Bar < Foo
end
# see the output here
Bar.title # => "Bar"
class Baz < Foo
end
# here the output changed, as ##title value is holding the currently updated
#value
Bar.title # => "Baz"
Baz.title # => "Baz"
You can do that using Module#class_variable_get and Module#class_variable_set:
class Foo
def self.title= title
class_variable_set(:##title, title)
end
def self.title
class_variable_get(:##title)
end
end
class Bar < Foo
end
class Baz < Foo
end
Bar.title = 'Bar'
Baz.title = 'Baz'
Bar.title #=> "Bar"
Bar.title = "Cat"
Baz.title #=> "Baz"
Bar.class_variables #=> [:##title]
Baz.class_variables #=> [:##title]
Foo.class_variables #=> []
Foo.methods(false) #=> [:title=, :title]
Bar.methods(false) #=> []
Baz.methods(false) #=> []
If we were to execute Bar's a getter before its setter, we'd get:
Bar.title #=> NameError: uninitialized class variable ##title in Bar
If desired, the class variables can be initialized (to nil, say) by adding the following to the class definition:
def self.inherited klass
klass.class_variable_set(:##title, nil)
end
Sorry for the newbie question but how do I pass/access attar_accessor to and from multiple classes? in my example below the Foo class never is able to see the updated values from the Level class.
class Level
attr_accessor :level, :speed
end
class Foo
attr_reader :a, :b
def initialize
#x = Level.new()
#a = #x.level
#b = #x.speed
end
def reload
#a = #x.level
#b = #x.speed
end
end
#testa = Foo.new()
#testb = Level.new()
#testb.level = 5
#testb.speed = 55
puts "Value for attr_accessor is:"
puts "#{#testb.level}"
puts "#{#testb.speed}"
puts "#{#testa.a}"
puts "#{#testa.b}"
#testa.reload
puts "#{#testa.a}"
puts "#{#testa.b}"
Instance of Level class you declaring in Foo's constructor and #testb are two different objects.
You might want to edit your Foo class this way:
class Foo
def initialize(level)
#x = level # very strange name for such a thing.
#a = #x.level
#b = #x.speed
end
# rest of the class body is the same as yours
# so it is omitted
end
And then do your tests:
level = Level.new() # BTW: no need of instance variables here.
foo = Foo.new(level) # good job. Your foo has "captured" the level.
# et cetera
Any Ruby guru that explain this?
class Bar
##x = 10
def self.test
return ##x
end
end
class Foo < Bar
##x = 20
end
puts Bar.test # 20 why not 10?
puts Foo.test # 20
When i run this from TextMate. I would expect that
puts Bar.test returns 10
and
puts Foo.test returns 20
But for some reason (that i would love to know) ##x in Foo updates Bar as-well, which is the super class. What is it i'm missing?
This is to be expected. Class variables are shared within the hierarchy. See section in Wikipedia: http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Variables_and_Constants#Class_Variables
Compare this to class instance variables, which are private to that class only.
class Bar
#x = 10
def self.test
return #x
end
end
class Foo < Bar
#x = 20
end
Bar.test # => 10
Foo.test # => 20
class Test
class << self
attr_accessor :some
def set_some
puts self.inspect
some = 'some_data'
end
def get_some
puts self.inspect
some
end
end
end
Test.set_some => Test
puts Test.get_some.inspect => Test nil
Here above I could find self as Test itself but not returning the some_data as output.
But while I modified in following way it returns expected output
class Test
class << self
attr_accessor :some
def set_some
puts self.inspect
self.some = 'some_data'
end
def get_some
puts self.inspect
self.some
end
end
end
Test.set_some => Test
puts Test.get_some.inspect => Test some_data
What is the differences?
EDIT
Now in the first example if I set as get some method as
Test.some = 'new_data'
puts Test.some.inspect #=> new_data
Test.set_some
puts Test.get_some.inspect => new_data
Now it made me much more confused.
some = :foo makes ruby think it should create a new local variable with name some. If you want to call some=(), you have to use an explicit reciever - as in self.some = :foo. I once lost a bet on that... :-/
It's (local) variable in the first example
In the first example some is a local variable.
In the second one, some is a method of self. Why? Because attr_accessor :some is the same as:
def some= (val)
#some = val
end
def some
return #some
end
So, you have created the getter and setter methods for the instance variable #some (it's an instance variable of the object Test, as every class is also an object of class Class).
in the first method
def set_some
puts self.inspect
some = 'some_data'
end
some is a local variable.. its not the same as #some that is a instance variable (in this case a class instance variable) so the value disappears when the method ends.
if you want to call the setter method some or set #some to something then do this
#some = 'some_data'
or
self.some = 'some_data'
in the second method
def get_some
puts self.inspect
self.some
end
your calling the method some. which returns the instace variable #some.. and since at this point #some has no value.. returns nil..
Example 1 with no method override and no local variable
class Foo
def initialize
#foo = 'foo'
end
def print_foo
print #foo
print self.foo
print foo
end
end
#foo, self.foo, and foo will access instance variable #foo within the instance method:
Foo.new.print_foo #=> foofoofoo
Example 2 with method override
class Foo
def initialize
#foo = 'foo'
end
def foo
return 'bar'
end
def print_foo
print #foo
print self.foo
print foo
end
end
#foo will access the instance variable, but self.foo and foo will call the foo override method:
Foo.new.print_foo #=> foobarbar
Example 3 with method override and local variable
class Foo
def initialize
#foo = 'foo'
end
def foo
return 'bar'
end
def print_foo
foo = 'baz'
print #foo
print self.foo
print foo
end
end
#foo accesses instance variable, self.foo accesses override method, and foo accesses local variable:
Foo.new.print_foo #=> foobarbaz
I need to make some instance methods private after registering that object in another object.
I don't want to freeze the object because it must remain editable, only with less functionality. And I don't want to undef the methods since they are used internally.
What I need is something like:
class MyClass
def my_method
puts "Hello"
end
end
a = MyClass.new
b = MyClass.new
a.my_method #=> "Hello"
a.private_instance_method(:my_method)
a.my_method #=> NoMethodError
b.my_method #=> "Hello"
Any ideas?
You can call method private on the method name anytime to make it private:
>> class A
>> def m
>> puts 'hello'
>> end
>> end
=> nil
>> a = A.new
=> #<A:0x527e90>
>> a.m
hello
=> nil
>> class A
>> private :m
>> end
=> A
>> a.m
NoMethodError: private method `m' called for #<A:0x527e90>
from (irb):227
from /usr/local/bin/irb19:12:in `<main>'
or, from outside the class:
A.send :private, :m
class A
def test
puts "test"
end
def test2
test
end
end
a = A.new
class << a
private :test
end
a.test2 # works
a.test # error: private method
What's public and what's private is per class. But each object can have its own class:
class Foo
private
def private_except_to_bar
puts "foo"
end
end
class Bar
def initialize(foo)
#foo = foo.dup
class << #foo
public :private_except_to_bar
end
#foo.private_except_to_bar
end
end
foo = Foo.new
Bar.new(foo) # => "foo"
foo.private_except_to_bar
# => private method `private_except_to_bar' called for #<Foo:0xb7b7e550> (NoMethodError)
But yuck. Consider these alternatives:
Just make the method public.
Explore alternative designs.