Skip to content

Instantly share code, notes, and snippets.

@renatamarques97
Last active July 10, 2020 12:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renatamarques97/52ca1c26705177b19cf9a1a9f5eca20e to your computer and use it in GitHub Desktop.
Save renatamarques97/52ca1c26705177b19cf9a1a9f5eca20e to your computer and use it in GitHub Desktop.

numbers

def sum(first, *rest)
  rest.inject(first) { |o, x| o + x }
end

> sum(1) # first = 1, rest = []
1
> sum(1, 2) # first = 1, rest = [2]
3
> sum(1, 2, 3) # first = 1, rest = [2, 3]
6
def sum(*numbers)
  numbers.inject { |o, x| o + x }
end

strings

def full_name(first, *rest)
    rest.inject(first) { |o, x| o + " " + x }
end

full_name("renata", "marques")
=> "renata marques"
def full_name(*names)
  names.join(" ")
end

full_name("renata", "marques")
=> "renata marques"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment