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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:15:50+00:00 2026-05-13T21:15:50+00:00

I seem to not be able to do this (which I used to be

  • 0

I seem to not be able to do this (which I used to be able to do in Python). Let me explain ..

Suppose I have the following method in Ruby:

def someMethod(arg1=1,arg2=2,arg3=3)
...
...
...
end

Now to call this method I could do

someMethod(2,3,4)
someMethod(2,3)
someMethod(2)

and the arguments are taken by their respective order.. but what if I want to give arg2 at some point in my programming and want the default values for arg1 and arg3?

I tried writing someMethod(arg2=4) but this doesn’t seem to work in Ruby 1.9. What it does is it still thinks that arg1 is 4. In python I could at least get away with this, but in ruby I am not sure. Does anyone have any elegant ideas?

  • 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-13T21:15:50+00:00Added an answer on May 13, 2026 at 9:15 pm

    Only elegant solution for now is to use hash as parameters list:

    def some_method(options = {})
      defaults = {:arg1 => 1, :arg2 => 2, :arg3 => 3}
      options = defaults.merge(options)
      ...
    end
    

    and later in code use implicit hash when calling your method:

    some_method()
    some_method(:arg1 => 2, :arg2 => 3, :arg3 => 4)
    some_method(:arg2 => 4)
    

    In Ruby 1.9 you can use new hash keys syntax to get ir more like in Python:

    some_method(arg1: 2, arg2: 3, arg3: 4)
    

    If you want simpler syntax and still be able to use positional parameters, then I would suggest to play with something like this:

    def some_method(*args)
      defaults = {:arg1 => 1, :arg2 => 2, :arg3 => 3}
      options = args.last.is_a?(::Hash) ? args.pop : {}
      options = defaults.merge(options)
    
      arg1 = args[0] || options[:arg1]
      arg2 = args[1] || options[:arg2]
      arg3 = args[2] || options[:arg3]
      ...
    end
    
    some_method()
    some_method(2)
    some_method(3,4,5)
    some_method(arg2: 5)
    some_method(2, arg3: 10)
    

    If you would like to mimic Ruby arguments number check for method, you can add:

    fail "Unknown parameter name(s) " + (options.keys - defaults.keys).join(", ") + "." unless options.length == defaults.length
    

    EDIT:
    I updated my answer with Jonas Elfström‘s comment

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

Sidebar

Related Questions

I have a problem, which I do not seem to be able to solve...
This question might not seem programming related at first, but let me explain. I'm
This code does not seem to compile, I just need to write something to
I tried this but it does not seem to be valid syntax. <xsl:element name=$myElementName></xsl:element>
CLR profiler does not seem to work with the Silverlight CLR. Does another memory
Excel macros do not seem to allow the use of undo after running them.
The Visual Studio compiler does not seem to warn on signed/unsigned assignments, only on
A using declaration does not seem to work with an enum type: class Sample{
Seems that requirements on safety do not seem to like systems that use AI
In one of our application im getting an exception that i can not seem

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.