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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:51:25+00:00 2026-05-30T04:51:25+00:00

Given a certain object that respond_to? :+ I would like to know what it’s

  • 0

Given a certain object that respond_to? :+ I would like to know what it’s the identity element for that operation on that object. For example, if a is Fixnum then it should give 0 for operation :+ because a + 0 == a for any Fixnum. Of course I already know the identity element for :+ and :* when talking about Fixnums, but is there any standard pattern/idiom to obtain those dynamically for all Numeric types and operations?.

More specifically I have write some code (see below) to calculate shortest path between v1 and v2 (vertexes in a graph) where the cost/distance/weigh of each edge in the graph is given in a user-specified type. In the current implementation the cost/weight of the edges could be a Fixnum, a Float or anything that implements Comparable and can add 0 to itself and return self.

But I was wondering what is the best pattern:

  • requiring that type used must support a + 0 == a
  • requiring that type provide some kind of addition identity element discovery ‘a.class::ADDITION_IDENTITY_ELEMENT
  • ??

My Dijkstra algorithm implementation

  def s_path(v1,v2)
    dist = Hash.new { nil}
    pred = {}
    dist[v1] = 0 # distance from v1 to v1 is zero
    #pq = nodes 
    pq = [v1]

    while u = pq.shift
      for edge in from(u)
        u,v,cost = *edge
        new_dist = cost + dist[u]  
        if dist[v].nil? or new_dist < dist[v]
          dist[v] = new_dist
          pred[v] = u
          pq << v
        end
      end
    end
    path = [v2]
    path << pred[path.last] while pred[path.last]
    path.reverse
  end
  • 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-30T04:51:26+00:00Added an answer on May 30, 2026 at 4:51 am

    I think the a.class::ADDITION_IDENTITY_ELEMENT is pretty good except I would call it a.class::Zero.

    Another option would be to do (a-a).

    Personally I wouldn’t try to make things so abstract and I would just require that every distance be a Numeric (e.g. Float or Integer). Then you can just keep using 0.

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

Sidebar

Related Questions

Given a class that is semantically supposed to be an object of a certain
Given an object graph that looks something like the following; A --> B[] -->
Given an absolute or relative path (in a Unix-like system), I would like to
Given a method that accepts as a parameter a certain supertype. Is there any
I have a certain object type that is stored in a database. This type
I have a set of png images that I would like to process with
Given a table where the first column is seconds past a certain reference point
Given a filename, I need to be able to access certain metadata in an
If I have a certain week number (eg 51) and a given year (eg
if i have a function A,which can apply a certain rule on a given

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.