Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3623412
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:22:49+00:00 2026-05-18T23:22:49+00:00

A recent convert to Ruby here. The following question isn’t really practical; it’s more

  • 0

A recent convert to Ruby here. The following question isn’t really practical; it’s more of a question on how the internals of Ruby works. Is it possible to override the standard addition operator to accept multiple inputs? I’m assuming that the answer is no, given that the addition operator is a standard one, but i wanted to make sure i wasn’t missing something.

Below is the code I wrote up quick to verify my thoughts. Note, it’s completely trivial/contrived.

class Point
    attr_accessor :x, :y

    def initialize(x,y)
        @x, @y = x, y
    end


    def +(x,y)
        @x += x
        @y += y
    end


    def to_s
        "(#{@x}, #{@y})"
    end
end

pt1 = Point.new(0,0)
pt1 + (1,1) # syntax error, unexpected ',', expecting ')'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:22:50+00:00Added an answer on May 18, 2026 at 11:22 pm

    You should not mutate the object when implementing + operator. Instead return a new Point Object:

    class Point
        attr_accessor :x, :y
    
        def initialize(x,y)
            @x, @y = x, y
        end
    
    
        def +(other)
          Point.new(@x + other.x, @y + other.y)
        end
    
    
        def to_s
            "(#{@x}, #{@y})"
        end
    end
    
    ruby-1.8.7-p302:
    > p1 = Point.new(1,2)
    => #<Point:0x10031f870 @y=2, @x=1> 
    > p2 = Point.new(3, 4)
    => #<Point:0x1001bb718 @y=4, @x=3> 
    > p1 + p2
    => #<Point:0x1001a44c8 @y=6, @x=4> 
    > p3 = p1 + p2
    => #<Point:0x1001911e8 @y=6, @x=4> 
    > p3
    => #<Point:0x1001911e8 @y=6, @x=4> 
    > p1 += p2
    => #<Point:0x1001877b0 @y=6, @x=4> 
    > p1
    => #<Point:0x1001877b0 @y=6, @x=4> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a recent question I asked about the fastest way to convert a large
Is it possible to convert the following into HQL 3.0.5 using any approach? Subqueries,
I'm a recent semi-convert to Eclipse after 20 years of using vi and gvim.
I'm a recent vim convert (from fancy IDEs like eclipse.) I love the :make
A recent question about string literals in .NET caught my eye. I know that
A recent question came up about using String.Format(). Part of my answer included a
A recent question about StyleCop alerted me to the use of tools to enforce
This recent question about sorting randomly using C# got me thinking about the way
Following recent hardware problems, I attempted to switch a couple of our websites to
As a recent convert to git and a long-time Emacs hacker I'm wondering what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.