Bring your methods to life with punctuation

One of the little things that makes ruby so different is the fact that you can use punctuation in the name of your methods. I could really well be wrong on this, but I think ruby is the only language that allows a programmer to name his method logged? for example. Oh well, I should play it more safe. Let’s say that ruby is the only language I know that allow this.
It’s great because just by looking at the question mark, you know what the method is all about and that it will (should) return yes or no. The other punctuation character you can use is the exclamation (!). It is generally used to announce some kind of destructive action or something that is unusual. The “!” sign is used a lot in Rails ActiveRecord methods. For example, you can call both and!
What is the difference?! will raise an exception if something went wrong in the process of saving the object back to the database while will remain silent.
What’s your take on using punctuation in method names? Is it a useful feature or just flash in the pan?

4 thoughts on “Bring your methods to life with punctuation

  1. Thanks for that 🙂
    So how do you do your own method like that?
    For: ?
    def method
    return true or false
    if method?
    #do that
    For: !
    def method
    raise “exception”
    rescue “exception”
    or something like that?
    Can you tell us how to use both ! or ? or nothing ?, a article/post about methods will be really great 😀

  2. Hey Jamal,
    The “?” and “!” characters are not doing something special on their own. It’s just that you can include them in the name of your methods if you want. It’s only a convention to use the “?” character in methods that does nothing worth of mention other than returning yes or no. As it’s just a convention to use the “!” character in methods that do something destructive or “dangerous”. You could (but should not) use them for other purpose.
    So, if you want to write : if my_method? …
    You would have to define it this way :
    def my_method?
    return true #or false, or anything you want
    If you want to write : destroy!
    You would have to define it this way :
    def destroy!
    #Do something destructive like deleting all rows of a table in a DB
    I hope it does answer your question

  3. Most commercial stock shooters have found that the effortless way to produce a bunch of commercially acceptable stock images is to capitalize on the ideas of the leading stock houses that have done the market research and know the trends.
    If you know that you have a really important shoot the
    next day, make sure that you get ample rest. Many photographers require lenses with focal lengths of 300mm or more,
    such as the Canon EF100 – 400L IS, which is a great lens for sports

Leave a Reply

Your email address will not be published. Required fields are marked *