Skip to content

Instantly share code, notes, and snippets.

@beaugaines
Last active September 30, 2015 04:09
Show Gist options
  • Select an option

  • Save beaugaines/86f933a6d918858f857f to your computer and use it in GitHub Desktop.

Select an option

Save beaugaines/86f933a6d918858f857f to your computer and use it in GitHub Desktop.
Array methods
# Array definition
def new_array(a,b,c,d)
[a,b,c,d]
# or:
# Array.new << a << b << c << d
# but you would never do that...
end
def first_and_last(a)
[a.first,a.last]
# another way:
# [] << a.first << a.last
# or:
# Array.new << a.first << a.last
end
def no_fruits?(a)
a.length == 0 # or a.empty?
end
# Array Methods
def reverse_plus_one!(a)
a << a.first
a.reverse!
end
def pluses_everywhere(a)
a.join("+")
end
def array_quantity_plus_one(a)
a.length + 1
end
# Grocery List
def add_item!(item, list)
list << item unless list.include?(item)
list # must return list since if list includes item, the previous line evaluates to nil
end
def remove_item!(item, list)
list.delete(item) if list.include?(item)
list # must return list since Array#delete returns the item that is deleted
end
def show_list(list)
list.sort.uniq
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment