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 803281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:44:04+00:00 2026-05-14T23:44:04+00:00

In Ruby, it seems that a lot of coerce() help can be done by

  • 0

In Ruby, it seems that a lot of coerce() help can be done by

def coerce(something)
  [self, something]
end

that’s is, when

3 + rational

is needed, Fixnum 3 doesn’t know how to handle adding a Rational, so it asks Rational#coerce for help by calling rational.coerce(3), and this coerce instance method will tell the caller:

# I know how to handle rational + something, so I will return you the following:
[self, something]
# so that now you can invoke + on me, and I will deal with Fixnum to get an answer

So what if most operators can use this method, but not when it is (a – b) != (b – a) situation? Can coerce() know which operator it is, and just handle those special cases, while just using the simple [self, something] to handle all the other cases where (a op b) == (b op a) ? (op is the operator).

  • 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-14T23:44:05+00:00Added an answer on May 14, 2026 at 11:44 pm

    The point of coerce is not to know what operation you are trying to perform. Its purpose is to bring the argument and self to a common ground. Additionally, same operators can be commutative in certain classes, and not in other (Numeric#+ and Array#+, for example), so your small commutativity-based coerce exploit really won’t pay off.

    Instead of pushing your coerce to do what it’s not intended to, you should create a new class instead (such as ScalarPoint, for example), and use it to interface scalar values with your Point:

    class ScalarPoint
      attr_reader :val
    
      def initialize(val)
        @val = val
      end
    
      def +(arg)
        case arg
        when Point:
          Point.new(@val + arg.x, @val + arg.y)
        when ScalarPoint:
          ScalarPoint.new(arg.val + @val)
        else
          raise "Unknown operand!"
        end
      end
    
      # define other operators, such as * etc
    
      def coerce(arg)
        return [ScalarPoint.new(arg), self] if arg.is_a?(Numeric)
        raise "Can't handle"
      end
    end
    

    and

    class Point
      def coerce(arg)
        [ScalarPoint.new(arg), self] if arg.is_a?(Numeric)
        raise "Can't handle"
      end
    end
    

    etc. (NB: code not tested)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've read that Ruby 1.9 is a lot faster than 1.8, however it seems
It seems that a lot of Ruby on Rails questions are related to AuthLogic.
In every benchmark that I found on the web it seems that Ruby is
I tried to install Ruby 1.9.3 using RVM. It seems that it installed without
does FastCGI work well with PHP? It seems that some people running Ruby on
I have done a lot of rails and ruby work in the past (using
I have seen this in a lot of code written using Ruby-on-Rails. It seems
It seems that ActiveRecord and Ruby's Time object doesn't play nice with times that
It seems to me like Ruby has a lot of syntactic flexibility, and many
I'm working with a Ruby API that takes a series of boolean switches, something

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.