From: masta Blasta Date: 2013-02-22T05:47:58+09:00 Subject: Re: How meta class differs from real class ? Xavier R. wrote in post #1098080: > In Ruby- > > Could anyone help me to understand how meta class differs from real > class ? > > Examples with discussion could be better to understand the core of the > concept Not sure what you mean by "real class". Everything in Ruby is an object. All objects inherit from BasicObject. Classes in ruby are instances of the Class object, meaning: MyClass = Class.new Instance methods are on an instance of a class. Class methods, singleton methods of the eigenclass (metaclass), are called on the class constant. A good example is the Math module (Class is a subclass of Module). The Math module provides a bunch of utility methods, that wouldn't be as useful if they had to be called on instances of an object. you can easily do x = Math.sqrt(144) otherwise you'd have to open up the Fixnum class and add a sqrt method, in order to x = 144.sqrt but that would only be on Fixnum. What if the number was a Float 144.0, or a Bignum ? Another example, Let's say we have a class Student. stu = Student.new #(btw 'new' is a singleton method) some logical instance methods might be calculate_GPA, show_schedule etc. stu.calculate_GPA But what if we wanted to know the total average GPA of all our students? Calling stu.average_GPA does not make sense, because we're calling it on a specific student. It makes the code confusing. Instead we should have that method on the Eigenclass def Student.average_GPA; ..calculate GPA from all student records; end so then Student.average_GPA() would give us the average GPA amongst all our students, and it is namespaced in a way that is logical. -- Posted via http://www.ruby-forum.com/.