Skip to content

Instantly share code, notes, and snippets.

@elskwid
Created January 20, 2011 17:53
Show Gist options
  • Save elskwid/788265 to your computer and use it in GitHub Desktop.
Save elskwid/788265 to your computer and use it in GitHub Desktop.
# 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
Copy link

ghost commented Jan 20, 2011

def to
  full_name_with_company || 'unknown destination'
end

def full_name_with_company
  retval = [full_name, company_name].join(', ')
  return retval unless retval.empty?
  nil
end

def full_name
  retval = [self.first_name, self.last_name].join(' ')
  return retval unless retval.empty?
  nil
end

@willkurt
Copy link

#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