Skip to content

Instantly share code, notes, and snippets.

@mykiy
Last active April 3, 2018 11:43
Show Gist options
  • Select an option

  • Save mykiy/a882e3b1e2e0fd3c3ee50cce4cec5667 to your computer and use it in GitHub Desktop.

Select an option

Save mykiy/a882e3b1e2e0fd3c3ee50cce4cec5667 to your computer and use it in GitHub Desktop.
reduce block in ruby
#reduce
def sum(*args) #sum method accepts many number of arguments that method passed
args.reduce(0, :+) #reduce will reduce the array in single number, 0 is the initial value and + tells the reduce method to perform addition
end
sum(1,2,3,4)
=>
10
-----
multiplication
def mul(*args)
args.reduce(1, :*)
end
mul(2,3,4,5)
=>
120
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment