Last active
August 17, 2017 03:19
-
-
Save Nevillealee/21f877ac7114b64c94d46586bc3fdabd to your computer and use it in GitHub Desktop.
Lambda example
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
class Vehicle | |
attr_reader :make, :year | |
def initialize(make, year) | |
@make =make | |
@year = year | |
end | |
end | |
turbo911 = Vehicle.new("Porshe", 2017) | |
grandnational = Vehicle.new("Buick", 1987) | |
gallardo = Vehicle.new("Lamborghini", 2013) | |
slr = Vehicle.new("Mercedes", 2004) | |
cars = [turbo911, grandnational, gallardo, slr] | |
def sort_car(car, meth) | |
car.sort do |a, b| | |
meth.call(a) <=> meth.call(b) | |
end | |
end | |
manufacturer = lambda do |car| | |
car.make | |
end | |
sorted_cars = sort_car(cars, manufacturer) | |
puts sorted_cars.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment