2010/2/11 Aldric Giacomoni <aldric / trevoke.net>:
> John Ydil wrote:
>>
>> File.foreach("/etc/raddb/users").each_slice 4 do |name, ip, dump, table|
>> =A0 =A0 =A0 =A0 puts name.strip!
>> =A0 =A0 =A0 =A0 puts ip.strip!
>> =A0 =A0 =A0 =A0 puts table.strip!
>> end
>>
>> here is the file /etc/raddb/users
>>
>> login Auth-Type :=3DLocal, User-Password =3D=3D "bonjour"
>> =A0 =A0 =A0 =A0 Framed-IP-Address =3D ipaddress,
>> =A0 =A0 =A0 =A0 Fall-Through =3D No
>> =A0 =A0 =A0 =A0 # table 254
>>
>>
>> Here is the error :
>> ./test.rb:12:in `foreach': no block given (LocalJumpError)
>> =A0 =A0 =A0 =A0 from ./test.rb:12
>>
>> I wonder if it's due to the difference between the 2 versions, Do you
>> have an idea ?
>
> Well, for this one, a quick look at the documentation will give you the
> answer:
> http://ruby-doc.org/core/classes/IO.html#M002243
>
> "foreach" expects a block. So you can't really do "each slice" on
> "foreach" since you should do "foreach" on ... er ... each line in the
> file.
> Probably you'll do the foreach, and a slice on that line within the code
> block instead.

He's on 1.8.2.  There you have to do

require 'enumerator'

File.to_enum(:foreach, "/etc/raddb/users").each_slice 4 do |name, ip,
dump, table|
       puts name.strip!
       puts ip.strip!
       puts table.strip!
end

The convenient feature to return an Enumerator from enumerating
methods which expect a block but do not receive it was added in 1.9
and backported to 1.8.7.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/