From: Rimantas Liubertas Date: 2009-09-18T15:13:05+09:00 Subject: Re: simple question: compare words > 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??? 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 -- http://rimantas.com/