From: Mike Stok Date: 2002-05-30T06:03:32+09:00 Subject: Re: Regexp in ranges as control In article <3cf53223@news.mhogaming.com>, Sean Russell wrote: > I've always liked the syntax: > > while gets > print if /start/../end/ > end > > However, on a recent project, I tried to extend this concept: > > lines.readlines("somefile").each do |line| > puts line if line === /start/../end/ > end > > This doesn't work, and I don't know why. I can /get/ it to work, with > > lines.readlines("somefile").each do |$_| > puts $_ if /start/../end/ > end This is just like the perl... "preamble blah start sdfas asdfas asdfasdf asdfasf end postamble".each { |line| puts line if line =~ /start/ .. line =~ /end/ } makes the variable explicit and works as expected. > but I have to say that this really, really makes me uncomfortable. Do I > /have/ to use the global variable $_ just to use regexps as switches? Why? > You don't need them for other ranges. For example, the following works: > > [1,2,3,4,5,6].each do |x| > puts x if x === 3..5 > end Or [1,2,3,4,5,6].each do |x| puts x if (3..5).include? x end A better explaniation would be forthcoming were I not busy. Hope this helps, Mike -- mike@stok.co.uk | The "`Stok' disclaimers" apply. http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60 http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA