# Simple question: compare words

**URL:** <https://www.ruby-forum.com/t/simple-question-compare-words/175710>\
**Category:** Ruby\
**Created:** [September 18, 2009, 7:14am UTC](https://www.ruby-forum.com/t/simple-question-compare-words/175710 "2009-09-18T07:14:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [September 18, 2009, 7:14am UTC](https://www.ruby-forum.com/t/simple-question-compare-words/175710/1 "2009-09-18T07:14:41Z")

</div>

I want to compare same words in a and b then,  
I wrote this,

$ye  
$bu  
a = “ali”, “bel”, “del”,“en”  
b = “ali”,“bas”,“del”,“sx”,“en”,“sz”,“zen”

for xa in a  
$ye= xa  
end  
b.each do |bx|  
$bu = bx  
if $ye == $bu  
puts “#{$ye} and #{$bu}”  
end  
end

result:  
=\> en and en

but result is only “en and en”. why the last one?  
where is the others ali and del???

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [September 18, 2009, 8:03am UTC](https://www.ruby-forum.com/t/simple-question-compare-words/175710/2 "2009-09-18T08:03:56Z")

</div>

Ahmet K. wrote:

> for xa in a  
> $ye= xa  
> end

That is the equivalent to:

$ye = “ali”  
$ye = “bel”  
$ye = “del”  
$ye = “en”

which gives you:

puts $ye

–output:–  
“en”

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [September 18, 2009, 8:11am UTC](https://www.ruby-forum.com/t/simple-question-compare-words/175710/3 "2009-09-18T08:11:14Z")

</div>

On Fri, Sep 18, 2009 at 2:14 PM, Ahmet K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## end but result is only “en and en”. why the last one? where is the others ali and del???

Maybe this will do what you want.

a = [“ali”, “bel”, “del”,“en”]  
b = [“ali”,“bas”,“del”,“sx”,“en”,“sz”,“zen”]

p a&b

But, to kind of answer your question,

a = [“ali”, “bel”, “del”,“en”]  
b = [“ali”,“bas”,“del”,“sx”,“en”,“sz”,“zen”]

for xa in a  
$ye= xa  
end

puts $ye #Look here.

p $ye  
b.each do |bx|  
$bu = bx

puts $ye #And look here.

if $ye == $bu  
puts “#{$ye} and #{$bu}”  
end  
end

Harry

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [September 18, 2009, 8:28am UTC](https://www.ruby-forum.com/t/simple-question-compare-words/175710/4 "2009-09-18T08:28:06Z")

</div>

Try adding puts $ye after for loop to see what value it contains.

for xa in a  
$ye= xa  
end  
puts $ye

This should give you a clue to your problem.

Then look at the documentation on Ruby arrays class - Ruby provides lots  
of lovely ways of processing arrays.

for example try  
array & other\_array

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [September 18, 2009, 8:13am UTC](https://www.ruby-forum.com/t/simple-question-compare-words/175710/5 "2009-09-18T08:13:35Z")

</div>

> end  
> but result is only “en and en”. why the last one?  
> where is the others ali and del???

You get only en, because by the end of this loop:

> for xa in a  
> $ye= xa  
> end

you have the last item (“en”) in a assigned to $ye and then continue  
to compare this last value to the b.  
No wonder it finds only “en”.

In Ruby you can find common element in array with &:

> > a = %w{ali bel del en}  
> > =\> [“ali”, “bel”, “del”, “en”]

> > b = %w{ali bas del sx en sz zen}  
> > =\> [“ali”, “bas”, “del”, “sx”, “en”, “sz”, “zen”]

> > a & b  
> > =\> [“ali”, “del”, “en”]

Regards,  
Rimantas

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [September 18, 2009, 10:51am UTC](https://www.ruby-forum.com/t/simple-question-compare-words/175710/6 "2009-09-18T10:51:40Z")

</div>

On Fri, Sep 18, 2009 at 4:08 PM, Ahmet K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > $ye = “del”  
> > $ye = “en”
> > 
> > which gives you:
> > 
> > puts $ye
> > 
> > –output:–  
> > “en”
> 
> Yes, I want that.

No, you don’t. 🙂

You go through all of them but you don’t do anything with them.  
Then you compare only the last one to the elements in the other array.

Harry

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [September 18, 2009, 9:07am UTC](https://www.ruby-forum.com/t/simple-question-compare-words/175710/7 "2009-09-18T09:07:22Z")

</div>

7stud – wrote:

> Ahmet K. wrote:
> 
> > for xa in a  
> > $ye= xa  
> > end
> 
> That is the equivalent to:
> 
> $ye = “ali”  
> $ye = “bel”  
> $ye = “del”  
> $ye = “en”
> 
> which gives you:
> 
> puts $ye
> 
> –output:–  
> “en”

Yes, I want that. I want to compare them by one by with other words.  
Also, It is not Array it is String.  
because if I use $ye.class it gives me String  
and I got this error. undefined method `&’ for “en”:String
