ragingsquirrel3
ragingsquirrel3

Reputation: 425

Custom + Method in Ruby String

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?

Upvotes: 3

Views: 174

Answers (1)

Jörg W Mittag
Jörg W Mittag

Reputation: 369428

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.

Upvotes: 4

Related Questions