Newbie query regarding ruby best practice

Discussion in 'Ruby' started by Neowulf, Jan 31, 2006.

  1. Neowulf

    Neowulf Guest

    Hi all,

    I've just finished working on a few util scripts in Ruby to cut my
    teeth and I was wondering what the best practice is with regards the
    use of class methods.

    E.g. Is it better to do something like this...

    require 'fileutils'

    FileUtils.mv(myfile,newpath)
    FileUtils.cp(newpath,anotherpath)
    FileUtils.rm(newpath)

    or this...

    fu = FileUtils.new

    fu.mv(myfile,newpath)
    fu.cp(newpath,anotherpath)
    fu.rm(newpath)

    I assume the second would only create a single instance of a
    "fileutils" object, thus requiring less overhead than the first?

    ~Neowulf
     
    Neowulf, Jan 31, 2006
    #1
    1. Advertisements

  2. Neowulf

    Guest

    Welcome to Ruby! Regarding your question, let's use irb try it out...

    C:\irb
    irb(main):001:0> require 'fileutils'
    => true
    irb(main):002:0> fu = FileUtils.new
    NoMethodError: undefined method `new' for FileUtils:Module
    from (irb):2
    irb(main):003:0> quit

    Looks like FileUtils isn't a class and doesn't have a .new method. It's
    just a module that contains a bunch of handy methods for file
    operations.

    FileUtils has very good documentation. Use "ri FileUtils" to see it.
     
    , Jan 31, 2006
    #2
    1. Advertisements

  3. Neowulf

    Neowulf Guest

    Ah... I see. Your not really creating an instance of the object at
    all.

    Thanks for the heads up.

    I'm really starting to enjoy coding in Ruby.

    Thanks again for the help.

    Cheers,

    ~Neowulf
     
    Neowulf, Feb 1, 2006
    #3
  4. Neowulf wrote:
    > Hi all,
    >
    > I've just finished working on a few util scripts in Ruby to cut my
    > teeth and I was wondering what the best practice is with regards the
    > use of class methods.
    >
    > E.g. Is it better to do something like this...
    >
    > require 'fileutils'
    >
    > FileUtils.mv(myfile,newpath)
    > FileUtils.cp(newpath,anotherpath)
    > FileUtils.rm(newpath)
    >
    > or this...
    >
    > fu = FileUtils.new
    >
    > fu.mv(myfile,newpath)
    > fu.cp(newpath,anotherpath)
    > fu.rm(newpath)
    >
    > I assume the second would only create a single instance of a
    > "fileutils" object, thus requiring less overhead than the first?
    >
    > ~Neowulf
    >


    Or

    fu = FileUtils

    (without the .new)

    I don't think FileUtils.mv creates a new instance of FileUtils. These
    are class methods, so you can call them directly on the class object
    without instantiating the class.

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Feb 1, 2006
    #4
  5. Neowulf

    Guest

    > E.g. Is it better to do something like this...

    > require 'fileutils'


    > FileUtils.mv(myfile,newpath)
    > FileUtils.cp(newpath,anotherpath)
    > FileUtils.rm(newpath)


    Try this...

    require 'fileutils'
    include FileUtils
    mv(myfile, newpath)
    cp(newpath,anotherpath)
    rm(newpath)

    If you are looking for a more concise way of using a module (which is
    what FileUtils is), this is a common idiom for doing it.

    Bret
     
    , Feb 2, 2006
    #5
    1. Advertisements

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. Bryan Ax
    Replies:
    0
    Views:
    770
    Bryan Ax
    Jun 26, 2003
  2. Replies:
    5
    Views:
    599
    Kevin Spencer
    Aug 16, 2005
  3. Jeff
    Replies:
    0
    Views:
    1,296
  4. Replies:
    6
    Views:
    360
    Chris Uppal
    Feb 9, 2007
  5. Replies:
    1
    Views:
    505
    Manish Pandit
    Oct 23, 2007
  6. Colin Higwell

    Total newbie question: Best practice

    Colin Higwell, Nov 29, 2011, in forum: Python
    Replies:
    6
    Views:
    273
    Pedro Henrique G. Souto
    Nov 30, 2011
  7. Rcmn 73

    ruby sql-query good practice

    Rcmn 73, Feb 1, 2007, in forum: Ruby
    Replies:
    1
    Views:
    219
    Simon Kröger
    Feb 1, 2007
  8. oldyork90
    Replies:
    1
    Views:
    331
    Jeremy J Starcher
    Sep 10, 2008
Loading...