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.
05a7eb6910a3bf70e59542018581c8e0?d=identicon&s=25 Alex Mic (alexmic)
on 2006-04-12 19: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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-04-12 19: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 Gray II
52a177e9dbd3e614825aabc4e45f8cd6?d=identicon&s=25 Mark Volkmann (Guest)
on 2006-04-12 19:49
(Received via mailing list)
On 4/12/06, alex mic <alexandremicouleau@free.fr> wrote:
>   quest="u"
>         @procdata = str
>       else
>         store ++

Ruby doesn't support ++.  I don't know if that's the only problem
though.
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2006-04-12 21:33
(Received via mailing list)
Hi,

Am Donnerstag, 13. Apr 2006, 02:42:39 +0900 schrieb James Edward Gray
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-04-13 00: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 (dblack@wobblini.net)
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
05a7eb6910a3bf70e59542018581c8e0?d=identicon&s=25 Alex Mic (alexmic)
on 2006-04-13 11: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
9358cc96c46055cd68d4a76a9aefe026?d=identicon&s=25 Daniel Harple (Guest)
on 2006-04-13 16: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
05a7eb6910a3bf70e59542018581c8e0?d=identicon&s=25 Alex Mic (alexmic)
on 2006-04-13 19:44
Well obviously when I wrote "final method" I meant "possibly working
method yet very dirty one".

Daniel Harple 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.