Skip to content

Instantly share code, notes, and snippets.

@beaugaines
Last active January 1, 2016 19:08
Show Gist options
  • Save beaugaines/8188102 to your computer and use it in GitHub Desktop.
Save beaugaines/8188102 to your computer and use it in GitHub Desktop.
def add_item (item, list)
# nb: no need to return list explicitly; the << operator - which
# is a method in the Array class - returns the Array itself.
list << item unless list.include?(item)
# list
# also, no need for this else statement
# else
#list
#end
end
def remove_item (item, list)
list.delete(item)
# Array#delete returns the deleted item. So here you need to return list
list
end
# this is the right approach. note that you can chain method calls because
# each method call on your array object will return the array itself. so here
# the result of `list.uniq` will itself be an array - and then you call sort on
# that - and the return value of that will be your uniq, sorted array
def full_list (list)
list.uniq.sort
end
# also - as mentioned, be careful about indentation.
class Feh
def my_method
collection.each do |item|
item.do_something
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment