# Ruby noob

**URL:** <https://www.ruby-forum.com/t/ruby-noob/87707>\
**Category:** Ruby\
**Created:** [March 5, 2007, 12:56pm UTC](https://www.ruby-forum.com/t/ruby-noob/87707 "2007-03-05T12:56:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mosfet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mosfet](https://www.ruby-forum.com/u/mosfet)\
**Post date:** [March 5, 2007, 12:56pm UTC](https://www.ruby-forum.com/t/ruby-noob/87707/1 "2007-03-05T12:56:17Z")

</div>

Hi,

I would like to parse a very simple html(index\_msg.htm) file described  
below :

 WM\_ACTIVATE 0x0006 0x0000 WM\_NULL WM\_ACTIVATEAPP 0x001C 0x0001 WM\_CREATE ... I would like to parse this file and to extract information like this :

enum foo  
{  
eWM\_ACTIVATE = 0x0006,  
eWM\_ACTIVATEAPP = 0x0001,  
…  
};

I am starting with this :

fileIn = File.open(“C:/WIKI\_CE/index\_msg.htm”, “r”)  
fileOut = File.new(“C:/WIKI\_CE/enumWmMsg.h”, “w”)

begin  
while (line = fileIn.readline)  
line.chomp  
$stdout.print line  
end  
rescue EOFError  
fileIn.close  
fileOut.close  
end

but now I am stuck. Should I use regex or how can I compare two string ?

---

<div class="post-metadata">

**Author:** ![mosfet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mosfet](https://www.ruby-forum.com/u/mosfet)\
**Post date:** [March 5, 2007, 1:12pm UTC](https://www.ruby-forum.com/t/ruby-noob/87707/2 "2007-03-05T13:12:19Z")

</div>

mosfet wrote:

> WM\_NULL
> 
> fileIn = File.open(“C:/WIKI\_CE/index\_msg.htm”, “r”)  
> end  
> This should get you started:

=====================================================================  
require ‘rubygems’  
require ‘scrubyt’

data = Scrubyt::Extractor.define do  
fetch(‘input.html’)

record do  
var\_name ‘WM\_ACTIVATE’  
code ‘0x0006’  
end  
end

result = data.to\_xml.to\_s  
names = result.scan(/var\_name\>(.+?)\</var\_name/).flatten  
values = result.scan(/code\>(.+?)\</code/).flatten  
pairs = names.zip(values)

# pairs.each do |name, value| puts “e#{name} = #{value}” end

The XML to array code kind of sucks, in the next version of scRUBYt! you  
will be able to output the result directly to a hash (or CSV or YAML or  
some other, more friendly format for such a task).

Cheers,  
Peter  
\_\_  
[http://www.rubyrailways.com](http://www.rubyrailways.com) :: Ruby and Web2.0 blog  
[http://scrubyt.org](http://scrubyt.org) :: Ruby web scraping framework  
[http://rubykitchensink.ca/](http://rubykitchensink.ca/) :: The indexed archive of all things Ruby

---

<div class="post-metadata">

**Author:** ![mosfet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mosfet](https://www.ruby-forum.com/u/mosfet)\
**Post date:** [March 5, 2007, 1:15pm UTC](https://www.ruby-forum.com/t/ruby-noob/87707/3 "2007-03-05T13:15:19Z")

</div>

On 3/5/07, mosfet [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> WM\_NULL
> 
> fileIn = File.open(“C:/WIKI\_CE/index\_msg.htm”, “r”)  
> end
> 
> but now I am stuck. Should I use regex or how can I compare two string ?

1. have a look at hpricot
2. if it’s too big for you use regexen with /m flag, and use  
Regex#scan():

REGEX = /\s\*

(.\*?)\<\/td\>\s\* (.\*?)\<\/td\> (.\*?)\<\/td\> (.\*?) (.\*?) \<\/tr\>/xm

file\_in = File.read(“C:/WIKI\_CE/index\_msg.htm”)  
File.open(“C:/WIKI\_CE/enumWmMsg.h”, “w”) do |file\_out|  
file\_in.scan(REGEX) do  
file\_out.puts $1, $2, $3, $4, $5  
end  
end  
end

Notes:

1. we\_use\_snake\_case\_for\_variable\_names
2. Use File.open with block to automatically close the file
3. You’ll have the values in $1…$5
4. It seems you are inconsistent - in the first example you chose the  
second line, in the other the fourth one.

In any case, Peter’s approach will be easier, and more stable.
