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 <xref
linkend="#{label}"/> 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[ <xref linkend="#{id}"/> ])
s.gsub!(%r[^#{word} ], %Q[<xref linkend="#{id}"/> ])
s.gsub!(%r[ #{word}$], %Q[ <xref linkend="#{id}"/>])
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[ <xref linkend="#{id}"/> ])
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 <xref/> 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 <Radek at Hnilica dot CZ>
=======================================
No matter how far down the wrong road you've gone, turn back.
Turkish proverb