Topic: Formatting a Name
I'd like to do something like the following
self.first_name + " " unless self.first_name.nil?
self.middle_name + " " unless self.middle_name.nil?
self.last_name unless self.last_name.nil?
without having to resort to something like this
unless !self.middle_name.nil? && !self.middle_name.nil? && !self.middle_name.nil?
if !self.first_name.nil? && !self.last_name.nil?
self.first_name + " " + self.last_name
if self.first_name.nil? && self.last_name.nil?
self.first_name + " " + self.middle_name + " " + self.last_name
While this DOES work, it looks horrible! I know I'm in the Ruby forum, but this is in a Rails app. I'm here because it's a formatting problem, not a Rails issue. So, the problem. The top example only spits out the last name if it is there. If it is not, it returns nil.
How do I get it to return all three parts of the name with those conditions in place?
Last edited by slant (2007-06-06 13:33:12)