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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:50:19+00:00 2026-06-17T08:50:19+00:00

So I really like this syntax in Lisp: (+ 1 1 2 3 5

  • 0

So I really like this syntax in Lisp:

 (+ 1 1 2 3 5 8 13)
 => 33

I want to add a list of items in Ruby and would like to approximate this as best as possible.
Right now, my best solution involves an array and the collect/map method.

So:

sum = 0; [1,1,2,3,5,8,13].collect { |n| sum += n }

BUT…

I would like to add methods to this which could return nil.

sum = 0; [1, booking_fee, 8,13].collect { |n| n = 0 if n.nil?; sum += n }

And it would be really nice to do this, where all of the lines in the middle refer to methods that may return nil, but I can’t exactly build an array in this manner. This is just an idea of what I want my syntax to look like.

def total
  Array.new do
    booking_fee
    rental_charges
    internationalization_charges
    discounts
    wild_nights
  end.collect { |n| n = 0 if n.nil?; sum += n }
end

Any suggestions before I try to hack away and effectuate Greenspun’s Rule? (Programming is indeed a compulsion.

  • 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-17T08:50:20+00:00Added an answer on June 17, 2026 at 8:50 am
    • There is already a method inject for doing what you want.
    • Changing nil to a number without affecting a number is easy: use to_i (or to_f if you are dealing with float).

    .

    [
      booking_fee,
      rental_charges,
      internationalization_charges,
      discounts,
      wild_nights,
    ].inject(0){|sum, item| sum + item.to_i}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would really like to do something like this: Callable<MyObject> myCallable = .... Future<MyObject>
i want to fetch information from the database using objects. i really like this
I know this isn't a programming question but I would really like the users
I really like this syntax, though I forgot what it was called @notifications.delete_if{|x| x
If i have some code looks like this(Please ignore the syntax, i want to
I just started to learn Erlang, and really like their list comprehension syntax, for
So I rewrote my paths to something like: URL/really/nice/paths/ using mod_rewrite rules like this:
I feel like this might be really simple but I'm just not getting it
Can anyone explain why using comments like this in css cause really weird effects
Its when I try to do stuff like this I realise I really need

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.