Try this:
array.inject(0){|sum,x| sum + x }
See Ruby’s Enumerable Documentation
(note: the 0
base case is needed so that 0
will be returned on an empty array instead of nil
)
Related Posts:
- What does %w(array) mean?
- Array to Hash Ruby
- Ruby: undefined method `[]’ for nil:NilClass when trying to get Enumerator on an Array of Hashes
- How to map with index in Ruby?
- Ruby array to string conversion
- How to check if a value exists in an array in Ruby
- What is the “right” way to iterate through an array in Ruby?
- Difference between map and collect in Ruby?
- Creating and iterating a 2d array in Ruby
- What is the best way to convert an array to a hash in Ruby
- How can I delete one element from an array by value
- Get underlined text with Markdown
- What is the meaning of *nix?
- Get underlined text with Markdown
- What does the “map” method do in Ruby?
- How to write a switch statement in Ruby
- Rails 5 ActionController::InvalidAuthenticityToken error
- what does ? ? mean in ruby
- How to update Ruby Version 2.0.0 to the latest version in Mac OSX Yosemite?
- How to use “gets” and “gets.chomp” in Ruby
- Getting a substring in Ruby by x number of chars
- How to check whether a string contains a substring in Ruby
- Ruby: How to install a specific version of a ruby gem?
- ruby operator “=~” [duplicate]
- What does << mean in Ruby?
- What is attr_accessor in Ruby?
- ActionController::UnknownFormat
- What does ||= (or-equals) mean in Ruby?
- How to read lines of a file in Ruby
- Ruby equivalent for Python’s “try”?
- What does “+=” (plus equals) mean?
- Rails 4 RoutingError: No Route Matches
- undefined method (NoMethodError) ruby
- Multi-Line Comments in Ruby?
- How to uninstall ruby installed by ruby-install
- PG::ConnectionBad – could not connect to server: Connection refused
- Rails syntax error : unexpected keyword_ensure, expecting end-of-input
- Removing all installed Gems and starting over
- Ruby `send` vs `call` method
- What is the meaning of “do | |” in Ruby?
- RVM is not a function, selecting rubies with ‘rvm use …’ will not work
- How to delete specific characters from a string in Ruby?
- Ruby: kind_of? vs. instance_of? vs. is_a?
- Rails: I installed Ruby, now “bundle install” doesn’t work
- How to do a newline in output
- Ruby: What is the easiest way to remove the first element from an array?
- Ruby String to Date Conversion
- p vs puts in Ruby
- Determining type of an object in ruby
- Fizz Buzz in Ruby for dummies
- How to Uninstall RVM? [duplicate]
- What is the difference between a Process’ pid, ppid, uid, euid, gid and egid?
- Equivalent of “continue” in Ruby
- What does the Ruby method ‘to_sym’ do?
- Array to Hash Ruby
- Ruby 2.0.0p0 IRB warning: “DL is deprecated, please use Fiddle”
- Getting error: dyld: Symbol not found: _clock_gettime
- Uninstall old versions of Ruby gems
- Uploading a file in Rails
- What is java interface equivalent in Ruby?
- Sort hash by key, return hash in Ruby
- Gem installation error: You have to install development tools first
- How to understand strptime vs. strftime
- How to break out from a ruby block?
- ERROR: While executing gem … (Gem::FilePermissionError)
- No such file or directory @ rb_sysopen ruby
- How to add to an existing hash in Ruby
- Ruby ‘require’ error: cannot load such file
- How to convert a string to lower or upper case in Ruby
- What is the canonical way to trim a string in Ruby without creating a new string?
- Difference between $stdout and STDOUT in Ruby
- No implicit conversion of String into Integer (TypeError)?
- Multi-Line Comments in Ruby?
- ValueError: setting an array element with a sequence
- Python Math – TypeError: ‘NoneType’ object is not subscriptable
- How do I declare and initialize an array in Java?
- Convert list to array in Java [duplicate]
- Convert pandas dataframe to NumPy array
- Are vectors passed to functions by value or by reference in C++
- Does JavaScript have a method like “range()” to generate a range within the supplied bounds?
- Double array initialization in Java
- Array of arrays (Python/NumPy)
- I want to declare an empty array in java and then I want do update it but the code is not working
- Why is using “for…in” for array iteration a bad idea?
- Overflow / math range error for log or exp
- How do I create an array of strings in C?
- How to properly malloc for array of struct in C
- C++ array assign error: invalid array assignment
- Reading CSV file and storing values into an array
- “Actual or formal argument lists differs in length”
- Empty an array in Java / processing
- Is there a simple way to make a random selection from an array in JavaScript or any other programming language?
- Error array dimension missing
- Sorting arrays in NumPy by column
- console.log showing contents of array object
- How to sort 2 dimensional array by column value?
- char array not assignable
- Warning: comparison of distinct pointer types
- Using the Pythagorean theorem with Java
- Array of Linked Lists C++