Ruby still strange to me... ;-)

Try putting the definition of error_messages above the place where you
use it.


Hi --

Thanks, this drove me into the right direction.

Anyway, I changed my design a little bit:

class Country < ActiveRecord::Base
  validates_presence_of :abbrevation
  validates_presence_of :name

  def self.error_messages
    { :not_allowed_chars_in_abbrevation => 'Only characters A-Z are

  def after_validation
self.error_messages[:not_allowed_chars_in_abbrevation]) if
!@errors.on(:abbrevation) and @abbrevation !~ /^[a-z]{2}$/i

Now I'm getting such nasty errors againa! Why? :open_mouth:

NoMethodError: undefined method `error_messages' for

Like most questions about Ruby, it comes down to:

  1. an object can have its own methods
  2. classes are objects too


You've defined error_messages as a singleton method for your class (a
class method), but then you're trying to call it on a totally
different object, namely an *instance* of your class.


Hi --

Thanks a lot for this information. I see, I'm still quite nowhere in
really understanding Ruby, but I'm making progress. :wink:

Is it possible that in Java on can call class methods (static methods)
from the class itself and from an instantiated object?

MyClass.staticMethod() => someValue
myClassObj.staticMethod() => someValue

I'm just wondering... :slight_smile: Already some years ago since I used Java.

I don't know (I'm not a Java programmer), but someone probably does
:slight_smile: In Ruby, you can do:

  class MyClass
     def self.some_class_method
        # ...

     def an_instance_method

Basically, as long as you can get hold of an object that understands
what you're asking it to do (in this case, the object in question is
self.class), you can ask it to do it.


Yes java and ruby work the same in this manner. You can call static
or class level methods from either another static or an instance


Well I wouldn't say that they work exactly the same... You can't call a
class method on an instance directly (foo.some_class_method), but you
can call it on the class of the instance (foo.class.some_class_method).

In java, you can call the static method on the instance but it's a
compiler warning ("Static method should be accessed in a static way")...
you're supposed to call static methods on the class itself.


Michael Kovacs wrote: