On 3/3/07, Robert Klemme <shortcutter / googlemail.com> wrote:
> On 03.03.2007 09:00, Robert Dober wrote:
> > On 3/3/07, ara.t.howard / noaa.gov <ara.t.howard / noaa.gov> wrote:
> >> On Sat, 3 Mar 2007, Yannick Grams wrote:
> >>
> >> > Hello all!
> >> >
> >> > I'm fairly new to Ruby, and I'm trying to write a program that looks at
> >> > each character of a string and then processes it using a block. I've
> >> > been using:
> >> >
> >> > String.each do
> >> >   #block
> >> > end
> >> >
> >> > but something isn't working. I'm sure that there is a simple answer,
> >> but
> >> > I'm not that experienced with the language. If someone could please
> >> help
> >> > me out, I'd greatly appreciate it.
> >> >
> >> > --
> >> > Posted via http://www.ruby-forum.com/.
> >>
> >> harp: ~> ri String#each
> >> ------------------------------------------------------------ String#each
> >>       str.each(separator=$/) {|substr| block }        => str
> >>       str.each_line(separator=$/) {|substr| block }   => str
> >> ------------------------------------------------------------------------
> >>       Splits _str_ using the supplied parameter as the record separator
> >>       (+$/+ by default), passing each substring in turn to the supplied
> >>       block. If a zero-length record separator is supplied, the string is
> >>       split on +\n+ characters, except that multiple successive newlines
> >>       are appended together.
> >>
> >>          print "Example one\n"
> >>          "hello\nworld".each {|s| p s}
> >>          print "Example two\n"
> >>          "hello\nworld".each('l') {|s| p s}
> >>          print "Example three\n"
> >>          "hello\n\n\nworld".each('') {|s| p s}
> >>
> >>       _produces:_
> >>
> >>          Example one
> >>          "hello\n"
> >>          "world"
> >>          Example two
> >>          "hel"
> >>          "l"
> >>          "o\nworl"
> >>          "d"
> >>          Example three
> >>          "hello\n\n\n"
> >>          "world"
> >>
> >>
> >> harp:~ > ruby -e'  puts String.instance_methods.grep(/each/)  '
> >> each
> >> each_with_index
> >> each_line
> >> each_byte
> >>
> >>
> >> harp:~ > ruby -e'  "foobar".each_byte{|b| p b}  '
> >> 102
> >> 111
> >> 111
> >> 98
> >> 97
> >> 114
> >>
> >>
> >> harp:~ > ruby -e'  "foobar".each_byte{|b| p b.chr}  '
> >> "f"
> >> "o"
> >> "o"
> >> "b"
> >> "a"
> >> "r"
> >>
> >>
> >> -a
> >> --
> >> be kind whenever possible... it is always possible.
> >> - the dalai lama
> >>
> >>
> >
> > I'd like to add two remarks
> > (1) ruby -e'  "foobar".split("").each{|b| p b} '
> > and
> > (2) I feel it is a pity that
> > s.each("") is not the same as s.split("").each
> > and
>
> Yeah, String's enumeration is a bit weird and inconsistent.  Using a
> String as array of lines does have it's uses at times but I wonder
> whether changing #each to return characters would be more useful (apart
> from breaking existing code).
>
> > (3)
> > "foobar".to_a does not deliver "foobar".split(""). The Arrayness of
> > String might even indicate that String#to_a return an array of bytes
> > as delivered by #[index]?
> > Note that the easiest way to do this ( which I found ) was
> >
> > x=[]; each_byte{ |b| x << b}; x
>
> There's also
>
> irb(main):014:0> require 'enumerator'
> => true
> irb(main):015:0> "foobar".to_enum(:each_byte).to_a
> => [102, 111, 111, 98, 97, 114]
>
Thx Robert,
when will I ever know the whole Standard API???

Robert
> Kind regards
>
>         robert
>
>


-- 
We have not succeeded in answering all of our questions.
In fact, in some ways, we are more confused than ever.
But we feel we are confused on a higher level and about more important things.
-Anonymous