From: Robert Klemme Date: 2013-06-24T05:03:00+09:00 Subject: Re: Error in xoring elements --089e0141a95e4fae3e04dfd7c8b5 Content-Type: text/plain; charset=ISO-8859-1 On Sun, Jun 23, 2013 at 9:45 PM, buzz k. wrote: > Hey guys, > I am having an issue in working on ruby arrays. > Firstly suppose i have a = [1,2,3] and b [4,5,6] and c=[] > Did you mean b = [4,5,6] > i get an error for this code:- > for i in 0..2 > c[i] = a[i]*b[i] > end > > saying []= cant be applied to fixnum > Certainly not irb(main):006:0> a = [1,2,3] => [1, 2, 3] irb(main):007:0> b = [4,5,6] => [4, 5, 6] irb(main):008:0> c = [] => [] irb(main):009:0> for i in 0..2; c[i] = a[i] * b[i] end => 0..2 irb(main):010:0> c => [4, 10, 18] Btw, you can also do irb(main):012:0> c = a.zip(b).map {|x,y| x * y} => [4, 10, 18] Anyways ultimately i solved this error using the << operator > However now i am getting an error :undefined method `^' for [1]:Array > when i apply > for i in 0..2 > c << a[i] ^ b[i] > end > i.e. i am trying to xor values of two arrays and store in a third array > Why the issue and if there is anyway of solving with the first method > itself > Precedence irb(main):013:0> c << a[1] ^ b[1] NoMethodError: undefined method `^' for [4, 10, 18, 2]:Array from (irb):13 from /usr/bin/irb:12:in `
' irb(main):014:0> c << (a[1] ^ b[1]) => [4, 10, 18, 2, 7] Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --089e0141a95e4fae3e04dfd7c8b5 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

--089e0141a95e4fae3e04dfd7c8b5--