How I can do something like this:
class Some < String
def m1(a, b)
self = a + b
end
end
s = Some.new("hello")
s.m1("one ", "two")
p s # => "one two"
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That depends on how exactly you define “something like”.
If you want to make it so that all variables that point to the given
Someobject, now instead point to the string that is the result ofa + b, that’s not possible.If you want to change the string contents of the given
Someobject, you can use thereplacemethod, i.e.replace(a+b).To illustrate the difference between using
replaceand reassignment:The latter behavior is not achievable using a method.