Skip to content

Instantly share code, notes, and snippets.

@swistak35
Created August 12, 2012 21:20
Show Gist options
  • Save swistak35/3334538 to your computer and use it in GitHub Desktop.
Save swistak35/3334538 to your computer and use it in GitHub Desktop.
Functional equivalent to oop code in ruby
# Functional:
new_person = ->(name,birthdate,gender,title,id=nil) {
return ->(attribute) {
return id if attribute == :id
return name if attribute == :name
return birthdate if attribute == :birthdate
return gender if attribute == :gender
return title if attribute == :title
nil
}
}
dave = new_person.("Dave","06-01-1974","male","Baron")
puts dave.(:name) # => "Dave"
puts dave.(:gender) # => "male"
# OOP:
class Person
attr_reader :id, :name, :birthdate, :gender, :title
def initialize(name,birthdate,gender,title,id=nil)
@id = id
@name = name
@birthdate = birthdate
@gender = gender
@title = title
end
end
dave = Person.new("Dave","06-01-1974","male","Baron")
puts dave.name
puts dave.gender
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment