D
Dennis Roberts
Hi I am new to Ruby and I am working on my first program.
I have two arrays, I want to see the difference between them. I was
thinking of ary1 - ary2 but sometimes it should be ary2 - ary1 and I am
not sure at run time which one will be different.
So then I thought okay I will check if they are the same and if they are
not then I will print both of them out side by side in report fashion.
So in a method I call aryDiffReport I set two variables: count which is
the length of the bigger array, and len which is the length of the
largest item in either array (for this I wrote my own method
*_longestItem - couldn't find anything built in to do it).
Using the two variables and sprintf I construct a third array that looks
something like the following when printed out:
one <-> one
two <-> two
a <-> b
<-> 12
Okay so my first thoughts is this will be real slow with big arrays.
Being new to Ruby I was thinking I could get some advice on what I am
trying to do. Is there a better way? I appreciate all feedback good or
bad.
* I kinda thought this was a neat little method so I decided to share
def _longestItem(ary1, ary2)
itemlen = 0
ary1.each do |i|
if i.length > itemlen
then
itemlen = i.length
end
end
ary2.each do |i|
if i.length > itemlen
then
itemlen = i.length
end
end
return itemlen
end
I have two arrays, I want to see the difference between them. I was
thinking of ary1 - ary2 but sometimes it should be ary2 - ary1 and I am
not sure at run time which one will be different.
So then I thought okay I will check if they are the same and if they are
not then I will print both of them out side by side in report fashion.
So in a method I call aryDiffReport I set two variables: count which is
the length of the bigger array, and len which is the length of the
largest item in either array (for this I wrote my own method
*_longestItem - couldn't find anything built in to do it).
Using the two variables and sprintf I construct a third array that looks
something like the following when printed out:
one <-> one
two <-> two
a <-> b
<-> 12
Okay so my first thoughts is this will be real slow with big arrays.
Being new to Ruby I was thinking I could get some advice on what I am
trying to do. Is there a better way? I appreciate all feedback good or
bad.
* I kinda thought this was a neat little method so I decided to share
def _longestItem(ary1, ary2)
itemlen = 0
ary1.each do |i|
if i.length > itemlen
then
itemlen = i.length
end
end
ary2.each do |i|
if i.length > itemlen
then
itemlen = i.length
end
end
return itemlen
end