From: s@... (Stefan Schmiedl) Date: 2002-04-22T05:52:37+09:00 Subject: Re: Novice: Each and p On Sun, 21 Apr 2002 19:51:20 GMT, Thorsten Haude wrote: > #! /usr/bin/ruby -w > # > # This is free software; you can redistribute and/or modify it under the > # the terms of version 2 the GNU General Public License as published by > # the Free Software Foundation. See http://www.gnu.org/copyleft/gpl.html > > File.open("/home/yooden/.mail/keywords") { |file| > file.each_line { |line| > print line if line =~ /Ruby/ > end > } > - - - Schnapp - - - > I get a parse error at line ten. This is an example from your book, so > I'm afraid something is wrong with my setup. Hi Thorsten. This proves that 20 eyes see more than the 18 (presumably) which were employed for proofreading. You are doing fine, we have a typo. replace the single "end" with a single "}" and it should run. Or change the "{" after "each_line" to "begin". > ruby 1.6.3 (2001-03-19) [i686-linux] > Is there a standard way to verify a sane environment? if in doubt, use the windows way: reinstall :-) > >>About "each" look at >>http://www.approximity.com/rubybuch2/node24_main.html >>and especially read "Iteratoren". > The problem I had was more in the two |'s. What exactly is the meaning > of these? Do they just mean "Here's the name of whatever I iterate > upon"? Is there any side effect, anything more to know? > The reason I wonder is simply that this looks unusual to me. each_line yields one value to the code block, and you choose to denote it by "line" inside your block. There is another iterator "each_with_index" for sequences, where the block receives two values, which you could denote "| value, index |" or "| index, value |", I keep forgetting which one is correct. You might also want to check out the German Ruby list: http://www.unix-ag.uni-siegen.de/mailman/listinfo/ruby-de/ Thanks for finding the typo. S. -- Stefan Schmiedl Approximity GmbH http://www.approximity.com Research & Development mailto:stefan@approximity.com Loreleystr. 5, 94315 Straubing, Germany Tel. (0 94 21) 74 01 06, Fax (0 94 21) 74 01 21 shhhh ... I can't hear my code!