From: "Esteban Manchado Velázquez" Date: 2006-06-11T04:09:15+09:00 Subject: Re: Global not evaluated properly in 'if' statement modifier --HcAYCG3uE/tztfnV Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hi Darren, On Sun, Jun 11, 2006 at 03:13:30AM +0900, darren kirby wrote: > Hello all, >=20 > I am scratching my head over this one. > I have a method: >=20 > 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' > > end > end 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}" 2) I wouldn't use a global variable anyway. I think it's better sending the out type as a parameter. 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. 4) Wouldn't it be better having dispatchFile as a Convert method? Perha= ps with other name, but... > When I run this code the 'if's are never evaluated. That is, I have print= ed=20 > $outtype, and it has a valid value, but the second method which matches t= he=20 > value never runs. If I comment out the 'if' and run the two methods manua= lly=20 > it works fine. >=20 > In my tests $outtype's value is 'mp3', so why doesn't song.wavToMp3 run? > What am I missing? 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, --=20 Esteban Manchado Vel=E1zquez - http://www.foton.es EuropeSwPatentFree - http://EuropeSwPatentFree.hispalinux.es --HcAYCG3uE/tztfnV Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.3 (GNU/Linux) iD8DBQFEixjQhYgK5b1UDsERAk0WAJkBp9z3iIXQkU9L6CDAiu+rn+dY+wCeP35Z z3FyKPgOyxh8P6J/1IDwuaI= =XsBv -----END PGP SIGNATURE----- --HcAYCG3uE/tztfnV--