Must be my bug, but it looks like Ruby's ... could it be?

R

RichardOnRails

I know my Subject line looks like heresy in this newsgroup, but I
can't figure out how I'm screwing up in this bug.

I posted the program at http://www.pastie.org/1849430
It reads in a little control data, which is displayed at
http://www.pastie.org/1849434

The program reads in the control data and stores it in hash containing
two hashes in this case, which represents the two pseudo-hashes in the
control data.

My problem is conflicting output by the statements numbered 155 & 156:
puts %[%d\t%s => %s] % [num+=1, key, value]
print " key = "; p key
which respectively yield the following as the 4th and 5th output lines
of the program:
1 BackupFile => {:filename => "BackupFile.rb", :path =>
".", :digits => 3}
key = " BackupFile => {:filename => \"BackupFile.rb\", :path => \".
\", :digits"

The first of these output lines indicates to me that key ==
"BackupFile", as I expected.
But the second line seems to indicate that key == " BackupFile =>
{:filename => \"BackupFile.rb\", :path => \".\", :digits"

Can anyone suggest how I can end this inconsistency?

Thanks in Advance,
Richard
 
M

Mike Stok

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


I know my Subject line looks like heresy in this newsgroup, but I
can't figure out how I'm screwing up in this bug.
=20
I posted the program at http://www.pastie.org/1849430
It reads in a little control data, which is displayed at
http://www.pastie.org/1849434
=20
The program reads in the control data and stores it in hash containing
two hashes in this case, which represents the two pseudo-hashes in the
control data.
=20
My problem is conflicting output by the statements numbered 155 & 156:
puts %[%d\t%s =3D> %s] % [num+=3D1, key, value]
print " key =3D "; p key
which respectively yield the following as the 4th and 5th output lines
of the program:
1 BackupFile =3D> {:filename =3D> "BackupFile.rb", :path = =3D>
".", :digits =3D> 3}
key =3D " BackupFile =3D> {:filename =3D> \"BackupFile.rb\", = :path =3D> \".
\", :digits"
=20
The first of these output lines indicates to me that key =3D=3D
"BackupFile", as I expected.
But the second line seems to indicate that key =3D=3D " BackupFile =3D>
{:filename =3D> \"BackupFile.rb\", :path =3D> \".\", :digits"
=20
Can anyone suggest how I can end this inconsistency?

I don't see an inconsistency in the output, the data might not have been =
split as you expected.

#!/usr/bin/env ruby

num =3D 0
key =3D ' BackupFile =3D> {:filename =3D> "BackupFile.rb", :path =3D> =
".", :digits'
value =3D '3}'

puts %[%d\t%s =3D> %s] % [num+=3D1, key, value]
print " key =3D "; p key =20

seems to produce approximately your output, in your first puts how do =
you tell which =3D> is which?

Your split looks like the first .+ is too greedy as it tries to consume =
as much of the string as possible while the match can still work. You =
could use the ? modifier to make it parsimonius, or use split maybe:

#!/usr/bin/env ruby

string =3D 'BackupFile =3D> {:filename =3D> "BackupFile.rb", :path =3D> =
".", :digits =3D> 3}'

string =3D~ /(^.+)\s\=3D\>\s(.+)$/
p $1
p $2

string =3D~ /(^.+?)\s\=3D\>\s(.+)$/
p $1
p $2

array =3D string.split(' =3D> ', 2)
p array[0]
p array[1] =20

=3D>

ratdog:tmp mike$ ./try.rb
"BackupFile =3D> {:filename =3D> \"BackupFile.rb\", :path =3D> \".\", =
:digits"
"3}"
"BackupFile"
"{:filename =3D> \"BackupFile.rb\", :path =3D> \".\", :digits =3D> 3}"
"BackupFile"
"{:filename =3D> \"BackupFile.rb\", :path =3D> \".\", :digits =3D> 3}"

Hope this helps,

Mike

- --=20

Mike Stok <[email protected]>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.




-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)

iEYEARECAAYFAk28C0EACgkQnsTBwAWZE9qAgACdGxPswALo1oYNerDr/kjElDyz
gqQAn2G3hUKdMLZpcuScqE7kLZ/W/aLR
=3DUJYj
-----END PGP SIGNATURE-----
 
7

7stud --

RichardOnRails wrote in post #995908:
I know my Subject line looks like heresy in this newsgroup, but I
can't figure out how I'm screwing up in this bug.

I posted the program at http://www.pastie.org/1849430
It reads in a little control data, which is displayed at
http://www.pastie.org/1849434

The program reads in the control data and stores it in hash containing
two hashes in this case, which represents the two pseudo-hashes in the
control data.

My problem is conflicting output by the statements numbered 155 & 156:
puts %[%d\t%s => %s] % [num+=1, key, value]

I know about ruby's % substitutions, but I can't decipher that line--you
have obfuscated the code very well! Not a good thing. I would write
that line as:

puts "%d\t%s => %s" % [num+=1, key, val]

lol. You are using the % shortcut for %Q and you are using [] as the
delimiter. I have only one thing to say: horrible code writing.
 
R

RichardOnRails

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I know my Subject line looks like heresy in this newsgroup, but I
can't figure out how I'm screwing up in this bug.
I posted the program athttp://www.pastie.org/1849430
It reads in a little control data, which is displayed at
http://www.pastie.org/1849434
The program reads in the control data and stores it in hash containing
two hashes in this case, which represents the two pseudo-hashes in the
control data.
My problem is conflicting output by the statements numbered 155 & 156:
     puts %[%d\t%s => %s] % [num+=1, key, value]
     print " key = "; p key
which respectively yield the following as the 4th and 5th output lines
of the program:
     1      BackupFile => {:filename => "BackupFile.rb", :path =>
".", :digits => 3}
      key = " BackupFile => {:filename => \"BackupFile.rb\", :path => \".
\", :digits"
The first of these output lines indicates to me that key ==
"BackupFile", as I expected.
But the second line seems to indicate that key == " BackupFile =>
{:filename => \"BackupFile.rb\", :path => \".\", :digits"
Can anyone suggest how I can end this inconsistency?

I don't see an inconsistency in the output, the data might not have been split as you expected.

#!/usr/bin/env ruby

num = 0
key = ' BackupFile => {:filename => "BackupFile.rb", :path => ".", :digits'
value = '3}'

puts %[%d\t%s => %s] % [num+=1, key, value]
print " key = "; p key    

seems to produce approximately your output, in your first puts how do youtell which => is which?

Your split looks like the first .+ is too greedy as it tries to consume as much of the string as possible while the match can still work.  You could use the ? modifier to make it parsimonius, or use split maybe:

#!/usr/bin/env ruby

string = 'BackupFile => {:filename => "BackupFile.rb", :path => "..", :digits => 3}'

string =~ /(^.+)\s\=\>\s(.+)$/
p $1
p $2

string =~ /(^.+?)\s\=\>\s(.+)$/
p $1
p $2

array =  string.split(' => ', 2)
p array[0]
p array[1]  

=>

ratdog:tmp mike$ ./try.rb
"BackupFile => {:filename => \"BackupFile.rb\", :path => \".\", :digits"
"3}"
"BackupFile"
"{:filename => \"BackupFile.rb\", :path => \".\", :digits => 3}"
"BackupFile"
"{:filename => \"BackupFile.rb\", :path => \".\", :digits => 3}"

Hope this helps,

Mike

- --

Mike Stok <[email protected]>http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)

iEYEARECAAYFAk28C0EACgkQnsTBwAWZE9qAgACdGxPswALo1oYNerDr/kjElDyz
gqQAn2G3hUKdMLZpcuScqE7kLZ/W/aLR
=UJYj
-----END PGP SIGNATURE-----

Hi Mike,

Thanks for your response. I had more serious things to work on last
week; hence, my belated response.

Your question about the appropriateness of my regexp got me to look
more closely at my parsing of the input file and saw an incorrect
result. The fact that one use of the incorrect data misled me to "cry
wolf." I'll try to apply the Abstract Syntax Tree pattern from Russ
Olsen's "Design Patterns in Ruby". It's perhaps overkill for this
problem but it's better than my hand-crafted incorrect routine.

Best wishes,
Richard
 
R

RichardOnRails

RichardOnRails wrote in post #995908:
I know my Subject line looks like heresy in this newsgroup, but I
can't figure out how I'm screwing up in this bug.
I posted the program athttp://www.pastie.org/1849430
It reads in a little control data, which is displayed at
http://www.pastie.org/1849434
The program reads in the control data and stores it in hash containing
two hashes in this case, which represents the two pseudo-hashes in the
control data.
My problem is conflicting output by the statements numbered 155 & 156:
    puts %[%d\t%s => %s] % [num+=1, key, value]

I know about ruby's % substitutions, but I can't decipher that line--you
have obfuscated the code very well!  Not a good thing.   I would write
that line as:

puts "%d\t%s => %s" % [num+=1, key, val]

lol.  You are using the % shortcut for %Q and you are using [] as the
delimiter.  I have only one thing to say: horrible code writing.

Hi 7Stud,

Thanks for your response. As I mentioned to Mike, I had more serious
things to work on last
week; hence, my belated response.
puts %[%d\t%s => %s] % [num+=1, key, value]
vs.
puts "%d\t%s => %s" % [num+=1, key, val]

I used %[...] instead of "..." because I often want to include
embedded double-quotes in my strings and believe my delimiting
approach is clearer since I don't have to escape double-quotes.
Nevertheless, I'm mindful of the ancient adage, de gustibus non est
disputandum.

As I also mentioned to Mike, I now realize that my parsing routine
produced errors in in my control data. Rather than trying to correct
my amateurish code, I'm going to study one of Ruby Design Patterns
for application to this problems.

Best wishes,
Richard
 
7

7stud --

RichardOnRails wrote in post #997471:
two hashes in this case, which represents the two pseudo-hashes in the

lol. You are using the % shortcut for %Q and you are using [] as the
delimiter. I have only one thing to say: horrible code writing.

Hi 7Stud,

Thanks for your response. As I mentioned to Mike, I had more serious
things to work on last
week; hence, my belated response.
puts %[%d\t%s => %s] % [num+=1, key, value]
vs.
puts "%d\t%s => %s" % [num+=1, key, val]

I used %[...] instead of "..." because I often want to include
embedded double-quotes in my strings

I'm not sure which 'strings' you are talking about? key? val? In that
case, it doesn't matter what is inside key or val:

key = 'He said, "Hi".'
result = "%s" % key
puts result

--output:--
He said, "Hi".

Note that the substitution is not equivalent to writing:

result = "He said, "Hi"."

...which will not parse correctly.

If you are talking about the string on the left, then you need to alter
your quoting regimen depending on the specific situation--do not use a
one size fits all approach. That will just make your code hard to
decipher. And after trying to decipher your code, I would suggest that
NOBODY ever use the % shortcut--either use %q or %Q. Code clarity is
much more important than saving one character while typing.










and believe my delimiting
 
R

RichardOnRails

RichardOnRails wrote in post #997471:








two hashes in this case, which represents the two pseudo-hashes in the
lol. You are using the % shortcut for %Q and you are using [] as the
delimiter. I have only one thing to say: horrible code writing.
Hi 7Stud,
Thanks for your response.  As I mentioned to Mike, I had more serious
things to work on last
week; hence, my belated response.
puts %[%d\t%s => %s] % [num+=1, key, value] vs.
puts "%d\t%s => %s" % [num+=1, key, val]
I used %[...] instead of "..." because I often want to include
embedded double-quotes in my strings

I'm not sure which 'strings' you are talking about?  key? val?  In that
case, it doesn't matter what is inside key or val:

key = 'He said, "Hi".'
result = "%s" % key
puts result

--output:--
He said, "Hi".

Note that the substitution is not equivalent to writing:

result = "He said, "Hi"."

..which will not parse correctly.

If you are talking about the string on the left, then you need to alter
your quoting regimen depending on the specific situation--do not use a
one size fits all approach.  That will just make your code hard to
decipher.  And after trying to decipher your code, I would suggest that
NOBODY ever use the % shortcut--either use %q or %Q.  Code clarity is
much more important than saving one character while typing.

 and believe my delimiting
approach is clearer since I don't have to escape double-quotes.
Nevertheless, I'm mindful of the ancient adage, de gustibus non est
disputandum.
As I also mentioned to Mike,  I now realize that my parsing routine
produced errors in in my control data.  Rather than trying to correct
my amateurish code,  I'm going to study one of Ruby Design Patterns
for application to this problems.
Best wishes,
Richard

Hi 7Stud,

Thanks for taking the trouble press the case that %[...] is less clear
than including the explicit q/Q
I would suggest that NOBODY ever use the % shortcut--either use %q or %Q.

You may be correct that nobody uses this shortcut. However, AFAIK,
Matz implemented the shortcut. In my mind, that weighs more heavily
than the proportion of people that use it. Further more, it helps
avoid having to remember which of q/Q corresponds to which quote
mark. Finally, when I post a question here, I presume that the
people who'll respond are Ruby experts who won't be troubled by my use
of the shortcut.

In any case, I've moved on to another idea, namely using "Design
Patterns in Ruby" to parse user-input rather than my convoluted code.
Of course, I ran into another problem. I hope you'll be interested in
looking at my latest puzzle at
http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/d1af38232e963ed6#

Best wishes,
Richard
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top