[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Getting the version number of the ruby interpreter

William James

1/21/2005 9:08:00 PM


Trevor Wennblom wrote:
>
> http://www.zenspider.com/Languages/Ruby/Qui...


That site has this:

# prints lines starting at 'start' and ending at 'end'
while gets
print if /start/../end/
end

Is that correct?

4 Answers

Nicholas Van Weerdenburg

1/21/2005 9:42:00 PM

0

That's no longer supported as of 1.8, but unfortunately gives no
error, according to page 64 of PickAxe2.

while line = gets
puts line if line =~ /start/ .. line =~ /end/
end

is the correct form now.

Note: gets and print still operate magically on $_ in 1.8.2 but I'm
guessing / / doesn't, and hence that example no longer working.

More in line with the original example, I'd guess that this also works:

while gets
print if $_ =~ /start/ .. $_ =~ /end/
end
# $_ implicit for gets and print

Regards,
Nick
--
Nicholas Van Weerdenburg

On Sat, 22 Jan 2005 06:10:58 +0900, William James <w_a_x_man@yahoo.com> wrote:
>
> Trevor Wennblom wrote:
> >
> > http://www.zenspider.com/Languages/Ruby/Qui...
>
>
> That site has this:
>
> # prints lines starting at 'start' and ending at 'end'
> while gets
> print if /start/../end/
> end
>
> Is that correct?
>
>


Nicholas Van Weerdenburg

1/21/2005 10:13:00 PM

0

Did some testing, / / will operate on $_ implicitly, so that's not why
it no longer works as it did.

e.g.
while gets
print if /start/
end

will print the line containing start.

So I'm guessing that / / changed what it returns (Matchdata or nil)
compared to what it used to return prior to 1,8 (Fixnum or nil) and
that breaks using it by itself with ranges.

Of course, this is all considered bad form in Ruby now anyhow. Still,
it's very nice for command-line 1 lines ( ruby -n -e 'print if /ruby/'
input.txt )

Nick

On Fri, 21 Jan 2005 16:41:55 -0500, Nicholas Van Weerdenburg
<vanweerd@gmail.com> wrote:
> That's no longer supported as of 1.8, but unfortunately gives no
> error, according to page 64 of PickAxe2.
>
> while line = gets
> puts line if line =~ /start/ .. line =~ /end/
> end
>
> is the correct form now.
>
> Note: gets and print still operate magically on $_ in 1.8.2 but I'm
> guessing / / doesn't, and hence that example no longer working.
>
> More in line with the original example, I'd guess that this also works:
>
> while gets
> print if $_ =~ /start/ .. $_ =~ /end/
> end
> # $_ implicit for gets and print
>
> Regards,
> Nick
> --
> Nicholas Van Weerdenburg
>
> On Sat, 22 Jan 2005 06:10:58 +0900, William James <w_a_x_man@yahoo.com> wrote:
> >
> > Trevor Wennblom wrote:
> > >
> > > http://www.zenspider.com/Languages/Ruby/Qui...
> >
> >
> > That site has this:
> >
> > # prints lines starting at 'start' and ending at 'end'
> > while gets
> > print if /start/../end/
> > end
> >
> > Is that correct?
> >
> >
>


--
Nicholas Van Weerdenburg


James Gray

1/21/2005 10:27:00 PM

0

On Jan 21, 2005, at 4:13 PM, Nicholas Van Weerdenburg wrote:

> Of course, this is all considered bad form in Ruby now anyhow. Still,
> it's very nice for command-line 1 lines ( ruby -n -e 'print if /ruby/'
> input.txt )

Which is exactly why the old behavior still works there. ;)

james% cat test.txt
start
middle
end
james% ruby -n -e 'print unless /middle/' test.txt
start
end

James Edward Gray II



William James

1/22/2005 12:48:00 AM

0


Nicholas Van Weerdenburg wrote:
>
> while gets
> print if $_ =~ /start/ .. $_ =~ /end/
> end


That works. The equivalent Awk program is:

/start/,/end/

An Awk program to skip blank lines at the beginning of the file
(including lines containing only spaces)
and to print the rest of the lines:

NF,0