'each' returning array

A newbie problem:

Is there a way to transparently accumulate expresions evaluated within
a block into an array, without introducing an external variable? For
example, I’d like to write something like this:

“HAL”.each_byte__returning_array {|x| (x+1).chr} .collect # more processing

…instead of this:

l=[]; “HAL”.each_byte {|x| l<< (x+1).chr}; l.collect #…

One of my first impressions with ruby was the surprise that each*
method family doesn’t return such accumulated arrays just for method
chaining.

Am I thinking totally non-rubyish? :wink: If so, please show me the Right
Way…

···


He who foos last foos best

Would Array#map do what you want?

%w{ H A L }
=> [“H”, “A”, “L”]
%w{ H A L }.map {|x| (x[0]+1).chr}
=> [“I”, “B”, “M”]

···

On Fri, Feb 28, 2003 at 06:01:31AM +0900, Wojciech Kaczmarek wrote:

A newbie problem:

Is there a way to transparently accumulate expresions evaluated within
a block into an array, without introducing an external variable? For
example, I’d like to write something like this:

“HAL”.each_byte__returning_array {|x| (x+1).chr} .collect # more processing

…instead of this:

l=; “HAL”.each_byte {|x| l<< (x+1).chr}; l.collect #…

One of my first impressions with ruby was the surprise that each*
method family doesn’t return such accumulated arrays just for method
chaining.

Am I thinking totally non-rubyish? :wink: If so, please show me the Right
Way…


He who foos last foos best


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

Hi,

···

At Fri, 28 Feb 2003 06:01:31 +0900, Wojciech Kaczmarek wrote:

Is there a way to transparently accumulate expresions evaluated within
a block into an array, without introducing an external variable? For
example, I’d like to write something like this:

“HAL”.each_byte__returning_array {|x| (x+1).chr} .collect # more processing

Enumerator in “rough”.

require “enumerator”
“HAL”.enum_for(:each_byte).collect {|x| (x+1).chr} # => [“I”, “B”, “M”]


Nobu Nakada

how have i lived so long without knowing that!

can you explain more?

-a

···

On Fri, 28 Feb 2003 nobu.nokada@softhome.net wrote:

Hi,

At Fri, 28 Feb 2003 06:01:31 +0900, > Wojciech Kaczmarek wrote:

Is there a way to transparently accumulate expresions evaluated within
a block into an array, without introducing an external variable? For
example, I’d like to write something like this:

“HAL”.each_byte__returning_array {|x| (x+1).chr} .collect # more processing

Enumerator in “rough”.

require “enumerator”
“HAL”.enum_for(:each_byte).collect {|x| (x+1).chr} # => [“I”, “B”, “M”]

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

In this case: yes, or more general (not involving %w-style literal
array): aString.unpack(“C*”).map {…}

…but that was only an example. The heart of the matter is that I want
to chain methods like this:

anArray.method1{block1}.method2{block2}

Well, I can just write a general mixin with something like ‘each’ but
with accumulating block data into an array. I wondered whether is it
done in builtin stuff.

···

On Fri, 28 Feb 2003 06:08:17 +0900, Daniel Carrera wrote:

Is there a way to transparently accumulate expresions evaluated within
a block into an array, without introducing an external variable? For
example, I’d like to write something like this:

“HAL”.each_byte__returning_array {|x| (x+1).chr} .collect # more processing

…instead of this:

l=; “HAL”.each_byte {|x| l<< (x+1).chr}; l.collect #…

One of my first impressions with ruby was the surprise that each*
method family doesn’t return such accumulated arrays just for method
chaining.

Would Array#map do what you want?

%w{ H A L }
=> [“H”, “A”, “L”]
%w{ H A L }.map {|x| (x[0]+1).chr}
=> [“I”, “B”, “M”]


We demand rigidly defined areas of doubt and uncertainty!

Is there a way to transparently accumulate expresions evaluated within
a block into an array, without introducing an external variable? For
example, I’d like to write something like this:

“HAL”.each_byte__returning_array {|x| (x+1).chr} .collect # more processing

Finally I’ve written a 5-lines long mixin, …

Enumerator in “rough”.

require “enumerator”
“HAL”.enum_for(:each_byte).collect {|x| (x+1).chr} # => [“I”, “B”, “M”]

…but this module is great, nice that it’s going to be included in
ruby distro. Thanks!

···

On Fri, 28 Feb 2003 06:39:45 +0900, nobu.nokada@softhome.net wrote:


This is not a healthy way of thinking.
(Emacs Psychiatrist)

Hi,

Enumerator in “rough”.

require “enumerator”
“HAL”.enum_for(:each_byte).collect {|x| (x+1).chr} # => [“I”, “B”, “M”]

how have i lived so long without knowing that!

can you explain more?

$ cvs -d :pserver:anonymous@cvs.ruby-lang.org:/src co rough/ext/enumerator

from enumerator.txt:

···

At Fri, 28 Feb 2003 07:01:47 +0900, ahoward wrote:

** Enumerable::Enumerator(Class)

A class which provides a method `each’ to be used as an Enumerable
object.


Nobu Nakada