String Matching in an Array

I have an array set up that I want to match the user input to a property
of a sting. However whenever I try to impliment the code the stings
never match and I am returned nil.

The first two ways I tried:
class POlist
def find(lookup)
@purchase_orders.find { |aPOlist| lookup == aPOlist.po_number }

for i in 0…@purchase_orders.length

return @purchase_orders[i] if lookup ==

@purchase_orders[i].po_number

end

return nil

end
end

and the other one:

found_one = false
print "Please Enter a PO: "
find_po = gets
for i in 0…aPOlist.length
if aPOlist[i] == nil then break
end
if find_po.to_s == aPOlist[i].po_number
found_one = true
puts aPOlist[i]
end
end
if found_one == false
puts “PO NOT FOUND”
menu_screen.root
end

Forgot to mention I’m trying to match an 8 digit number, but I pretty
sure it’s being stored as a string.

I am taking the values from an Excel worksheet

po_number = worksheet.Range("#{col}#{row}").value

if po_number.length == 8 #the standard Purchase Order length
aPOlist.append(PurchaseOrder.new("#{po_number}", “#{notes}”,
“#{company}”, “#{unit_config}”, “#{due_date}”, “#{quantity.to_i}”))
end

Ryan Edwards-crewe wrote:

end

Don’t forget to chomp the input to get rid of the newline:

irb(main):001:0> val = gets
12345678
=> “12345678\n”
irb(main):002:0> val = gets.chomp
12345678
=> “12345678”
irb(main):003:0>

Also, instead of doing

for i in 0…aPOlist.length
#code
end

a prettier way is

aPOlist.each_index do |i|
#code
end

Most excellent thank you, it’s working now, it’s always something small
:stuck_out_tongue:
But if you don’t mind me picking your brain, or anyone’s for that
matter. Is there a way I can clear the screen of the console from ruby,
(eg. cls in terminal or windows command prompt), things just get real
messy looking when all it does is scroll.

Thanks.

system ‘clear’

Just shell out to the way it is usually done.

mark

Thanks Mark and Chris duely noted.

Ryan Edwards-crewe wrote:

Posted via http://www.ruby-forum.com/.
use the system command:
system(‘cls’)
Or
cls #note the back-quotes

Note that Array (and anything that mixes in Enumerable) has a #find
method:

anPOlist.find {|aPO| aPO.poNumber == aPOnumber}

will return the first match, or nil if not found

cheers

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs