Match an ARray Element by if

Hi

bellow example :
a =Array.new
a = ['Cat','Dog']

if a.include? 'Dog1'
  puts "yes"
else
puts "no"

end

its works perfectly

But when i am trying to this

sssh = Net::SSH.start(@hostname2, @username, :password => @password)
                res2 = ssh.exec!(@cmd)
                ssh.close
                puts "2nd host"
                sname = Array.new
                sname << res2
               if (sname.include?'test1')
                puts "its has value"
                else
                puts "its does not have value"
                end
                puts sname

but this does not work

output :

its does not have value
test1
test2

Can any one please tell me what i am doing wrong ?
Thanks

···

--
Posted via http://www.ruby-forum.com/.

Hi,

It's probably a whitespace issue. Use "p sname" instead of "puts sname"
to see how the array elements actually look like.

And then you should strip the whitespace from the elements before you
check for elements:

sname.map(&:strip).include? 'test1'

By the way, leave out this "Array.new". It's completely useless in the
first case (since you immediately overwrite it) and can be replaced with
the literal "[]" in the second case.

···

--
Posted via http://www.ruby-forum.com/.

Hi thanks for the technique

["test1\n"]

so the array has a line carriage return

but the syntax
sname.map(&:strip).include? 'test1'

(&:strip) looks like does not work

···

--
Posted via http://www.ruby-forum.com/.

I have to revise my previous post:

Since sname is a completely new array, it cannot contain two strings
"test1" and "test2" but only one string consisting of two lines.
Something like

"test1\ntest2"

Which yields the question what you actually want: Split the string into
lines and then save these lines in an array? Or simply check if one of
the lines equals "test1"?

This would be

res2 =~ /^test1$/

···

--
Posted via http://www.ruby-forum.com/.

Hi just wondering if you can help me my bellow logic, as its very
related to this one

I Need a logic for the bellow problem:

I am running bellow script form a server,

Hostname :

Server-dk-2.domain.lan

And We have 2 other servers
Server-dh-2.domain
Server-dm-2.domin

Basically we have 3 server :
Server-dk-2.domain.lan
Server-dh-2.domain
Server-dm-2.domin

As you can see all same other then k,h,m

So I want to run the script into other 2 servers .

The logic is :
If the script runs from server-dk-2.domain.lan ,

It should check server-dh-2.domain and server-dm-2.domain.lan

I know how to connect to rest serve via ssh-net module

But don’t understand how will I implement the logic of connecting the
other 2 server

Current code is where I have defined the host by hand but it has to be
defined by its self

So the logic is : if the current server is
Server-dk-2.domain.lan , then those hostname1 and hostname2 will be
Server-dh-2.domain and server-dm-2.domain.lan

Or if current script running server is : Server-dh-2.domain the the
hostnae1 and hostname 2 would be Server-dk-2.domain.lan ,
server-dm-2.domain.lan

require 'net/ssh'

def name_check(name)

        count =0
#Assuing I am running the script from Server-dk-2.domain.lan
        @hostname1 = " Server-dh-2-domain "
        @hostname2 = " server-dm-2.domain.lan "
        @username = "test"
        @password = "test"
@cmd = "sudo ls /root/test/"
        begin
                ssh = Net::SSH.start(@hostname1, @username, :password =>
@password)
                res1 = ssh.exec!(@cmd)
                ssh.close
                sname = Array.new

                sname << res1
                sname.each { |item|
if (item =~ /#{name}/)
                              count = count +1

                               }

                ssh = Net::SSH.start(@hostname2, @username, :password =>
@password)
                res2 = ssh.exec!(@cmd)
                ssh.close
tname << res2
                tname.each { |item|
                        puts item
                        if (item =~ /#{name}/)
                                count = count +1
                                puts "its there"
                        else "No tiem"
                        end
                                                }
                if count >2
puts “We found two object”
                end
        rescue
        puts "Unable to connect to #{@hostname} using
#{@username}/#{@password}"

···

--
Posted via http://www.ruby-forum.com/.

res2 =~ /^test1$/

its works fine
sname << res1
                sname.each { |item|
                                               if (item =~ /#{name}/)
                            puts "its there"
                     else "No tiem"
                  end

What's the error message?

a = ['Cat','Dog']
#if a.include? 'Dog1'
if a.map(&:strip).include? 'Dog1'
  puts "yes"
else
puts "no"

end

array.rb:5: wrong argument type Symbol (expected Proc) (TypeError)

···

--
Posted via http://www.ruby-forum.com/\.

sharmin malik wrote in post #1075208:

res2 =~ /^test1$/

its works fine
sname << res1
                sname.each { |item|
                                               if (item =~ /#{name}/)
                            puts "its there"
                     else "No tiem"
                  end

This makes no sense. Again: Why do you even need an array when you only
got one string? At least that's the case in the original code.

If I understand you correctly, ssh.exec!(@cmd) returns a string that may
have multiple lines. To check if one of these lines is "test1", simply
use

res2 = ssh.exec! @cmd
if res2 =~ /^test1$/
  puts "its has value"
else
  puts "its does not have value"
end

The "sname" array has absolutely no purpose (unless you want to do
something different than your original code suggests).

What's the error message?

array.rb:5: wrong argument type Symbol (expected Proc) (TypeError)

Then I guess you use an obsolete Ruby version. Maybe 1.8.6? If that's
the case, update your Ruby.

···

--
Posted via http://www.ruby-forum.com/\.

ete Ruby version. Maybe 1.8.6? If that's

the case, update your Ruby.

yes, i am using the old one ..
i will try by update

···

--
Posted via http://www.ruby-forum.com/\.