lib/regexp.rb in markdown_exec-1.6 vs lib/regexp.rb in markdown_exec-1.7
- old
+ new
@@ -23,19 +23,17 @@
# # Add file name, line number, line to captures_hash
# captures_hash[:file_name] = $~.pre_match.split("\n").last
# captures_hash[:line_number] = $~.pre_match.count("\n") + 1
# captures_hash[:line] = $&
-
-require 'English'
class Regexp
def gsub_format(input_str, format_str, context: {})
input_str.gsub(self) do
format(
format_str,
- context.merge($LAST_MATCH_INFO.names.each_with_object({}) do |name, hash|
- hash[name.to_sym] = $LAST_MATCH_INFO[name]
+ context.merge($~.names.each_with_object({}) do |name, hash|
+ hash[name.to_sym] = $~[name]
end)
)
end
end
end
@@ -90,10 +88,11 @@
def test_with_context
input_str = 'Jane is 25 years old.'
re = /^(?<name>\w+) is (?<age>\d+).*$/
fmt = "%<name>s's age is %<age>d and she lives in %<city>s"
- result = re.gsub_format(input_str, re, fmt, context: { city: 'New York' })
+ result = re.gsub_format(input_str, re, fmt,
+ context: { city: 'New York' })
assert_equal "Jane's age is 25 and she lives in New York", result
end
def test_with_context