Comining Arrays

P

Paul Sholtz

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?
 
A

Adam Prescott

[Note: parts of this message were removed to make it a legal post.]

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.
 
D

David J.Hamilton

Excerpts from Paul Sholtz'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]]
 
R

RichardOnRails

Excerpts from Paul Sholtz'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]]

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
 
S

Sam Duncan

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
 
S

Sam Duncan

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

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
 
R

RichardOnRails

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 01/03/11 16:45, RichardOnRails 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
 
S

Sam Duncan

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
 
R

RichardOnRails

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top