The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
? and ! are only allowed as method suffixes in ruby

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Paul Gross

Posts: 152
Nickname: pgross
Registered: Sep, 2007

Paul Gross is a software developer for ThoughtWorks.
? and ! are only allowed as method suffixes in ruby Posted: Sep 27, 2007 2:05 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Paul Gross.
Original Post: ? and ! are only allowed as method suffixes in ruby
Feed Title: Paul Gross's Blog - Home
Feed URL: http://feeds.feedburner.com/pgrs
Feed Description: Posts mainly about ruby on rails.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Paul Gross
Latest Posts From Paul Gross's Blog - Home

Advertisement

Ruby method names can contain a question mark or an exclamation point, but it must be the last character. So foo? and foo! are fine, but foo?bar and foo!bar are not.

This does not seem like a big deal until you run into code that assumes that it can add characters to the end of any method name. For example, Ruby Facets has a method called cache which allows the user to ensure a method is called once and only once per instance. You can see the code and comments in the Facets SVN Repository.

For exampe, the following code will only print bar once:


def foo
  puts "bar" 
end
cache :foo

foo
foo

cache is implemented by aliasing the original method and redefining it. The new version checks a hash to see if it has already been called. The aliasing is done with the line:


alias_method '__#{ m }__', '#{ m }'

So a method called:

foo

will be aliased to:

__foo__

This will fail if the method ends in a ? or ! since it cannot append __ to the end of the method name.

This is a subtle bug to track down. The error is cryptic, since the ruby parser fails when it reaches the trailing underscores. Furthermore, the code doesn’t fail until the method is called, not when the caching is done:


def foo?
  true
end
cache :foo?

>> foo?
NoMethodError: undefined method `__' for #<Object:0xb7cc99f0 @cache={"foo?"=>{}}>
        from (eval):8:in `foo?'
        from (irb):34

A better way to redefine methods is to unbind and rebind them. This technique is explained nicely in Jay’s blog: Ruby: Alias method alternative

Read: ? and ! are only allowed as method suffixes in ruby

Topic: - ¡Papá el Messenger me quiere obligar a actualizarlo! Previous Topic   Next Topic Topic: Edge Rails Documentation: Revisited

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use