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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:50:02+00:00 2026-05-31T00:50:02+00:00

This is so simple that I can’t believe it caught me. def meth(id, options

  • 0

This is so simple that I can’t believe it caught me.

def meth(id, options = "options", scope = "scope")
  puts options
end

meth(1, scope = "meh")

-> "meh"

I tend to use hashes for argument options just because it was how the herd did it– and it is quite clean. I thought it was the standard. Today, after about 3 hours of bug hunting, I traced down an error to this gem I happen to be using that assumes named parameters will be honored. They are not.

So, my question is this: Are named parameter officially not honored in Ruby (1.9.3), or is this a side effect of something I’m missing? If they are not, why not?

  • 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-31T00:50:03+00:00Added an answer on May 31, 2026 at 12:50 am

    What’s actually happening:

    # Assign a value of "meh" to scope, which is OUTSIDE meth and equivalent to
    #   scope = "meth"
    #   meth(1, scope)
    meth(1, scope = "meh")
    
    # Ruby takes the return value of assignment to scope, which is "meh"
    # If you were to run `puts scope` at this point you would get "meh"
    meth(1, "meh")
    
    # id = 1, options = "meh", scope = "scope"
    puts options
    
    # => "meh"
    

    There is no support* for named parameters (see below for 2.0 update). What you’re seeing is just the result of assigning "meh" to scope being passed as the options value in meth. The value of that assignment, of course, is "meh".

    There are several ways of doing it:

    def meth(id, opts = {})
      # Method 1
      options = opts[:options] || "options"
      scope   = opts[:scope]   || "scope"
    
      # Method 2
      opts = { :options => "options", :scope => "scope" }.merge(opts)
    
      # Method 3, for setting instance variables
      opts.each do |key, value|
        instance_variable_set "@#{key}", value
        # or, if you have setter methods
        send "#{key}=", value
      end
      @options ||= "options"
      @scope   ||= "scope"
    end
    
    # Then you can call it with either of these:
    meth 1, :scope => "meh"
    meth 1, scope: "meh"
    

    And so on. They’re all workarounds, though, for the lack of named parameters.


    Edit (February 15, 2013):

    * Well, at least until the upcoming Ruby 2.0, which supports keyword arguments! As of this writing it’s on release candidate 2, the last before the official release. Although you’ll need to know the methods above to work with 1.8.7, 1.9.3, etc., those able to work with newer versions now have the following option:

    def meth(id, options: "options", scope: "scope")
      puts options
    end
    
    meth 1, scope: "meh"
    # => "options"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone tell me in simple words that what this L value is and
Can I get a complete simple scenario i.e. tutorial that suggest how this should
This is a simple question that can be answered fast by someone who's more
This sound so simple that I can't figure out why I can't find the
I am surprised that I can't find the answer to this simple task. I
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
I am a total beginner in JS/XML. I have this simple code that needs
I have this very simple example that I am using to learn structs in
This simple code is not producing any sound on a couple of machines that
This is a simple code that open and print the file content. #include <stdio.h>

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.