Forum: Ruby Getting rid of a while loop

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4b781b6ab8600b559b8d729f423db26e?d=identicon&s=25 Tait Pollard (customsgod)
on 2006-06-07 14:53
I get a feeling there should be another way to make this happen without
using a while loop, if anyone has any ideas that'd be great.

    while question=file.gets
      answer=file.gets
      @question_array[counter]=Questions.new(question,answer, false)
      counter+=1
    end
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-06-07 15:22
(Received via mailing list)
On Jun 7, 2006, at 7:53 AM, Tait Pollard wrote:

> I get a feeling there should be another way to make this happen
> without
> using a while loop, if anyone has any ideas that'd be great.
>
>     while question=file.gets
>       answer=file.gets
>       @question_array[counter]=Questions.new(question,answer, false)
>       counter+=1
>     end

One way would be:

require "enumerator"

# ...

file.each_slice(2) do |question, answer|
   @question_array << Questions.new(question, answer, false)
end

Hope that helps.

James Edward Gray II
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2006-06-07 15:49
(Received via mailing list)
2006/6/7, James Edward Gray II <james@grayproductions.net>:
> >     end
>
> One way would be:
>
> require "enumerator"
>
> # ...
>
> file.each_slice(2) do |question, answer|
>    @question_array << Questions.new(question, answer, false)
> end

Thanks for leaving the inject version for me. :-)

@question_array = file.to_enum(:each_slice, 2).inject([]) do |qa,(quest,
answ)|
  qa << Questions.new(quest, answ, false)
end

:-))

robert
05be5d6610e2c3f1780aa0e39e902e93?d=identicon&s=25 Farrel Lifson (Guest)
on 2006-06-07 15:54
(Received via mailing list)
while (questiom,answer = file.gets,file.gets) ?

Farrel
87e9a89c53ccf984db792113471c2171?d=identicon&s=25 Kroeger, Simon (ext) (Guest)
on 2006-06-07 16:59
(Received via mailing list)
> > > without
> >
> @question_array = file.to_enum(:each_slice, 2).inject([]) do
> |qa,(quest, answ)|
>   qa << Questions.new(quest, answ, false)
> end
>
> :-))
>
> robert

hehe, nice, with 1.8.4

@question_array = file.enum_slice(2).map{|q, a| Questions.new(q, a,
false)}

should be possible, isn't it?

cheers

Simon
36958dd94ca666a38483df282a5214d5?d=identicon&s=25 Peter Ertl (Guest)
on 2006-06-07 17:59
(Received via mailing list)
this is _really_ strange!


d:\workspace>ruby -v
ruby 1.8.4 (2005-12-24) [i386-mswin32]

irb(main):009:0> [1,2,3].methods.grep(/slice/)
=> ["slice", "slice!"]

the methods 'each_slice' and 'enum_slice' do not exist...

is this a bug of the windows build?
23172b6630dc631a134c9bad2fec2a39?d=identicon&s=25 Chris (Guest)
on 2006-06-07 18:16
you forgot to
require 'enumerator'


cheers
36958dd94ca666a38483df282a5214d5?d=identicon&s=25 Pete (Guest)
on 2006-06-07 21:48
(Received via mailing list)
Chris schrieb:
> you forgot to
> require 'enumerator'
>
>
> cheers
>
>
aaahhhh :-))

thanks so much!!
This topic is locked and can not be replied to.