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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:47:12+00:00 2026-05-16T19:47:12+00:00

When using an idiom such as: def func(*args) # some code end What is

  • 0

When using an idiom such as:

def func(*args)
  # some code
end

What is the meaning of *args? Googling this specific question was pretty hard, and I couldn’t find anything.

It seems all the arguments actually appear in args[0] so I find myself writing defensive code such as:

my_var = args[0].delete(:var_name) if args[0]

But I’m sure there’s a better way I’m missing out on.

  • 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-16T19:47:12+00:00Added an answer on May 16, 2026 at 7:47 pm

    The * is the splat (or asterisk) operator. In the context of a method, it specifies a variable length argument list. In your case, all arguments passed to func will be putting into an array called args. You could also specify specific arguments before a variable-length argument like so:

    def func2(arg1, arg2, *other_args)
      # ...
    end
    

    Let’s say we call this method:

    func2(1, 2, 3, 4, 5)
    

    If you inspect arg1, arg2 and other_args within func2 now, you will get the following results:

    def func2(arg1, arg2, *other_args)
      p arg1.inspect       # => 1
      p arg2.inspect       # => 2
      p other_args.inspect # => [3, 4, 5]
    end
    

    In your case, you seem to be passing a hash as an argument to your func, in which case, args[0] will contain the hash, as you are observing.

    Resources:

    • Variable Length Argument List, Asterisk Operator
    • What is the * operator doing

    Update based on OP’s comments

    If you want to pass a Hash as an argument, you should not use the splat operator. Ruby lets you omit brackets, including those that specify a Hash (with a caveat, keep reading), in your method calls. Therefore:

    my_func arg1, arg2, :html_arg => value, :html_arg2 => value2
    

    is equivalent to

    my_func(arg1, arg2, {:html_arg => value, :html_arg2 => value2})
    

    When Ruby sees the => operator in your argument list, it knows to take the argument as a Hash, even without the explicit {...} notation (note that this only applies if the hash argument is the last one!).

    If you want to collect this hash, you don’t have to do anything special (though you probably will want to specify an empty hash as the default value in your method definition):

    def my_func(arg1, arg2, html_args = {})
      # ...
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've found myself using the following idiom lately in clojure code. (def *some-global-var* (ref
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
I've been using the idiom below for some time now. And it seems to
I've been making some objects using the pimpl idiom, but I'm not sure whether
I constantly find myself using this idiom in KO-based HTML templates: <!-- ko if:
This question pertains to pretty much all C-like curly bracket programming languages. I'm talking
I'm using the pimpl-idiom with std::unique_ptr : class window { window(const rectangle& rect); private:
Josh Bloch mentions using a local variable in double check idiom for possible performance
Using CI for the first time and i'm smashing my head with this seemingly
We are using Eclipselink for ORM, but we have a need for some more

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.