Last active
August 29, 2015 13:56
-
-
Save oneawayman/8844350 to your computer and use it in GitHub Desktop.
Bloc Web Development Blocks Checkpoint
This file contains 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
##Sorting | |
# This method should take an array of strings or hashes as an argument, and sort each element by its length | |
def sort_by_length(sort_this_array) | |
sort_this_array.sort { |x,y| x.length <=> y.length } | |
end | |
# Create method named filter that takes an array of numbers as an argument | |
# and returns an array consisting of numbers that are greater than 5. | |
def filter(filter_this_array) | |
filter_this_array.select { |num| num > 5 } | |
end | |
##Mapping | |
# Create an add_two method that takes an array of numbers and returns an array of strings. | |
def add_two(map_this_array) | |
map_this_array.map { |item| "#{item} + 2 = #{item + 2}" } | |
end | |
## Practice with Yield | |
class Array | |
def new_map | |
a = [] | |
self.each do |item| | |
a << yield(item) | |
end | |
a | |
end | |
def new_map!(&block) | |
array = self | |
self.replace(self.new_map(&block)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Notes -
The spaceship operator returns 1,0 or -1 based on the value of the left argument relative to the value of the right argument. For example: a <=> b returns:
-1 if a < b
0 if a = b
1 if a > b