I am trying to learn ruby through a udev utility coding.
I usually manage to debug on my own but in this case i dont understand
what is wrong…
def finddevice()
puts “We are now trying to find the device”
found=0
store=0
quest=“u”
while found!=1
io = IO.popen(“cat /proc/scsi/usb-storage/#{store}”, “r+”)
io.close_write
str = io.read
while quest!=“y” or quest!=“n”
puts “This is what is found on /proc/scsi/usb-storage/#{store}. Is
it your device [y/n]”
quest=gets.chop!.downcase
if quest==“y”
found=1 @procdata = str
else
store ++
end
end
end
end
I get an unexpected kend at the end following the else…
I have one function, two while and one if - else, which mean four “end”
I tried to end the if before the else and to not end the else (because
of one line only) with no success.
quest=“u”
while found!=1
io = IO.popen(“cat /proc/scsi/usb-storage/#{store}”, “r+”)
io.close_write
str = io.read
while quest!=“y” or quest!=“n”
See the other answers for the ++ and possibly other things – but note
also that this while will execute forever. Every string is either
not “y” or not “n”
Don’t use numbers as a substitute for booleans. Also, why are you
calling IO.popen? File.read would work fine.
def ask(msg)
loop do
print "#{msg} [y/n]: "
response = gets().strip
if response == “y”
return true
elsif response == “n”
return false
else
puts “Invalid response. Please enter y or n”
end
end
end
def find_device
puts “We are now trying to find the device”
store = 0
loops do
usb_dev = “/proc/scsi/usb-storage/#{store}”
usb = File.read(usb_dev)
if ask(“Found in #{usb_dev}: #{usb}. Is this your device?”) @procdata = usb
break
else
store += 1
end
end
end
store += 1 Ex-con (java) habits are hard to loose
It fixed the error.
unknown wrote:
Hi –
On Thu, 13 Apr 2006, alex mic wrote:
quest=“u”
while found!=1
io = IO.popen(“cat /proc/scsi/usb-storage/#{store}”, “r+”)
io.close_write
str = io.read
while quest!=“y” or quest!=“n”
The loop is indeed infinite, i changed for
while !(quest==“y” or quest==“n”)
Here is the final method
def finddevice()
puts “We are now trying to find the device”
found=0
store=0
quest=“u”
while found!=1
io = IO.popen(“cat /proc/scsi/usb-storage/#{store}”, “r+”)
io.close_write
str = io.read
puts str
while !(quest==“y” or quest==“n”)
puts “This is what is found on /proc/scsi/usb-storage/#{store}. Is
it your device [y/n]”
quest=gets.chop!.downcase
if quest==“y”
found=1 @procdata = str
else
store+=1
end
end
end
end
Well obviously when I wrote “final method” I meant “possibly working
method yet very dirty one”.
Daniel H. wrote:
Don’t use numbers as a substitute for booleans.
Yes sir!
Just to mention that I haven’t coded for years and that TIMTOWTDI is a
friend I tend to rely on too much.
Also, why are you
calling IO.popen? File.read would work fine.
Of course. Well codind on a XP for linux is not always the best way to
proceed… Please pitty my condition, as having access to the files is
sometimes helpfull.
def ask(msg)
loop do
print "#{msg} [y/n]: "
response = gets().strip
if response == “y”
return true
elsif response == “n”
return false
else
puts “Invalid response. Please enter y or n”
end
end
end
def find_device
puts “We are now trying to find the device”
store = 0
loops do
usb_dev = “/proc/scsi/usb-storage/#{store}”
usb = File.read(usb_dev)
if ask(“Found in #{usb_dev}: #{usb}. Is this your device?”) @procdata = usb
break
else
store += 1
end
end
end
– Daniel
Well, this is more clean not to mention lines saving (I bet you gessed
this was not the only question of my piece of software) and reader
friendly.
Your help is greatly appriciated, so thanks again.
Alex.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.