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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:54:18+00:00 2026-05-27T19:54:18+00:00

I have a method with an optional argument. How can I decide whether the

  • 0

I have a method with an optional argument. How can I decide whether the Argument was given or not?

I came up with the following solutions. I am asking this question since I am not entirely satisfied with any of them. Exists there a better one?

nil as default value

def m(a= nil)
    if a.nil?
        ...
    end
end

The drawback with this one is, that it cannot be decided whether no argument or nil was given.

custom NoArgument as default value

class NoArgument
end

def m(a= NoArgument.new)
    if NoArgument === a
        ...
    end
end

Whether nil was given can be decided, but the same problem exists for instances of NoArgument.

Evaluating the size of an ellipsis

def m(*a)
    raise ArgumentError if m.size > 1
    if m.size == 1
        ...
    end
end

In this variant it can be always decided whether the optional argument was given.
However the Proc#arity of this method has changed from 1 to -1 (not true, see the comment). It still has the disadvantage of beeing worse to document and needing to manually raise the ArgumentError.

  • 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-27T19:54:19+00:00Added an answer on May 27, 2026 at 7:54 pm

    Jorg W Mittag has the following code snippet that can do what you want:

    def foo(bar = (bar_set = true; :baz))
      if bar_set
        # optional argument was supplied
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a feeling that this question is very simple, but I just can't
I have an action method that can take an optional id parameter. public ActionResult
In C++ methods can have optional arguments, like this: void myFunction (int arg1, int
I have class method that returns a list of employees that I can iterate
I have a method that can return either a single object or a collection
I have this method on a webpart: private IFilterData _filterData = null; [ConnectionConsumer(Filter Data
I have this method: public bool CanExecute() And after 70 commits, I added an
I have an designated initializer with optional arguments (similar to the following code), and
I have a class with a private static method with an optional parameter. How
This is, probably, a very simple answer for someone. I have a method with

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.