newbie 1.8.7 to 1.9.1 problem

P

Phil Richards

Several scripts that used to work now don't. I had not used ruby or any
of
these scripts for several months. In the meantime i upgraded vista to
windows 7 (clean install) and decided to install 1.9.1 rather than the
1.8.7 i used previously.

And now here's the problem i have:

f = open("http...
webpage = f.read
...
dump = webpage.sub(%r{<body.*?>(.*?)...
...
# process the rest line-by-line
dump.each do |eachline| # this line produces this error:
... :in `block in <main>': undefined method `each' for
#<String:0x1f3f620> (NoMethodError)

I guess its telling me dump is a string and you can't do "string".each
But i don't understand why this--and several similar--scripts DID WORK
with Ruby 1.8.7 So 1.8.7 would "split up" dump at every newline (CR/LF)
but 1.9.1 will not...???

Can someone tell me what's going on, and what i should try to get these
to work
with 1.9.1?

Thanks!
 
M

Michael Fellinger

Several scripts that used to work now don't. I had not used ruby or any
of
these scripts for several months. In the meantime i upgraded vista to
windows 7 (clean install) and decided to install 1.9.1 rather than the
1.8.7 i used previously.

And now here's the problem i have:

f =3D open("http...
webpage =3D f.read
...
dump =3D webpage.sub(%r{<body.*?>(.*?)...
...
# process the rest line-by-line
dump.each do |eachline| =C2=A0# this line produces this error:
... :in `block in <main>': undefined method `each' for
#<String:0x1f3f620> (NoMethodError)

I guess its telling me dump is a string and you can't do "string".each
But i don't understand why this--and several similar--scripts DID WORK
with Ruby 1.8.7 =C2=A0So 1.8.7 would "split up" dump at every newline (CR= /LF)
but 1.9.1 will not...???

Can someone tell me what's going on, and what i should try to get these
to work
with 1.9.1?

iota ~ % irb
"".methods.grep(/each/) =3D> [:each_line, :each_byte, :each_char, :each_codepoint]
"".is_a?(Enumerable)
=3D> false

--=20
Michael Fellinger
CTO, The Rubyists, LLC
 
R

Robert Klemme

Several scripts that used to work now don't. I had not used ruby or any
of
these scripts for several months. In the meantime i upgraded vista to
windows 7 (clean install) and decided to install 1.9.1 rather than the
1.8.7 i used previously.

And now here's the problem i have:

f = open("http...
webpage = f.read
..
dump = webpage.sub(%r{<body.*?>(.*?)...
..
# process the rest line-by-line
dump.each do |eachline| # this line produces this error:
.. :in `block in<main>': undefined method `each' for
#<String:0x1f3f620> (NoMethodError)

I guess its telling me dump is a string and you can't do "string".each
But i don't understand why this--and several similar--scripts DID WORK
with Ruby 1.8.7 So 1.8.7 would "split up" dump at every newline (CR/LF)
but 1.9.1 will not...???

Can someone tell me what's going on, and what i should try to get these
to work
with 1.9.1?

You just need to replace #each with #each_line and be done. I believe
the background of this is that 1.9 tries do it a bit better than 1.8
which assumed every String is an Enumerable of lines instead of
characters which would be more natural.

Kind regards

robert
 
B

Brian Candler

Phil said:
I guess its telling me dump is a string and you can't do "string".each

Correct: String#each was removed from 1.9
But i don't understand why this--and several similar--scripts DID WORK
with Ruby 1.8.7 So 1.8.7 would "split up" dump at every newline (CR/LF)
but 1.9.1 will not...???

That's right.

ruby 1.9 is a substantially different language than 1.8, both in terms
of its implementation (YARV engine) and its language specification (e.g.
total change to how Strings work). You might not infer this from the
"minor" version bump, but it's the case.
Can someone tell me what's going on, and what i should try to get these
to work
with 1.9.1?

You'll have to take it case-by-case. For example, use String#each_line
here. (Incidentally, I can see no logical reason for removing
String#each but not removing IO#each)

The good news is that the 1.8 language remains alive and well, so you
can just stick with that.
 

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,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top