3

I am trying to modify the Ruby String class, specifically the + method. I want it to return a specific string, such that

 "a" + "b" => "custom_result"

Here's what I have:

class String

  def +(a)
    "custom_result"
  end

end

When I try to require this file in irb, "custom_result" is in my prompt, and I can't execute anything. Any ideas?

2
  • What if you require this in another file, not IRB? Commented Feb 13, 2013 at 17:40
  • Just tried in rspec, and am getting expected result. Commented Feb 13, 2013 at 17:45

1 Answer 1

4

Well, IRb uses the String class both to print the results and to parse what you type into it. If you mess with that, IRb breaks, that's not really surprising.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.