What is the advantage is to use class methods over object me

Hi All,

Could u please tell me what is the advantage is to use class methods
over object method in ruby ? Which one is faster and what make it
faster, how it handles the parse and controls in depth ?

Thanks in advance
Jak.

On 10-07-14 11:17 PM, Arun K. wrote:

This is my present, probably naive, belief:

  1. All programming should be as much functional programming as is
    practical.
  2. When you have to have side effects, make a class to hold methods
    around the
    data. That way you keep better track of it and you illustrate a well
    understood
    story of what you are doing, which makes it safer and more maintainable.
  3. Then keep refactoring to make as much functional programming as is
    practical.

2010/7/15 Xeno C. / Eskimo North and Gmail
[email protected]:

  1. Then keep refactoring to make as much functional programming as is
    practical.

Why do you say we should do as much functional programming -
especially in a language such as Ruby which is much more OO than
functional?

Kind regards

robert

2010/7/15 Arun K. [email protected]:

Could u please tell me what is the advantage is to use class methods
over object method in ruby ? Which one is faster and what make it
faster, how it handles the parse and controls in depth ?

IMHO the primary criterion for using class methods over instance
methods is whether the code encapsulates an algorithm that is
independent of instance state or not. There can be no general
statement of the form “class methods are always faster than instance
methods” because this is not true. It completely depends on the
problem being solved.

Kind regards

robert

On Thursday, July 15, 2010 01:17:57 am Arun K. wrote:

Hi All,

Could u please tell me what is the advantage is to use class methods
over object method in ruby ?

Because the code “belongs” there, for whatever reason. For example, in
ActiveRecord:

john = User.find_by_name(‘John S.’)
puts john.email

It really wouldn’t make sense to make find_by_name an instance method,
or to
make email a class method.

Which one is faster and what make it
faster,

Neither. Both. The one that’s doing what it’s supposed to.

Let me see if I can make it clearer: There actually is no such thing as
a
class method in Ruby. Everything is an object, including classes –
class
methods are just “object methods”, or instance methods, on the class
object.

Here, let me prove it to you:

File.read ‘readme.txt’
f = File
f.read ‘readme.txt’

See? The call to File.read was just another method call.

But, as an example, if the Rails core team had written ActiveRecord so
it
worked like this:

User.find_email_by_name(‘John S.’)
User.find_address_by_name(‘John S.’)

That’s much harder to do efficiently than this:

john = User.find_by_name(‘John S.’)
john.email
john.address

I’m not saying instance methods are faster, they just make more sense in
that
specific case. It’s like asking whether arrays or hashes are faster –
it
depends entirely on what you’re trying to do, and it’s hardly even about
speed
– if you know what they’re meant for, you wouldn’t really think to use
an
array instead of a hash, or vice versa.

Thanks David M. and all, It clear my doubts and also get some valid
points…U all great.