Last active
January 1, 2016 19:08
-
-
Save beaugaines/8188102 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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