I’m fairly new to ruby (and am using Rails) and was wondering whether the following method could be tidied up. In its current state it does work, however I get the feeling that there is a much nicer way of writing it and would like to learn more about the syntax.
def fullAddress
full = self.address1 + "</br>"
if self.address2.blank?
else
full = full + self.address2 + "</br>"
end
if self.address3.blank?
else
full = full + self.address3 + "</br>"
end
full = full + self.posttown + "</br>" + self.postcode
end
Every ‘self’ will have address1, posttown and postcode, but address2 and address3 are optional and should only be added to the fullAddress if they are present (i.e. neither nil or blank).
This question might not be suitable for SO, but I’ve come across this type of helper method quite a bit and am sure I can implement it in a nicer way.
1 Answer