Modified "showRE" anomalies

I modified “showRE” demonstrated in the online version of Programming
Ruby. My purpose was to have what struck me as a more convenient way
of analyzing the nuances of Ruby’s implementation of Regular
Expressions. My mod is:

def applyRE(a,re)
if a =~ re
puts "Val ‘#{a}’, Pat ‘#{re}’ ==> #{$`}<<#{$&}>>#{$’}"
else
puts "Val ‘#{a}’, Pat ‘#{re}’ ==> no match"
end
end

v1 = '12.34’
p1 = ‘/^\d*.?/’

applyRE(v1, /^\d*.?/) # Val ‘12.34’, Pat ‘#Regexp:0x2a6aa68’ ==>
<<12.>>34
applyRE(v1, p1) # Val ‘12.34’, Pat ‘/^\d*.?/’ ==> no match

The first invocation with a literal pattern argument applies the
pattern correctly but doesn’t display the pattern as a string, which I
intended.

The second invocation with a variable argument containing the pattern
displays the pattern as intended but doesn’t apply the pattern as
intended.

Is there a simple way to correct my error(s)?

TIA,
Richard

batsman@tux-chan:/tmp$ expand -t2 al.rb
def applyRE(a,re)
if a =~ re
puts “Val ‘#{a}’, Pat ‘#{re.source}’ ==> #{$`}<<#{$&}>>#{$'}”
else
puts “Val ‘#{a}’, Pat ‘#{re.source}’ ==> no match”
end
end

v1 = ‘12.34’
p1 = /^\d*.?/ # this a Regexp not a String!

applyRE(v1, /^\d*.?/)
applyRE(v1, p1)
batsman@tux-chan:/tmp$ /usr/bin/ruby -v
ruby 1.6.8 (2002-12-24) [i386-linux]
batsman@tux-chan:/tmp$ /usr/bin/ruby al.rb
Val ‘12.34’, Pat ‘^\d*.?’ ==> <<12.>>34
Val ‘12.34’, Pat ‘^\d*.?’ ==> <<12.>>34

batsman@tux-chan:/tmp$ expand -t2 ak.rb
def applyRE(a,re)
if a =~ re
puts “Val ‘#{a}’, Pat ‘#{re}’ ==> #{$`}<<#{$&}>>#{$'}”
else
puts “Val ‘#{a}’, Pat ‘#{re}’ ==> no match”
end
end

v1 = ‘12.34’
p1 = /^\d*.?/

applyRE(v1, /^\d*.?/)
applyRE(v1, p1)
batsman@tux-chan:/tmp$ ruby -v
ruby 1.8.0 (2003-03-03) [i686-linux]
batsman@tux-chan:/tmp$ ruby ak.rb
Val ‘12.34’, Pat ‘(?-mix:^\d*.?)’ ==> <<12.>>34
Val ‘12.34’, Pat ‘(?-mix:^\d*.?)’ ==> <<12.>>34

Regexp#to_s has changed.

···

On Mon, May 26, 2003 at 03:18:10PM +0900, Richard wrote:

I modified “showRE” demonstrated in the online version of Programming
Ruby. My purpose was to have what struck me as a more convenient way
of analyzing the nuances of Ruby’s implementation of Regular
Expressions. My mod is:

def applyRE(a,re)
if a =~ re
puts “Val ‘#{a}’, Pat ‘#{re}’ ==> #{$`}<<#{$&}>>#{$'}”
else
puts “Val ‘#{a}’, Pat ‘#{re}’ ==> no match”
end
end

v1 = ‘12.34’
p1 = ‘/^\d*.?/’

applyRE(v1, /^\d*.?/) # Val ‘12.34’, Pat ‘#Regexp:0x2a6aa68’ ==>
<<12.>>34
applyRE(v1, p1) # Val ‘12.34’, Pat ‘/^\d*.?/’ ==> no match


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

  • long f_ffree; /* free file nodes in fs */
  • long f_ffree; /* freie Dateiknoten im Dateisystem */
    – Seen in a translation

Mauricio,

Your two corrections:

#{re.source}
p1 = /^\d*.?/ # this a Regexp not a String!

are excellent. Thank you VERY much. This will help me and hopefully
others in the future use of REs in Ruby, Perl, etc.

···

Richard