Some noob questions

Robert Klemme wrote:

···

2010/2/11 John Ydil <john.gendrot@cnsi.fr>:

"foreach" since you should do "foreach" on ... er ... each line in the

� � � �puts name.strip!

� � � �from ./test.rb:5

Maybe I haven't the full ruby because it's a small linux...

Oooops! Sorry for the wrong advice. Either that or Enumerator did
not exist in 1.8.2 (which is ancient btw - I cannot remember having
used it).

Kind regards

robert

So there is no way i can do that in 1.8.2 ?
--
Posted via http://www.ruby-forum.com/\.

You can write Enumerator yourself. It isn't too hard

Enum = Struct.new :inst, :args do
  include Enumerable

  def each(&b)
    inst.send(*args, &b)
    self
  end
end

class Object
  def to_enum(*a)
    Enum.new(self, a)
  end
end

:slight_smile:

Kind regards

robert

···

2010/2/12 John Ydil <john.gendrot@cnsi.fr>:

Robert Klemme wrote:

2010/2/11 John Ydil <john.gendrot@cnsi.fr>:

"foreach" since you should do "foreach" on ... er ... each line in the

� � � �puts name.strip!

� � � �from ./test.rb:5

Maybe I haven't the full ruby because it's a small linux...

Oooops! Sorry for the wrong advice. Either that or Enumerator did
not exist in 1.8.2 (which is ancient btw - I cannot remember having
used it).

Kind regards

robert

So there is no way i can do that in 1.8.2 ?

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

Robert Klemme wrote:

not exist in 1.8.2 (which is ancient btw - I cannot remember having
used it).

Kind regards

robert

So there is no way i can do that in 1.8.2 ?

You can write Enumerator yourself. It isn't too hard

Enum = Struct.new :inst, :args do
  include Enumerable

  def each(&b)
    inst.send(*args, &b)
    self
  end
end

class Object
  def to_enum(*a)
    Enum.new(self, a)
  end
end

:slight_smile:

Kind regards

robert

Thanks again Robert but (there is always a BUT)....

I copy paste the code you gave me

Enum = Struct.new :inst, :args do
  include Enumerable

  def each(&b)
    inst.send(*args, &b)
    self
  end
end

class Object
  def to_enum(*a)
    Enum.new(self, a)
  end
end
        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

./test.rb:31: undefined method `each_slice' for #<struct Enum inst=File,
args=[:foreach, "/etc/raddb/users"]> (NoMethodError)

I think we progress....

···

2010/2/12 John Ydil <john.gendrot@cnsi.fr>:

--
Posted via http://www.ruby-forum.com/\.

Slowly.... I believe #each_slice was added later as well. Oh well.
Can't you just upgrade your Ruby version? That seems a much better
option than trying to retrofit all the new behavior on the old
version.

Kind regards

robert

···

2010/2/12 John Ydil <john.gendrot@cnsi.fr>:

Thanks again Robert but (there is always a BUT)....

I copy paste the code you gave me

   File\.to\_enum\(:foreach, &quot;/etc/raddb/users&quot;\)\.each\_slice 4 do

>name, ip, dump, table|
puts name.strip!
puts ip.strip!
puts table.strip!
end

./test.rb:31: undefined method `each_slice' for #<struct Enum inst=File,
args=[:foreach, "/etc/raddb/users"]> (NoMethodError)

I think we progress....

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

Slowly.... I believe #each_slice was added later as well. Oh well.
Can't you just upgrade your Ruby version? That seems a much better
option than trying to retrofit all the new behavior on the old
version.

Kind regards

robert

I'll try to upgrade.

···

--
Posted via http://www.ruby-forum.com/\.

John Ydil wrote:

Slowly.... I believe #each_slice was added later as well. Oh well.
Can't you just upgrade your Ruby version? That seems a much better
option than trying to retrofit all the new behavior on the old
version.

Kind regards

robert

I'll try to upgrade.

Unfortunately, the provider doesn't have a newer version of ruby :s

···

--
Posted via http://www.ruby-forum.com/\.

module Enumerable
  def each_slice(num)
    sl =

    each do |x|
      sl << x

      if sl.size == num
        yield sl
        sl =
      end
    end

    yield sl unless sl.empty?

    self
  end
end

Kind regards

robert

···

2010/2/15 John Ydil <john.gendrot@cnsi.fr>:

John Ydil wrote:

Slowly.... I believe #each_slice was added later as well. Oh well.
Can't you just upgrade your Ruby version? That seems a much better
option than trying to retrofit all the new behavior on the old
version.

Kind regards

robert

I'll try to upgrade.

Unfortunately, the provider doesn't have a newer version of ruby :s

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

You are awesome !

···

--
Posted via http://www.ruby-forum.com/.

John Ydil wrote:

You are awesome !

Robert, indeed, is. Donations can be sent to
rk_is_our_idol@awesomeness.wow for attempts at cloning his awesomeness.

···

--
Posted via http://www.ruby-forum.com/\.