Sam Roberts wrote:

>>why not ParseTree or ripper ?
> 
> I have no idea what ripper does, but parse tree just gives symbols, it
> doesn't have enough information for me to build a regex, as above, does
> it?

Ripper basically is Ruby's integrated Ruby parser. It will invoke 
callbacks for every kind of construct it encounters.

This code snippet ought to get you started with it:

irb(main):017:0> class MyParser < Ripper
irb(main):018:1>   def method_missing(name, *args)
irb(main):019:2>     puts "#{name}: #{args.inspect}"
irb(main):020:2>   end
irb(main):021:1> end
=> nil
irb(main):022:0> MyParser.new.parse("puts 'Hello World!' if true")
on__scan: ["puts"]
on__IDENTIFIER: ["puts"]
on__scan: [" "]
on__space: [" "]
on__scan: ["'"]
on__new_string: ["'"]
on__scan: ["Hello World!"]
on__add_string: [nil, "Hello World!"]
on__scan: ["'"]
on__string_end: [nil, "'"]
on__scan: [" "]
on__space: [" "]
on__scan: ["if"]
on__KEYWORD: ["if"]
on__argstart: ["Hello World!"]
on__fcall: [:puts, nil]
on__scan: [" "]
on__space: [" "]
on__scan: ["true"]
on__KEYWORD: ["true"]
on__varref: [:true]
on__if_mod: [nil, nil]
=> nil