2 Comments
A quick and easy tip today. It won’t impress the veteran ruby developer, but it will impress every ruby newcomers, guaranteed!
Say I have a horse racing ruby application that allow people to know information about every competing horses. I have an input box that people can use to enter the name of a horse. Once they click OK, information about the specified horse is displayed to the user.
Instead of going the traditonal route, how about adding a “horse” method to the String class? It could not be easier :
Talk about some great syntactic ruby sugar!
2 CommentsRemember people to be nice in your comments. I have no problem if you formulate a constructive remark but offensive comments will be removed as soon as they get here. You stay polite or you go elsewhere, it’s that simple. Now I will put an emoticon to convince you all that I am not angry: :)
class String
def horse
HORSES.detect { |h| h[:name] == self || h[:also_known_as].include?(self) }
end
end

