Created
August 12, 2012 21:20
-
-
Save swistak35/3334538 to your computer and use it in GitHub Desktop.
Functional equivalent to oop code in ruby
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
# 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