Comining Arrays

I have the following code:

http://www.pastie.org/1618151

It’s a routine from a simple crypto algo, where I’m basically combining
two arrays of data (a “sanitized” stream of data to encrypt, and a
“keystream” to obfuscate the data).

The “pattern” I’m using to combine the streams is a bit ugly, and this
is something I’ve run up against in many other projects as well.

That is, I’m stepping through one array, and then I’m “counting” to keep
track of where I am in the other array, so as to “add” the two arrays.

I know (before calling the routine) that both arrays are of the same
size, and that elements of the corresponding arrays will be added
together.

Is there a way to make this routine prettier? More Ruby-ific?

On Mon, Feb 28, 2011 at 8:31 PM, Paul S. [email protected]
wrote:

That is, I’m stepping through one array, and then I’m “counting” to keep
track of where I am in the other array, so as to “add” the two arrays.

I know (before calling the routine) that both arrays are of the same
size, and that elements of the corresponding arrays will be added
together.

I think what you want is Enumerable#zip.

Excerpts from Paul S.'s message of Mon Feb 28 12:31:12 -0800 2011:

That is, I’m stepping through one array, and then I’m “counting” to keep
track of where I am in the other array, so as to “add” the two arrays.

I know (before calling the routine) that both arrays are of the same
size, and that elements of the corresponding arrays will be added
together.

Sounds like you want zip.

%w( a b c ).zip( 1…3 ).each {|l, d| puts “# l:#{l}, d:#{d}” }

l:a, d:1

l:b, d:2

l:c, d:3

=> [[“a”, 1], [“b”, 2], [“c”, 3]]

On 01/03/11 16:45, RichardOnRails wrote:

Hey David,

Neat code, but I don’t get it.

%w( a b c ) yields an instance of class String.

ruby-1.9.2-p0 > %w( a b c )
=> [“a”, “b”, “c”]
ruby-1.9.2-p0 > %w( a b c ).kind_of?(String)
=> false
ruby-1.9.2-p0 > %w( a b c ).kind_of?(Array)
=> true

On Feb 28, 4:46pm, “David J.Hamilton” [email protected] wrote:

%w( a b c ).zip( 1…3 ).each {|l, d| puts “# l:#{l}, d:#{d}” }

l:a, d:1

l:b, d:2

l:c, d:3

=> [[“a”, 1], [“b”, 2], [“c”, 3]]


med vnlig hlsning
David J. Hamilton

Hey David,

Neat code, but I don’t get it.

%w( a b c ) yields an instance of class String.

%w( a b c ).zip( 1…3 ) invokes the ostensible zip method of the
String class with a Range instance as an argument. But String has no
zip method.

So I misunderstand something about writing Ruby. Would you be so kind
as to enlighten me.

Thanks in Advance,
Richard

And just to be sure, with 1.8.7 …

irb(main):001:0> RUBY_VERSION
=> “1.8.7”
irb(main):002:0> %w( a b c )
=> [“a”, “b”, “c”]
irb(main):003:0> %w( a b c ).kind_of?(String)
=> false
irb(main):004:0> %w( a b c ).kind_of?(Array)
=> true

On Feb 28, 11:05pm, Sam D. [email protected] wrote:

ruby-1.9.2-p0 > %w( a b c )
=> [“a”, “b”, “c”]
ruby-1.9.2-p0 > %w( a b c ).kind_of?(String)
=> false
ruby-1.9.2-p0 > %w( a b c ).kind_of?(Array)
=> true

Hi Sam,

Thanks. My mind stopped this the thought that %w creates strings out
of bare words. I forgot that those strings end up in an array. I
should have checked that my stupid claim was correct before posting.
So, with an apology to you and the newsgroup,

Best wishes,
Richard

On Mar 1, 1:51pm, Sam D. [email protected] wrote:

So, with an apology to you and the newsgroup,

Best wishes,
Richard

I certainly hope I’m not expected to apologise every time I don’t follow
something. The list is, after all, something to learn from.

Thanks, Sam. It’s nice to see a gracious response!

For a while back I received a lot of guff because I wrote and then
posted Ruby code in a style that originated with a big gun at
Microsoft about two decades ago, e.g. sFirstName containing a
reference to a string. Instead of addressing the substance of my
question, they thought it important to hector me for code that lives
quietly and inconspicuously on my computer. It reminded me of Salem,
Mass in the 1600s.

In light of that experience some months ago, your response is a
refreshing indeed.

Best wishes,
Richard

No need to apologise! I had to check myself =]

I certainly hope I’m not expected to apologise every time I don’t follow
something. The list is, after all, something to learn from.

Sam

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs