+ 2
Need help with ruby
Hello everyone ,new at ruby,i need help If you have a class with just one method in it.then you create two objects/instances of that class.When i make a call to the method of the class how do i identify which of the two object made that call? Something you can do with the this keyword in js! https://code.sololearn.com/cvT3HPqfHns5/?ref=app
2 Answers
+ 5
Something like this?
class Call
def speak
puts "hello"
end
def speak_with_ID
puts "#{self} : hello"
end
end
Obj1 = Call.new
Obj2 = Call.new
Obj1.speak_with_ID
Obj2.speak_with_ID
Output:
# <Call: 0x000000027cd010>: hello
# <Call: 0x000000027ccef8>: hello
+ 4
I mean, the equivalent of this is self in Ruby. Although self has more context within the methods of a class.