Skip to content

Instantly share code, notes, and snippets.

@jacortinas
Created December 13, 2012 17:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacortinas/4277991 to your computer and use it in GitHub Desktop.
Save jacortinas/4277991 to your computer and use it in GitHub Desktop.
class Name
def initialize name=''
@name = name
end
def name
@name ||= @name.to_s
end
alias :full :name
def first
split_name.first
end
def last
split_name.last
end
def == other
self.to_s == other.to_s
end
def to_s
name
end
def inspect
"#<Name \"#{name}\">"
end
private
def split_name
@split_name ||= name.split ' ', 2
end
end
class User < ActiveRecord::Base
def name
@name ||= Name.new super
end
def name= new_name
@name = Name.new new_name
super @name
end
def first_name
name.first
end
def last_name
name.last
end
end
@jacortinas
Copy link
Author

User has a name column that is a string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment