Few question about IO

Hello,

I have few questions to wise mans.

First is about IO. I want read from input stream words or longs in given endianity. How can I extend IO class to have a method to read word or long word
from stream. I try

class IO
def getw
getc()+256*getc()
end
public :getw
end

but calling:
puts ARGF.getw()

gives me
./dis68k.rb:45: private method `getw’ called for #Object:0x401b1550 (NoMethodError)

Next question is about Regular expression. I have a Hash filled with
keyword, label pairs. Some of the keywords contains characters like:

  • & ( and other strange characters. Then I have a string in which I
    want replace all ocurrences of keywords with string the keyword must be like a word separated from
    other text by white spaces. Now I use this method

def mangle(s, dict)
dict.each_pair do |key,id|
word = Regexp.quote(key)
s.gsub!(%r[ #{word} ], %Q[ ])
s.gsub!(%r[^#{word} ], %Q[ ])
s.gsub!(%r[ #{word}$], %Q[ ])
end
s
end

Is there any better and speedy way how to do it. I’m not great ruby hacker.
The
s.gsub!(%r[\b#{word}\b], %Q[ ])

does not work because word can be + , : , ; , … . In my case word
is any sequence of printable characters separated from others with
white spaces.

The main goal is replace all ocurences of given words with to
that word. The table with words and its linkends is in file. The
word is any sequence of printable characters separated from others by
whitespaces.

···


Radek Hnilica

No matter how far down the wrong road you’ve gone, turn back.
Turkish proverb

$ ruby -e ‘puts ARGF.class’
Object

···

On Thu, 19 Dec 2002, Radek Hnilica wrote:

Hello,

I have few questions to wise mans.

First is about IO. I want read from input stream words or longs in given endianity. How can I extend IO class to have a method to read word or long word
from stream. I try

class IO
def getw
getc()+256*getc()
end
public :getw
end

but calling:
puts ARGF.getw()

gives me
./dis68k.rb:45: private method `getw’ called for #Object:0x401b1550 (NoMethodError)


foo.rb:

def Object
def getw
getc + 256*getc
end
end

$ruby -r foo -e ‘puts ARGF.getw’
< hi
26984

Next question is about Regular expression. I have a Hash filled with
keyword, label pairs. Some of the keywords contains characters like:

  • & ( and other strange characters. Then I have a string in which I
    want replace all ocurrences of keywords with string the keyword must be like a word separated from
    other text by white spaces. Now I use this method

def mangle(s, dict)
dict.each_pair do |key,id|
word = Regexp.quote(key)
s.gsub!(%r[ #{word} ], %Q[ ])
s.gsub!(%r[^#{word} ], %Q[ ])
s.gsub!(%r[ #{word}$], %Q[ ])
end
s
end

Is there any better and speedy way how to do it. I’m not great ruby hacker.
The
s.gsub!(%r[\b#{word}\b], %Q[ ])

does not work because word can be + , : , ; , … . In my case word
is any sequence of printable characters separated from others with
white spaces.

The main goal is replace all ocurences of given words with to
that word. The table with words and its linkends is in file. The
word is any sequence of printable characters separated from others by
whitespaces.

\s is all white space characters, and \S is all non-whitespace characters.
since you also wish to match end of line, this might work:

s.gsub(%r[(\s|^)#{word}(\s|$)],%Q[\1#{id}\2])

This has one flaw:

s = “foo foo foo foo”
word = “foo”
id = “bar”

s.gsub(%r[(\s|^)#{word}(\s|$)],%Q[\1#{id}\2])
gets: bar foo bar foo
because the space after the first foo (bar) is ignored when looking for
the first space of the next word.

One possible solution may be to do it twice. Any others escape me


Radek Hnilica


Greg Millam
walker at deafcode.com

does not work for me:
irb(main):032:0> “: dbl dup + ;”.gsub(%r[(\s|^)+(\s|$)], %Q[\1:plus:\2])
=> “: dbl dup\001:plus:\002;”

something is wrong with \1 and \2

$ ruby --version
ruby 1.7.3 (2002-12-17) [i586-linux]

···

On Thu, Dec 19, 2002 at 05:41:46PM +0900, Greg Millam wrote:

On Thu, 19 Dec 2002, Radek Hnilica wrote:

\s is all white space characters, and \S is all non-whitespace characters.
since you also wish to match end of line, this might work:

s.gsub(%r[(\s|^)#{word}(\s|$)],%Q[\1#{id}\2])

This has one flaw:

s = “foo foo foo foo”
word = “foo”
id = “bar”

s.gsub(%r[(\s|^)#{word}(\s|$)],%Q[\1#{id}\2])
gets: bar foo bar foo
because the space after the first foo (bar) is ignored when looking for
the first space of the next word.


Radek Hnilica

No matter how far down the wrong road you’ve gone, turn back.
Turkish proverb

Looking into ruby source, especially io I found that the correct place
for getw method seems to be a Kernel module.

Extending module Kernel with new methods.

module Kernel
def getw
getc + 256*getc
end
end

if $0 == FILE
puts “Brej veèír, statkáøi! Hledím do trubky: " + " a vidím:”
puts ARGF.getw
end

it works for me.

···

On Thu, Dec 19, 2002 at 05:41:46PM +0900, Greg Millam wrote:

foo.rb:

def Object
def getw
getc + 256*getc
end
end

$ruby -r foo -e ‘puts ARGF.getw’
< hi
26984


Radek Hnilica

No matter how far down the wrong road you’ve gone, turn back.
Turkish proverb

I would say Happy XMas to all, but I know that I don’t get to net till
january.

$ ruby -e ‘puts ARGF.class’
Object


foo.rb:

def Object
def getw
getc + 256*getc
end
end

$ruby -r foo -e ‘puts ARGF.getw’
< hi
26984

Thanks. In your code is an error. I do not know why it work for ya.
In my script I wrote:

Extending class Object with new methods. It’s an dirty hack.

class Object
def getw
getc + 256*getc
end
end

and it works. Thanks. Only one thing. the method getc is not a
method of class Object. Looking in classes, the right Class to exted
is IO which has method getc. But trying exted it does no work as you
can see in my firts post.

···

In article Pine.LNX.4.44.0212190332310.7487-100000@tyger.deafcode.com, Greg Millam wrote:


Radek Hnilica

No matter how far down the wrong road you’ve gone, turn back.
Turkish proverb

does not work for me:
irb(main):032:0> “: dbl dup + ;”.gsub(%r[(\s|^)+(\s|$)], %Q[\1:plus:\2])
=> “: dbl dup\001:plus:\002;”

something is wrong with \1 and \2

My bad - need to escape those.

\1 and \2

irb(main):032:0> “: dbl dup + ;”.gsub(%r[(\s|^)+(\s|$)], %Q[\1:plus:\2])

Whoops.

  • Greg
···

On Thu, 19 Dec 2002, Radek Hnilica wrote:


Radek Hnilica


Greg Millam
walker at deafcode.com

does not work for me:
irb(main):032:0> “: dbl dup +
;”.gsub(%r[(\s|^)+(\s|$)], %Q[\1:plus:\2])
=> “: dbl dup\001:plus:\002;”

something is wrong with \1 and \2

%Q is equivalent to double-quoting.
%q is equivalent to single-quoting

irb(main):002:0> “\1”
“\001”
irb(main):003:0> ‘\1’
“\1”
irb(main):004:0> %Q(\1)
“\001”
irb(main):005:0> %q(\1)
“\1”

When you call gsub, you must use the single-quoted
form. This ‘gotcha’ just bit me earlier today.

Jason

PS. It feels really good to be able to actually help
others for a change. :wink:

Thanks much. Now it works

def mangle(s, dict)
dict.each_pair do |key,id|
word = Regexp.quote(key)
s.gsub!(%r[(\s|^)#{word}(\s|$)], %Q[\1\2])
s.gsub!(%r[(\s|^)#{word}(\s|$)], %Q[\1\2])
end
s
end

···

On Thu, Dec 19, 2002 at 06:38:16PM +0900, Greg Millam wrote:

My bad - need to escape those.

\1 and \2

irb(main):032:0> “: dbl dup + ;”.gsub(%r[(\s|^)+(\s|$)], %Q[\1:plus:\2])


Radek Hnilica

No matter how far down the wrong road you’ve gone, turn back.
Turkish proverb