Forum: Ruby Shame on me poorr newbie " unexpected kend"

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Alex M. (Guest)
on 2006-04-12 21:35
Hi,

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.

Shamefull but I come for help.

Regards

Alexmic
James G. (Guest)
on 2006-04-12 21:43
(Received via mailing list)
On Apr 12, 2006, at 12:35 PM, alex mic wrote:

>   quest="u"
>       	found=1
>       	@procdata = str
>       else
>         store ++

That's not a valid Ruby statement.  Try:

   store += 1

>       end
>     end
>   end
> end

Hope that helps.

James Edward G. II
Mark V. (Guest)
on 2006-04-12 21:49
(Received via mailing list)
On 4/12/06, alex mic <removed_email_address@domain.invalid> wrote:
>   quest="u"
>         @procdata = str
>       else
>         store ++

Ruby doesn't support ++.  I don't know if that's the only problem
though.
Bertram S. (Guest)
on 2006-04-12 23:33
(Received via mailing list)
Hi,

Am Donnerstag, 13. Apr 2006, 02:42:39 +0900 schrieb James Edward G.
II:
> That's not a valid Ruby statement.  Try:
>
>   store += 1

Further you probably want to increment `store' at the end of
the _outer_ loop.

Ruby is a good opportunity to write readable code. Do
yourself a favour and take it.

Bertram
unknown (Guest)
on 2006-04-13 02:21
(Received via mailing list)
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"

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" :-)


David

--
David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" coming in PDF April 15, and in paper May 5!
http://www.manning.com/black
Alex M. (Guest)
on 2006-04-13 13:57
First thank you all for your fast answers.

james gray wrote:
>That's not a valid Ruby statement.  Try:
>
>  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

thanks again

Regards

Alexmic
Daniel H. (Guest)
on 2006-04-13 18:08
(Received via mailing list)
On Apr 13, 2006, at 11:57 AM, alex mic wrote:

>     while !(quest=="y" or quest=="n")
>     end
>   end
> end

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


-- Daniel
Alex M. (Guest)
on 2006-04-13 21:44
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 topic is locked and can not be replied to.