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

  • Home
  • SEARCH
  • 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 8215025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:39:11+00:00 2026-06-07T11:39:11+00:00

Is it possible to write a function that takes a block of strings, does

  • 0

Is it possible to write a function that takes a block of strings, does something with the strings, and then returns an array with the strings?

def collect_string(&block)
   # just toss them into an array and return it
   return ...
end

a = collect_string {
   "string 1"
   "string 2"
   "string 3"
}

When I print out what a is, I should get

["string 1", "string2", "string3"]

Now suppose I decided to change my mind and wanted to do something more with the strings first. Maybe I want to remove all of the vowels first, or just grab the first 3 characters.

  • 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-06-07T11:39:12+00:00Added an answer on June 7, 2026 at 11:39 am

    This is really not what the blocks are for. You’re going to make an array of strings anyway, so why not use an array to begin with?

    def collect_string &block
      v = block.call
      # do something with v
    end
    
    # block returning array
    a = collect_string {[
       "string 1",
       "string 2",
       "string 3"
    ]}
    

    If you use a block as in your example then it will return only “string 3”, the last expression evaluated. Previous strings are lost.

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

Sidebar

Related Questions

Is it possible to write a function that takes a type and returns a
Is it possible to write a function f that takes an arbitrary tuple of
How can I write (if it's possible at all...) a function which takes an
How do I write a function in C++ that takes a string s and
I want to write a Module Arg[f_,n_] that takes a function f (having <=n
I'm looking to write a function that takes the integers within a list, such
A while back I found that it's possible to write a C++ function that
I am using a library that has a function that takes an array of
Is it possible to write a Hadoop-ready reduce function that can find the longest
I need to write a function that takes 4 bytes as input, performs 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.