From: darren kirby Date: 2006-06-11T04:53:50+09:00 Subject: Re: Global not evaluated properly in 'if' statement modifier --nextPart1865943.BM6gAlK0ee Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline quoth the Esteban Manchado Vel=E1zquez: > Hi Darren, Hi,=20 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, inste= ad > 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 ba= sed=20 on "variable variables". This is certainly more elegant. > 2) I wouldn't use a global variable anyway. I think it's better sendi= ng > the out type as a parameter. I have tried this. I guess I don't fully understand Ruby namespaces, but=20 $outtype gets it's value from a command line option, and if I don't assign = it=20 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 =3D arg if ( opt =3D=3D '--out' ) [etc...] end If I assign to a regular variable 'outtype' and then pass it to=20 dispatchFile(): files.each { |file| dispatchFile(file, outtype) } I get the undefined variable error, so I guess vars in the do loop are=20 private, and I don't know how to use them outside of the loop if they are n= ot=20 globals. > 3) What is "type", a method? It should probably also be a parameter f= or > 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=20 an ogg, type will be 'ogg'. It's value is actually assigned in dispatchFile= =20 (the method I posted), I just removed a few housekeeping lines to keep the= =20 code I posted short.=20 > 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=20 do it. Convert only accepts 1 file at a time. The way my script works is to= =20 create an array of files from the command line arguments, then feed one by= =20 one to dispatchFile, where the proper Convert methods will be run depending= =20 on the input and output formats. This allows me to just send any file to=20 Convert and it will Do The Right Thing. > > I made a quick test and it worked for me, but I don't know the rest of > your code. This is the example that worked: > > ------------------------------------- 8< > ------------------------------------- class Convert > def initialize(file); end > > def oggToWav; puts "oggToWav"; end > def wavToOgg; puts "wavToOgg"; end > def wavToMp3; puts "wavToMp3"; end > def wavToM4b; puts "wavToM4b"; end > def wavToFlac; puts "wavToFlac"; end > end > > def type; "ogg"; end > > $outtype =3D 'mp3' > > def dispatchFile(file) > song =3D Convert.new(file) > case type > when 'ogg' > song.oggToWav > song.wavToOgg if $outtype =3D=3D 'ogg' > song.wavToMp3 if $outtype =3D=3D 'mp3' > song.wavToM4b if $outtype =3D=3D 'm4b' > song.wavToFlac if $outtype =3D=3D 'flac' > when 'mp3' > puts "mp3 nsbb" > end > end > > > 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, =2Dd =2D-=20 darren kirby :: Part of the problem since 1976 :: http://badcomputer.org "...the number of UNIX installations has grown to 10, with more expected..." =2D Dennis Ritchie and Ken Thompson, June 1972 --nextPart1865943.BM6gAlK0ee Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.3 (GNU/Linux) iD8DBQBEiyMswPD5Cr/3CJgRAplGAJ4lPQYjNnGEpUgPmf419Ji1szgflQCeNbsR o+vkb2l2t6rG/kzYrHovMDM= =BkW7 -----END PGP SIGNATURE----- --nextPart1865943.BM6gAlK0ee--