yoink@tumblr ~ % less 1117771200.txt
Regular Expressions & Ruby. Dammit!
Ok… so my first shot at Ruby. I’ve been trying to mess around with craigslist because I’ve been looking for houses, but you can’t put in any boolean logic in the keyword search (Kensington or Park Slope or Ditmas Park). So I’m trying to aggregate the results of a bunch of keyword searches (of course, I’m probably looking over a a more obvious way of doing this, but at least I get to play with Ruby… woo!). Part of this whole scrapey thing involves regular expressions. Pretty easy… at first:
irb(main):010:0> re = /rub./
=> /rub./
irb(main):011:0> m = re.match("rubyrubisomethingelse")
=> #<MatchData:0x8068264>
irb(main):012:0> m[0]
=> "ruby"
Yeah… but that doesn’t match every instance of the pattern (you know, like /pattern/g). I spent a whole lot of time trying to figure out how to match more than one pattern in a string. It turned out that it to be something really simple —using “somestring”.scan instead of re.match. So here you go – mimicking //g, preg_match_all(), etc:
irb(main):013:0> re = /rub./
=> /rub./
irb(main):014:0> s = "rubyrubisomethingelse"
=> "rubyrubisomethingelse"
irb(main):015:0> m = s.scan(re)
=> ["ruby", "rubi"]
Next up (maybe). Ruby and CGI. Yeah!!!!!!