I’m very new to coding of any kind, and I’m trying to learn Ruby by
working through some projects that are relevant to me. I’m working on a
small project to pull the headlines out of a news website for me. I’m
able to get match working just fine. I’ve tried switching to string.scan
to be able to iterate and nothing is returned. I can’t figure out why
not, and would appreciate suggestions.
I realize the code is very ugly, and I apologize!
Thank you in advance,
CJ
page_string=‘junk’
open(“http://www.scatoday.net/”) {|f|
page_string = f.read
} #THIS one works, one time #rezstring = //node/[0-9]+.title=".">/.match(page_string) #THESE Don’t #page_string.scan(//node/[0-9]+.title=".">/) #page_string.gsub(//node/[0-9]+.title=".">/)
I’m very new to coding of any kind, and I’m trying to learn Ruby by
Welcome to the world of Ruby - and programming!
working through some projects that are relevant to me. I’m working on a
small project to pull the headlines out of a news website for me. I’m
able to get match working just fine. I’ve tried switching to string.scan
to be able to iterate and nothing is returned. I can’t figure out why
not, and would appreciate suggestions.
#THIS one works, one time #rezstring = //node/[0-9]+.title=".">/.match(page_string) #THESE Don’t #page_string.scan(//node/[0-9]+.title=".">/)
What does “does not work” mean? I get 80 matches.
#page_string.gsub(//node/[0-9]+.title=".">/)
This cannot work since you either need a second argument or a block.
Please post error messages or a more clear description of what you think
does not work.
I am not a Ruby programmer (nothing against it; I just happen to use
other languages right now). But I am the publisher of the web site scatoday.net to which you refer.
My suggestion is that an easier way to retrieve our headlines is to use
our RSS feed, which is XML format and therefore easier to machine-parse.
I will try out both your suggestions shortly. To clarify “does not work”
when I say that I mean nothing is returned. No error messages, no output
at all. I had included a puts with jibberish for testing purposes so I
knew it ran at all.
Thank you both for your time!
CJ
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.