J
Jamis Buck
Here's an interesting snafu I ran into today:
class Substring < String
def sub!(pat, r=nil, &b)
super(pat, r, &b)
# --------------------
p $1 # -> "ll"
# --------------------
end
end
s = Substring.new("hello")
s.sub!(/(ll)/, "r")
# --------------------
p $1 # -> nil!!!
# --------------------
The captured subgroup in the regexp is correctly assigned to $1 when
examined inside the overridden #sub! method, but when checked after
the invocation of the overridden method, the value of $1 is nil.
Why?
- Jamis
class Substring < String
def sub!(pat, r=nil, &b)
super(pat, r, &b)
# --------------------
p $1 # -> "ll"
# --------------------
end
end
s = Substring.new("hello")
s.sub!(/(ll)/, "r")
# --------------------
p $1 # -> nil!!!
# --------------------
The captured subgroup in the regexp is correctly assigned to $1 when
examined inside the overridden #sub! method, but when checked after
the invocation of the overridden method, the value of $1 is nil.
Why?
- Jamis