Problem with while loop.

Discussion in 'Ruby' started by rumpy@mrbitter.org, Jun 20, 2008.

  1. Guest

    Hi,


    I'd greatly appreciate feedback on the following code.


    def create_files
    names_array = self.read_names #another method in my class

    while names_array.length != 0
    name = names_array.pop
    File.open( name, "w" ) { |f| f.puts "File is no longer empty" }
    end

    x.create_files


    Which results in:

    C:/Scripts/Ruby/file_gen.rb:34:in `initialize': Invalid argument - echo (Errno
    ::EINVAL)
    from C:/Scripts/Ruby/file_gen.rb:34:in `open'
    from C:/Scripts/Ruby/file_gen.rb:34:in `create_files'
    from C:/Scripts/Ruby/file_gen.rb:42

    The array contains the strings 'alpha' through 'foxtrot' and before
    producing the error above the file 'foxtrot' is created. So the logic
    works once before failing.

    When I pull this code out of the class definition it works perfectly
    and creates
    a file for every name in the array. What noob mistakes am I making?


    Thanks,

    S.
     
    , Jun 20, 2008
    #1
    1. Advertising

  2. On Friday 20 June 2008, wrote:
    > Hi,
    >
    >
    > I'd greatly appreciate feedback on the following code.
    >
    >
    > def create_files
    > names_array = self.read_names #another method in my class
    >
    > while names_array.length != 0
    > name = names_array.pop
    > File.open( name, "w" ) { |f| f.puts "File is no longer empty" }
    > end
    >
    > x.create_files
    >
    >
    > Which results in:
    >
    > C:/Scripts/Ruby/file_gen.rb:34:in `initialize': Invalid argument - echo
    > (Errno
    >
    > ::EINVAL)
    >
    > from C:/Scripts/Ruby/file_gen.rb:34:in `open'
    > from C:/Scripts/Ruby/file_gen.rb:34:in `create_files'
    > from C:/Scripts/Ruby/file_gen.rb:42
    >
    > The array contains the strings 'alpha' through 'foxtrot' and before
    > producing the error above the file 'foxtrot' is created. So the logic
    > works once before failing.
    >
    > When I pull this code out of the class definition it works perfectly
    > and creates
    > a file for every name in the array. What noob mistakes am I making?
    >
    >
    > Thanks,
    >
    > S.


    To understand what's going on, one should understand what the error
    Errno::EINVAL means. Unfortunately, the only explaination a very quick search
    returned is this:
    "Invalid argument. This is used to indicate various kinds of problems with
    passing the wrong argument to a library function."

    I don't know what can be causing it. I tried passing non-string arguments to
    File.open, but I always got TypeError s, except when I tried passing an
    integer, which gave me an Errno::EBADF exception (I'm on linux, though, so
    maybe on windows that can give you another kind of error). At any rate, the
    first thing I'd try (maybe you've already done it) is to check that indeed the
    contents of names_array is what you think it is, putting a
    p names_array
    just after creating it.

    If that doesn't give you a clue, you can rewrite the code in a more rubysh
    way. If I understand correctly, you're simply iterating on all the elements of
    the array and the while loop and calls to pop are simply there to know when to
    stop the loop. In this case, the whole while loop can be replaced by this:

    array_names.each do |n|
    File.open(n, "w"){|f| f.puts "File is no longer empty"}
    end

    The substance of the code shouldn't change (well, aside from the fact that the
    array won't be empty at the end), but this should ensure there is no mistake
    in the logic of the loop (I can't see any, but you never know).

    Other than this, I don't know what else to suggest.

    I hope this helps

    Stefano
     
    Stefano Crocco, Jun 20, 2008
    #2
    1. Advertising

  3. Guest

    Hi Stefano,


    Quoting Stefano Crocco <>:

    > On Friday 20 June 2008, wrote:
    >> Hi,
    >>
    >>
    >> I'd greatly appreciate feedback on the following code.
    >>
    >>
    >> def create_files
    >> names_array =3D self.read_names #another method in my class
    >>
    >> while names_array.length !=3D 0
    >> name =3D names_array.pop
    >> File.open( name, "w" ) { |f| f.puts "File is no longer empty" }
    >> end
    >>
    >> x.create_files
    >>
    >>
    >> Which results in:
    >>
    >> C:/Scripts/Ruby/file_gen.rb:34:in `initialize': Invalid argument - echo
    >> (Errno
    >>
    >> ::EINVAL)
    >>
    >> from C:/Scripts/Ruby/file_gen.rb:34:in `open'
    >> from C:/Scripts/Ruby/file_gen.rb:34:in `create_files'
    >> from C:/Scripts/Ruby/file_gen.rb:42
    >>
    >> The array contains the strings 'alpha' through 'foxtrot' and before
    >> producing the error above the file 'foxtrot' is created. So the logic
    >> works once before failing.
    >>
    >> When I pull this code out of the class definition it works perfectly
    >> and creates
    >> a file for every name in the array. What noob mistakes am I making?
    >>
    >>
    >> Thanks,
    >>
    >> S.

    >
    > To understand what's going on, one should understand what the error
    > Errno::EINVAL means. Unfortunately, the only explaination a very quick sea=

    rch
    > returned is this:
    > "Invalid argument. This is used to indicate various kinds of problems with
    > passing the wrong argument to a library function."



    Firstly, thank you very much for the feedback. I did also look for =20
    more info on Ruby errors. Not much luck finding details on =20
    Errno::EINVAL.


    > I don't know what can be causing it. I tried passing non-string arguments =

    to
    > File.open, but I always got TypeError s, except when I tried passing an
    > integer, which gave me an Errno::EBADF exception (I'm on linux, though, so
    > maybe on windows that can give you another kind of error). At any rate, th=

    e
    > first thing I'd try (maybe you've already done it) is to check that =20
    > indeed the contents of names_array is what you think it is, putting a
    > p names_array just after creating it.



    I did replace 'File.open( name, "w" ) { |f| f.puts "File is no longer =20
    empty" }' with 'puts name' and 'puts name.class' just to make sure. =20
    Sure enough all the array elements are neatly popped out and they are =20
    strings.


    > If that doesn't give you a clue, you can rewrite the code in a more rubysh
    > way. If I understand correctly, you're simply iterating on all the =20
    > elements of the array and the while loop and calls to pop are simply =20
    > there > to know when to stop the loop. In this case, the whole =20
    > while loop can be > replaced by this:



    Your analysis of the code is correct.


    > array_names.each do |n|
    > File.open(n, "w"){|f| f.puts "File is no longer empty"}
    > end



    I'll try this.


    > The substance of the code shouldn't change (well, aside from the =20
    > fact that the array won't be empty at the end), but this should =20
    > ensure > there is no mistake in the logic of the loop (I =20
    > can't see any, but you > never know).
    >
    > Other than this, I don't know what else to suggest.
    >
    > I hope this helps



    Thanks again for you time and suggestions.


    > Stefano



    Regards,

    S.
     
    , Jun 20, 2008
    #3
  4. Guest

    Hi Stefano,


    Just a quick update: a friend ran my entire code on Mac/Unix and it =20
    worked perfectly. The problem appears to be Windows related...ughh.


    Thanks,

    S.


    Quoting Stefano Crocco <>:

    > On Friday 20 June 2008, wrote:
    >> Hi,
    >>
    >>
    >> I'd greatly appreciate feedback on the following code.
    >>
    >>
    >> def create_files
    >> names_array =3D self.read_names #another method in my class
    >>
    >> while names_array.length !=3D 0
    >> name =3D names_array.pop
    >> File.open( name, "w" ) { |f| f.puts "File is no longer empty" }
    >> end
    >>
    >> x.create_files
    >>
    >>
    >> Which results in:
    >>
    >> C:/Scripts/Ruby/file_gen.rb:34:in `initialize': Invalid argument - echo
    >> (Errno
    >>
    >> ::EINVAL)
    >>
    >> from C:/Scripts/Ruby/file_gen.rb:34:in `open'
    >> from C:/Scripts/Ruby/file_gen.rb:34:in `create_files'
    >> from C:/Scripts/Ruby/file_gen.rb:42
    >>
    >> The array contains the strings 'alpha' through 'foxtrot' and before
    >> producing the error above the file 'foxtrot' is created. So the logic
    >> works once before failing.
    >>
    >> When I pull this code out of the class definition it works perfectly
    >> and creates
    >> a file for every name in the array. What noob mistakes am I making?
    >>
    >>
    >> Thanks,
    >>
    >> S.

    >
    > To understand what's going on, one should understand what the error
    > Errno::EINVAL means. Unfortunately, the only explaination a very quick sea=

    rch
    > returned is this:
    > "Invalid argument. This is used to indicate various kinds of problems with
    > passing the wrong argument to a library function."
    >
    > I don't know what can be causing it. I tried passing non-string arguments =

    to
    > File.open, but I always got TypeError s, except when I tried passing an
    > integer, which gave me an Errno::EBADF exception (I'm on linux, though, so
    > maybe on windows that can give you another kind of error). At any rate, th=

    e
    > first thing I'd try (maybe you've already done it) is to check that =20
    > indeed the
    > contents of names_array is what you think it is, putting a
    > p names_array
    > just after creating it.
    >
    > If that doesn't give you a clue, you can rewrite the code in a more rubysh
    > way. If I understand correctly, you're simply iterating on all the =20
    > elements of
    > the array and the while loop and calls to pop are simply there to =20
    > know when to
    > stop the loop. In this case, the whole while loop can be replaced by this:
    >
    > array_names.each do |n|
    > File.open(n, "w"){|f| f.puts "File is no longer empty"}
    > end
    >
    > The substance of the code shouldn't change (well, aside from the =20
    > fact that the
    > array won't be empty at the end), but this should ensure there is no mista=

    ke
    > in the logic of the loop (I can't see any, but you never know).
    >
    > Other than this, I don't know what else to suggest.
    >
    > I hope this helps
    >
    > Stefano
    >
    >
    >
    >
     
    , Jun 20, 2008
    #4
  5. Dave Bass Guest

    unknown wrote:
    > Just a quick update: a friend ran my entire code on Mac/Unix and it
    > worked perfectly. The problem appears to be Windows related...ughh.


    I'd be highly suspicious of the file names, looking for things like
    trailing whitespace etc.

    Also I'd try opening each file like this:

    f = File.open(name, "w")
    f.puts("File is no longer empty")
    f.fsync
    f.close

    The fewer operations you do in one line the better from a debugging
    point of view. (The f.sync is to ensure that no buffering occurs, just
    in case.)
    --
    Posted via http://www.ruby-forum.com/.
     
    Dave Bass, Jun 21, 2008
    #5
  6. Guest

    Thanks Dave. I'll give this a try. I almost choked when my friend told
    it ran without changes on OS X.

    S.


    Quoting Dave Bass <>:

    > unknown wrote:
    >> Just a quick update: a friend ran my entire code on Mac/Unix and it
    >> worked perfectly. The problem appears to be Windows related...ughh.

    >
    > I'd be highly suspicious of the file names, looking for things like
    > trailing whitespace etc.
    >
    > Also I'd try opening each file like this:
    >
    > f = File.open(name, "w")
    > f.puts("File is no longer empty")
    > f.fsync
    > f.close
    >
    > The fewer operations you do in one line the better from a debugging
    > point of view. (The f.sync is to ensure that no buffering occurs, just
    > in case.)
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    , Jun 21, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,238
    Tim Slattery
    Mar 30, 2005
  2. Daniel Pitts
    Replies:
    14
    Views:
    1,237
    Patricia Shanahan
    Dec 23, 2006
  3. Uday Bidkar
    Replies:
    4
    Views:
    494
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  4. Roedy Green
    Replies:
    3
    Views:
    434
    Mike Schilling
    Sep 13, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    381
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page