In PHP I can do this within a class's method:
Condition::evaluate($this, $primaryCondition);
This allows me to pass the entire instance of the class to a different class via $this. How can I achieve the same in Ruby?
In ruby you have a self
keyword. You can read about it in this article, it explains how self
behave in different contexts.
You can do this in Ruby of course.
Consider two classes:
class Foo
def test
puts 'We are in class: For'
end
end
class Bar
def initialize(your_object)
@your_object = your_object
end
def test(i = nil)
puts 'We are in class: Bar'
if @your_object
@your_object.test
end
end
end
foo = Foo.new
bar = Bar.new(foo)
bar.test
# We are in class: Bar
# We are in class: For
^^^^ You can see, you apply the method .test on the object stored in the foo variable.
The current object within a class definition can be addressed with the keyword "self".