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

  • Home
  • SEARCH
  • 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 8433801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:29:14+00:00 2026-06-10T06:29:14+00:00

I have a method with a lengthy list of optional arguments, such as: def

  • 0

I have a method with a lengthy list of optional arguments, such as:

def foo(foo = nil, bar = nil, baz = nil, qux = nil)
    # no-op
end

I thought that calling the method and passing a split hash as a parameter would map the hash items to parameters by matching the key with the method parameter:

params = { bar: 'bar', foo: 'foo' }
foo(*params)

Unfortunately, when I examine the local variables after calling the method with a split hash, I get exactly what I’d expect if I passed in a split array, but it’s not what I was hoping for:

foo == [:bar, 'bar'] # hoped: foo == 'foo'
bar == [:foo, 'foo'] # hoped: bar == 'bar'

What am I lacking here?

  • 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-06-10T06:29:15+00:00Added an answer on June 10, 2026 at 6:29 am

    (this answer refers to the version of Ruby that was current when the question was asked. See edit for the situation today.)

    Ruby does not support passing arguments by name. The splat operator (*) expands an arbitrary enumerable by calling to_ary on it and splices the result into the argument list. In your case, the enumerable you pass in is a hash, which gets transformed into an array of key-value pairs:

    [2] pry(main)> params.to_a
    => [[:bar, "bar"], [:foo, "foo"]]
    

    So the first two arguments of the function will be the values [:bar, "bar"] and [:foo, "foo"] (regardless of their parameter name!).

    If you want to have something similar to keyword arguments in Ruby, you can make use of the fact that you don’t need the braces when passing a hash to a function as the last argument:

    def foo(opts = {})
        bar = opts[:bar] || <default>
        foo = opts[:foo] || <default>
        # or with a lot of parameters:
        opts = { :bar => <default>, :foo => <default>, ... }.merge(opts)
    end
    
    foo(foo: 3)  # equivalent to foo({ foo: 3 })
    

    EDIT:

    As of version 2.0, Ruby now supports named arguments with a dedicated syntax. Thanks to user jgoyon for pointing that out.

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

Sidebar

Related Questions

I'm new to Python. I have a method that begins: def foo(self, list): length
I have this method private def getAddresses(data: List[Int], count: Int, len: Int): Tuple2[List[Address], List[Int]]
I have a method which in essence converts a datatable to a list of
I have a method to compare two list of objects. The objects are unique
Suppose I have a method that returns an array or list or some other
I have a method that has a reference to a linked list and a
I have method to get list of bean properities like below. How to check
I have a method which looks like this public void setDayIntervals(Days day, List<HourRange> ranges)
I have an iterator with a __len__ method defined. Questions: If you call list(y)
I have a method that will execute a query with a list of QueryParameters

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.