I wish to define methods within the class they belong to.
As implied in the title of this post, I prefer the class << self approach over the def thod one.
Ruby undefined method map how to#
Hurray, we now know exactly what class methods in Ruby are! We are ready to have an knowledgeable discussion about how to code them. We can see that theory in action easily: Example.is_a? Object # => true Example.class # => Class Example.singleton_class => # Example.instance_methods(false) => Example.singleton_class.instance_methods(false) => Ĭalling instance_methods with the false argument simply excludes inherited methods from the methods lists ( source). Since in Ruby classes are objects as well, class methods are merely methods defined on a specific instance of Class. (Well, almost every object this is not true for Numeric objects.) When we call a method upon an object, its singleton class is the first place Ruby will look for that method, before the regular class and its ancestor chain. That is because behind the scenes Ruby stored the average method in a special class that only an_array is pointing to - its own singleton class: an_array.singleton_class # => #> an_array.singleton_methods # => Įvery instance of every Ruby class has its own singleton class which is where its singleton methods are stored, such as the one we have just defined. Yet executing the same method on another instance of Array would end up in NoMethodError again: another_array = another_array.average # => NoMethodError: undefined method `average' for :Array We could monkey-patch Array and define an average method in it, but if we needed this method only for our an_array, we could also do this: def an_array.average reduce(:+) / count.to_f end If we try to run an_array.average we will get NoMethodError since neither Array nor its superclasses have an average method defined in them: an_array.average # NoMethodError: undefined method `average' for :Array That’s quite a common knowledge, so in order to challenge that, consider the following example: an_array = In general, Ruby methods are stored in classes while data is stored in objects, which are instances of classes. To answer that question we will need a quick dive into the Ruby Object Model. We all have defined class methods many times, but do we really know how do they work? In order to make an informed decision, it’s mandatory to understand the issue at stake well. Similarly to fashion, code style reflects our credo as developers, our values and philosophy. Yet, it certainly is important to make the proper choices when picking up style. It’s not style that matters, but sameness of style. Choosing a style guide means building agreements in areas where we have strong differences of opinion about issues of little significance. many stylistic choices are arbitrary, and purely a matter of personal preference. In a well-articulated write-up Sandi Metz claims that Having a shared style and following an actual style guide within an organization is important. Take a look at that section if you are unsure how all these actually look like. It criticizes the more explicit def thod, but does subordinately support the more esoteric class << self syntax. The Ruby Style Guide indicates that the preferred way to define class methods is def thod.