Created
January 20, 2011 17:53
-
-
Save elskwid/788265 to your computer and use it in GitHub Desktop.
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
# Version 0 (original) | |
def to | |
begin | |
name=self.first_name | |
unless name.nil? | |
name | |
name=name+" "+self.last_name | |
else | |
name=self.last_name | |
end | |
if name.nil? | |
name=self.company_name | |
elsif !self.company_name.nil? | |
name=name+', '+self.company_name | |
end | |
name | |
rescue StandardError=>e | |
name="unknown destination" | |
end | |
end | |
# Version 2 | |
def to | |
name = "#{self.first_name} #{self.last_name if self.first_name}" | |
name = self.company_name if name.nil? or name.strip.blank? | |
name = "unknown destination" if name.nil? or name.strip.blank? | |
name.strip | |
end | |
# Version 3 | |
def to | |
return retval = [ "#{self.first_name} #{self.last_name}".strip, self.company_name ].join(", ") unless retval.blank? | |
'unknown destination' | |
end | |
# Version 4 | |
def to | |
human_name.blank? ? 'unknown destination' : human_name | |
end | |
private | |
def human_name | |
[ "#{self.first_name} #{self.last_name}".strip, self.company_name ].join(", ") | |
end | |
# Version 5 | |
def to | |
# got a name? return it. company? return it too. otherwise unknown | |
[[[first, last].join(" ").strip, comp].join(', ').strip.gsub(/^,\s/,''), "unknown destination"].detect{|a| a.present?} | |
end |
ghost
commented
Jan 20, 2011
#I'm pretty sure this is the official ruby way to do this
def to
markers = [""," ",":D",", "]
((self.instance_variables().sort {|x,y| y.length <=> x.length}).map do |x|
markers[11%x.length] << self.instance_variable_get(x)
end).inject(["unknown destination","",""]){|x,y| (x<<y)[1..-1]}.join().sub(/^( |,)*/,"")
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment