Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can I reference an object from a method without passing it as a variable?
I am getting error:
NameError: undefined local variable or method ‘my_object’ for Myclass:Class
class B
attr_accessor :somethings
include Enumerable
def initialize(*values)
self.somethings = []
end
end
my_object = B.new({d:'d'})
class Myclass
def self.my_method
p my_object
end
end
Run: Myclass.my_method
What I do NOT want to do for all my methods is...
def self.my_method(my_object)
p my_object
end
I could also solve the issue with use from a global variable
$my_object = B.new({d:'d'})
However, I only really want to make the variable available to my methods in Myclass class.
I only really want to make the variable available to my methods in Myclass class.
Then you should define it as an instance variable in corresponding scope of Myclass. As it is now, my_object is a local variable and is not visible because of the scope gate.
class Myclass
#my_object = B.new({d:'d'})
def self.my_method
p #my_object
end
end
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Why in ruby, when you initiazlize a class do you set the instance variable equal to a variable of the same name?
def initialize(number)
#number = number
end
We do that so that newly-created object (not a class! with initialize and #vars, you initialize the object that was just created with new() method!) remembers the value of number.
Try using this one:
def initialize(number)
end
This gets a number, but does nothing with it. When this inializer ends, the object created will not remember what was the 'number'.
Here:
def initialize(number)
#foo = 5
#bar = number
end
the newly-created object will remember a 5 in #foo and the number in #bar.
The idea to name the #variable just like the parameter is just to make it easier. In the example above, it's hard to guess what the bar is about. Instead, if I rename the #bar into #number, it wil be obvious that it holds .. the number.
def initialize(number) def initialize(number)
#bar = number <-same thing-> #number = number
end just different name end
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I created a Test class which has an instance variable #val and which is defined inline.
class Test
#value = 10
def display
puts #value
end
end
t = Test.new
t.display
This gives no output. But if I set #val through initialize method then the code works.
#val = 10 ( which you wrote in the scope of the class Test) creates an instance variable for your class Test. Where as initialize creates an instance variable for the instances of the class Test.
Look below, for comfirming :-
class Test
#x = 10
def initialize
#x = 12
end
end
Test.instance_variable_get(:#x) # => 10
Test.new.instance_variable_get(:#x) # => 12
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it possible in Ruby to define such setters and getters for class that can be used with [] or () or smth alike? E.g.
word.meaning[:english] = "ruby"
puts word.meaning[:german] # "Rubin"
Note that word.meaning must not be a hash! and :english, :german are kind of additional parameters for setter/getter meaning.
Yes, it can be done. You need to define a []= and [] methods.
In this example, I am using a Hash as the internal data structure - you are free to use whatever you like.
class Word
attr_reader :meaning
def initialize
#meaning = Meaning.new
end
end
class Meaning
attr_reader :h
def initialize
#h = {}
end
def []=(key, value)
#h[key] = value
end
def [](key)
#h[key]
end
end
Example:
word = Word.new
word.meaning[:english] = 'Hello'
word.meaning[:english] # => 'Hello'
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm new to ruby and I was wondering the following, can I call a method in the same class such as:
class myClass
$myVar = inClass
def inClass
#value = (gets data from db)
end
end
If there is a way to do this, can someone please help me?
You can do as below
class MyClass
def in_class
#value = (gets data from db)
end
def call_inclass
$my_var = in_class # don't use global variable.
end
end
Create a new instance method call_inclass and call the required class in_class from there. Your one will not work, as in_class is an instance method, and within class.. end body self is set to that class. Thus $myVar = in_class will simply thorw an error as no method error. Because $myVar = in_class it will be interpreted implicitly as $myVar = self.in_class, where self is MyClass. in_class is not a method of MyClass, rather an instance method of instances of MyClass.
Thus wrapped the method call to in_class inside another instance method, like here call_inclass.
Another way is make in_class as a singleton method of MyClass. Then it will work.
class MyClass
# again don't use global variable, rather use
# class instance variable, #my_var
$my_var = in_class
def self.in_class
#value = (gets data from db)
end
end
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
If I am editing the Array class, shouldn't I have to define each method with a self (e.g. self.sum). I'm not sure why this passes the rpsec tests for the 'Test-First' Ruby track without the self.method immediately following the def.
class Array
def sum
count = 0
self.each {|x| count += x}
count
end
def square
self.map {|x| x * x}
end
def square!
self.map! {|x| x * x}
end
end
These are "instance methods" - they operate on an instance of an Array, not the Array class itself. If you were to put self. before the name of each method when defining it, you would be defining a "class method", which wouldn't make any sense for these methods.
Although not necessary, the reason that self. works when invoking these methods from within the body of another one of the methods is that self is defined to be the "instance" at that point. This contrasts to when you're defining the methods with def, where self is the Array class.