Skip to content

Instantly share code, notes, and snippets.

@Nevillealee
Last active August 17, 2017 03:19
Show Gist options
  • Save Nevillealee/21f877ac7114b64c94d46586bc3fdabd to your computer and use it in GitHub Desktop.
Save Nevillealee/21f877ac7114b64c94d46586bc3fdabd to your computer and use it in GitHub Desktop.
Lambda example
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