From: Josh Cheek Date: 2012-07-04T07:38:48+09:00 Subject: Re: passing ARGV to class methods --f46d041826ea76a88104c3f49386 Content-Type: text/plain; charset=ISO-8859-1 On Tue, Jul 3, 2012 at 5:07 PM, Neubyr Neubyr wrote: > > I was wondering if I really need to pass ARGV to MyPrinter's initialize > method. The ARGV is anyway available to the class so I think it doesn't > need to be passed and can be used directly during option parsing step - > 'opts_obj.parse!(ARGV)' . But I am not sure if it's a good practice or > not. Any tips? > > It kind of depends on the scope of what you're doing. Is it a small one file script? Then its probably not necessary (of course, in that case, the class itself is probably overkill, too, probably a procedural file all in the toplevel is enough). If it's got more than one file, or you start feeling the need for the class, then you should probably do it. Otherwise, you're coupling it to ARGV, it now knows about who is using it, and it can only be used easily in that context. If you want your code to be reusable or testable, then directly referencing ARGV is a losing strategy. If you can pass in the args array and file streams, then you can test that it does everything correctly, by giving it arrays of hypothetical arguments and StringIO instances. In general, it's better to avoid hard references (references to constants and globals). But be aware of your context. -Josh --f46d041826ea76a88104c3f49386 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Tue, Jul 3, 2012 at 5:07 PM, Neubyr Neubyr <lists@ruby-forum.com= > wrote:

I was wondering if I really need to pass ARGV to MyPrinter's initialize=
method. The ARGV is anyway available to the class so =A0I think it doesn= 9;t
need to be passed and can be used directly during option parsing step -
'opts_obj.parse!(ARGV)' . =A0But I am not sure if it's a good p= ractice or
not. Any tips?


It kind of depends on the scope = of what you're doing. Is it a small one file script? Then its probably = not necessary (of course, in that case, the class itself is probably overki= ll, too, probably a procedural file all in the toplevel is enough).

If it's got more than one file, or you start feelin= g the need for the class, then you should probably do it. Otherwise, you= 9;re coupling it to ARGV, it now knows about who is using it, and it can on= ly be used easily in that context. If you want your code to be reusable or = testable, then directly referencing ARGV is a losing strategy. If you can p= ass in the args array and file streams, then you can test that it does ever= ything correctly, by giving it arrays of hypothetical arguments and StringI= O instances.

In general, it's better to avoid hard references (r= eferences to constants and globals). But be aware of your context.

-Josh
--f46d041826ea76a88104c3f49386--