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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:13:13+00:00 2026-05-11T11:13:13+00:00

I have a method like this: def foo(fruit=’apple’, cut=sliced, topping=ice cream) # some logic

  • 0

I have a method like this:

def foo(fruit='apple', cut='sliced', topping='ice cream')   # some logic here end 

How can I call it where I only override the topping parameter but use the default values for the others, something like this

foo('','','hot fudge') 

Of course this does not work as intended, but I want to only provide a value for the third optional parameter, and have the first two stick with their default values. I know how to do this with a hash, but is their a shortcut way to do it, using the above syntax?

  • 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. 2026-05-11T11:13:14+00:00Added an answer on May 11, 2026 at 11:13 am

    You can’t use this syntax to do this in ruby. I would recommend the hash syntax for this.

    def foo(args={})   args[:fruit]    ||= 'apple'   args[:cut]      ||= 'sliced'   args[:topping]  ||= 'ice cream'   # some logic here end  foo(:topping => 'hot fudge') 

    You could also do this using positional arguments:

    def foo(fruit=nil,cut=nil,topping=nil)   fruit    ||= 'apple'   cut      ||= 'sliced'   topping  ||= 'ice cream'   # some logic here end  foo(nil,nil,'hot fudge') 

    Bear in mind that both of these techniques prevent you from passing actual nil arguments to functions (when you might want to sometimes)

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

Sidebar

Related Questions

Say, I have an XML String like this, <METHOD>foo</METHOD> <PARAM1>abc</PARAM1> <PARAM2>def</PARAM2> ... <PARAM99>ghi</PARAM99> <PARAM100>jkl</PARAM100>
Let's say that I have a method that looks like this: def raise_if_client_status_error(xml_resp) #
I have a controller method that is a little something like this: def suggestions
I have methods in all of my models that look like this: def formatted_start_date
If I have a method like this: public void DoSomething(int Count, string[] Lines) {
In a multithreaded scenario, I have a method like this: bool WaitForChange( time_duration WaitTime
I have wriiten a method like this public ArrayList<T> GetDoctorDetail(String name) { if (name!=null
I have a method that looks like this: public static String escape(String text) {
I have a javascript method looks like this JSMethod(JS_para_1,JS_para_2) { ...... ,,,,, } and
I have a method on my generated partial class like this: var pChildren =

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.