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

A

Arun Kumar

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.
 
X

Xeno Campanoli / Eskimo North and Gmail

On 10-07-14 11:17 PM, Arun Kumar 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.
 
R

Robert Klemme

2010/7/15 Xeno Campanoli / Eskimo North and Gmail <[email protected]=
:
On 10-07-14 11:17 PM, Arun Kumar wrote:

This is my present, probably naive, belief:

1. =A0All programming should be as much functional programming as is
practical.
2. =A0When you have to have side effects, make a class to hold methods ar= ound
the data. =A0That way you keep better track of it and you illustrate a we= ll
understood story of what you are doing, which makes it safer and more
maintainable.
3. =A0Then 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

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
R

Robert Klemme

2010/7/15 Arun Kumar said:
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
 
D

David Masover

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 Smith')
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 Smith')
User.find_address_by_name('John Smith')

That's much harder to do efficiently than this:

john = User.find_by_name('John Smith')
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.
 
A

Arun Kumar

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top