From: Matthew Smillie Date: 2006-06-11T03:57:35+09:00 Subject: Re: Global not evaluated properly in 'if' statement modifier On Jun 10, 2006, at 19:13, darren kirby wrote: > Hello all, > > I am scratching my head over this one. > I have a method: > > def dispatchFile(file) > song = Convert.new(file) > case type > when 'ogg' > song.oggToWav > song.wavToOgg if $outtype == 'ogg' > song.wavToMp3 if $outtype == 'mp3' > song.wavToM4b if $outtype == 'm4b' > song.wavToFlac if $outtype == 'flac' > when 'mp3' > > end > end > > When I run this code the 'if's are never evaluated. That is, I have > printed > $outtype, and it has a valid value, but the second method which > matches the > value never runs. If I comment out the 'if' and run the two methods > manually > it works fine. > > In my tests $outtype's value is 'mp3', so why doesn't song.wavToMp3 > run? > What am I missing? I'm not able to generalise this result, my similar code (at bottom) works as it should. So, at a guess, I'd suggest that $outtype is not actually "mp3" but is "mp3\n". #puts is smart enough not to include an extra \n, and if you're using #print, then the extra line break may not be obvious, since it would look just like the result of #puts. Similarly, "mp3 " wouldn't display as obviously wrong. For example: irb(main):020:0> foo = "mp3\n" irb(main):021:0> puts foo.inspect "mp3\n" => nil irb(main):022:0> puts foo mp3 => nil irb(main):023:0> foo == 'mp3' => false If that's not the case, then I'm stumped. matthew smillie. [this code works] def foobar type = 'ogg' case type when 'ogg' puts "*always" puts "*ogg" if $outtype == 'ogg' puts "*mp3" if $outtype == 'mp3' end end => nil foobar *always *mp3 => nil