quoth the Esteban Manchado Velázquez:
Hi Darren,
Hi,
I did get it sorted but I wanted to respond to your comments…
>
> Some suggestions:
>
> 1) I would calculate the method name dinamically with $outtype, instead
> of having 4 if constructions. Something like:
>
> song.send "wavTo#{$outtype.capitalize}"
Hey, that’s pretty neat. I wondered if there was a way to run the method
based
on “variable variables”. This is certainly more elegant.
2) I wouldn't use a global variable anyway. I think it's better sending
the out type as a parameter.
I have tried this. I guess I don’t fully understand Ruby namespaces, but
$outtype gets it’s value from a command line option, and if I don’t
assign it
to a global then I get:
undefined local variable or method `outtype’ for main:Object (NameError)
It is assigned thusly:
opts.each do |opt, arg|
$outtype = arg if ( opt == ‘–out’ )
[etc…]
end
If I assign to a regular variable ‘outtype’ and then pass it to
dispatchFile():
files.each { |file| dispatchFile(file, outtype) }
I get the undefined variable error, so I guess vars in the do loop are
private, and I don’t know how to use them outside of the loop if they
are not
globals.
3) What is "type", a method? It should probably also be a parameter for
dispatchFile, but I don’t know if that’s a function or a method.
‘type’ is a simple variable that holds the type of infile. So if the
infile is
an ogg, type will be ‘ogg’. It’s value is actually assigned in
dispatchFile
(the method I posted), I just removed a few housekeeping lines to keep
the
code I posted short.
4) Wouldn't it be better having dispatchFile as a Convert method?
Perhaps with other name, but…
I don’t know if this is feasible. It would certainly be a lot of
rewriting to
do it. Convert only accepts 1 file at a time. The way my script works is
to
create an array of files from the command line arguments, then feed one
by
one to dispatchFile, where the proper Convert methods will be run
depending
on the input and output formats. This allows me to just send any file to
Convert and it will Do The Right Thing.
def wavToMp3; puts "wavToMp3"; end
case type
dispatchFile(‘foo’)
------------------------------------- >8
It printed “oggToWav” and “wavToMp3”. It probably has nothing to do with
global variables not evaluating, but I can’t tell without seeing the rest
of the code.
Regards,
Thanks a lot for your help,
-d