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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:26:26+00:00 2026-05-26T22:26:26+00:00

In Ruby, a function can return multiple* values. Is it possible for a Ruby

  • 0

In Ruby, a function can return multiple* values. Is it possible for a Ruby function to determine how many return values its invoking code is expecting?

For instance:

a = f()                  # caller expects one return value
a, b = f()               # caller expects two return values
a, b, c = f()            # caller expects three return values

If we let r be the number of expected return values, is it possible to write a function f such that it can find r?

Specifically, how could we change the definition of rcount, below, such that is passes the following tests:

a, b = rcount()
puts "#{a},#{b}"           # FAIL, DESIRED: "2,1"      ACTUAL: "1,"
a, b, c = rcount()
puts "#{a},#{b},#{c}"      # FAIL, DESIRED: "3,2,1"    ACTUAL: "1,,"
a, b, c, d = rcount()
puts "#{a},#{b},#{c},#{d}" # FAIL, DESIRED: "4,3,2,1"  ACTUAL: "1,,,"

Where rcount is defined like this:

def rcount()
  ret = []
  r = nil                  # <== Q. IS IT POSSIBLE TO GET r PROGRAMMATICALLY?
  r ||= 1
  last = r - 1
  (0..last).each do |i|
    ret[i] = r - i
  end
  ret
end

*Actually multiple return values is syntactic sugar, the real return value is a single array. All Ruby functions return exactly one value.

  • 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-26T22:26:27+00:00Added an answer on May 26, 2026 at 10:26 pm

    No. The Ruby environment doesn’t give you that information. You could use an exception and ParseTree to find out what the calling code looks like, but that is overkill 🙂

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

Sidebar

Related Questions

is there any way I can reproduce this ruby function: def Password.hash(password,salt) Digest::SHA512.hexdigest(#{password}:#{salt}) end
In Ruby, how can I programmatically determine which class/module defines a method being called?
I have implemented Ruby C extension(i.e. Invoking C function from ruby script) Following is
How do I create a Ruby function that does not have an explicit number
I need to write a little ruby function that does word wrapping. I have
I'm going crazy: Where is the Ruby function for factorial? No, I don't need
I want to change the default arguments passed to a Ruby function. For example,
I need some examples of implementing curry function in ruby(1.8.6 or 1.8.7 not 1.9).
Is there any Ruby equivalent for Python's builtin zip function? If not, what is
I was wondering how to access a global function fn in ruby from a

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.