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

Discussion in 'Ruby' started by Arun Kumar, Jul 15, 2010.

  1. Arun Kumar

    Arun Kumar Guest

    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.
    --
    Posted via http://www.ruby-forum.com/.
    Arun Kumar, Jul 15, 2010
    #1
    1. Advertising

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



    --
    "It's the preponderance, stupid!" - Professor Stephen Schneider, IPCC member
    Xeno Campanoli / Eskimo North and Gmail, Jul 15, 2010
    #2
    1. Advertising

  3. 2010/7/15 Xeno Campanoli / Eskimo North and Gmail <=
    >:
    > 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/
    Robert Klemme, Jul 15, 2010
    #3
  4. 2010/7/15 Arun Kumar <>:
    > 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

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Jul 15, 2010
    #4
  5. On Thursday, July 15, 2010 01:17:57 am Arun Kumar 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 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.
    David Masover, Jul 17, 2010
    #5
  6. Arun Kumar

    Arun Kumar Guest

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

    --
    Posted via http://www.ruby-forum.com/.
    Arun Kumar, Dec 31, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    2,253
    Robert Maas, see http://tinyurl.com/uh3t
    Jun 11, 2005
  2. kai
    Replies:
    2
    Views:
    330
  3. deepu
    Replies:
    37
    Views:
    1,564
    CBFalconer
    Feb 24, 2006
  4. Tobiah
    Replies:
    5
    Views:
    355
    Piet Delport
    Mar 28, 2008
  5. Kenneth McDonald
    Replies:
    5
    Views:
    312
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page