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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:05:55+00:00 2026-05-27T10:05:55+00:00

We can create, for example, new array following way: values = Array.new(5) { rand(6)

  • 0

We can create, for example, new array following way:

values = Array.new(5) { rand(6) + 1 } # [3, 2, 3, 3, 2]

But how does it work in terms of function arguments?

For example if I define some function:

def some_func(a, b, c)
end

Function is defined with 3 arguments, I can make some of them optional but either way to call this function I would use

some_func(value1)
some_func(value1, value2)
some_func(value1, value2, value3)

But it doesn’t make sense to me in case of Array, where this block to define values go?

5 is definitely argument to a new function but what about block?

Thank you.

  • 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-27T10:05:56+00:00Added an answer on May 27, 2026 at 10:05 am

    Passing a block to Array.new works the same way as passing a block to any other method: It’s a sort of implicit argument. It does not appear in the method’s argument list (unless you “reify” it into a Proc using &), but you can call it using yield and check whether it is there using block_given?.

    For example if your some_func method were to take a block, it could look like this:

    def some_func(a,b,c)
      if block_given?
        yield a+b+c
      else
        a+b+c
      end
    end
    
    some_func(1,2,3) #=> 6
    some_func(1,2,3) {|x| x*2} #=> 12
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the following example i can create an object dynamically via a string; however,
I want to create a simple menu function which can call it example get_menu()
I want to create a helper object that will work the following way: It
How can I create pdf documents with 2D barcodes (for example QR code http://en.wikipedia.org/wiki/QR_Code
How can I create a basic UIButton programmatically? For example in my view controller,
How can I create View on Linked Server db. For Example I have a
For example DbVisualizer can be used to connect to a DB and create nice
Can you give me some links or example programs to create UIButtons on a
Can SQLite sort naturally? For example, CREATE TABLE animals ( id INTEGER NOT NULL
I want to create a command in mediawiki. For example, in latex I can

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.