# Newbie query regarding ruby best practice

**URL:** <https://www.ruby-forum.com/t/newbie-query-regarding-ruby-best-practice/53340>\
**Category:** Ruby\
**Created:** [February 1, 2006, 11:40pm UTC](https://www.ruby-forum.com/t/newbie-query-regarding-ruby-best-practice/53340 "2006-02-01T23:40:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neowulf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c4cdca/32.png) [@Neowulf](https://www.ruby-forum.com/u/Neowulf)\
**Post date:** [February 1, 2006, 11:40pm UTC](https://www.ruby-forum.com/t/newbie-query-regarding-ruby-best-practice/53340/1 "2006-02-01T23:40:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Neowulf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c4cdca/32.png) [@Neowulf](https://www.ruby-forum.com/u/Neowulf)\
**Post date:** [February 1, 2006, 11:43pm UTC](https://www.ruby-forum.com/t/newbie-query-regarding-ruby-best-practice/53340/2 "2006-02-01T23:43:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Neowulf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c4cdca/32.png) [@Neowulf](https://www.ruby-forum.com/u/Neowulf)\
**Post date:** [February 2, 2006, 12:08am UTC](https://www.ruby-forum.com/t/newbie-query-regarding-ruby-best-practice/53340/3 "2006-02-02T00:08:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Neowulf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c4cdca/32.png) [@Neowulf](https://www.ruby-forum.com/u/Neowulf)\
**Post date:** [February 2, 2006, 1:27am UTC](https://www.ruby-forum.com/t/newbie-query-regarding-ruby-best-practice/53340/4 "2006-02-02T01:27:33Z")

</div>

Neowulf wrote:

> FileUtils.mv(myfile,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.

---

<div class="post-metadata">

**Author:** ![Neowulf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c4cdca/32.png) [@Neowulf](https://www.ruby-forum.com/u/Neowulf)\
**Post date:** [February 2, 2006, 7:45am UTC](https://www.ruby-forum.com/t/newbie-query-regarding-ruby-best-practice/53340/5 "2006-02-02T07:45:23Z")

</div>

> 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
