Reputation: 121000
According to documentation, #scan
should accept both String
and Regexp
instances as parameter. But tests show strange behaviour:
▶ cat scantest.rb
#!/usr/bin/ruby
puts '='*10
puts 'foo'.scan '.'
puts '='*10
puts 'foo'.scan /./
puts '='*10
▶ rb scantest.rb
# ⇒ ==========
# ⇒ ==========
# ⇒ f
# ⇒ o
# ⇒ o
# ⇒ ==========
Inside both pry
and irb
, it doesn't properly scan for a string as well. What am I doing wrong?
Upvotes: 0
Views: 101
Reputation: 369264
With string '.'
, it scans for literal dots:
'foo'.scan '.'
# => []
'fo.o'.scan '.'
# => ["."]
While with regular expression /./
, it matches any characters (except newline):
'foo'.scan /./
# => ["f", "o", "o"]
"foo\nbar".scan /./
# => ["f", "o", "o", "b", "a", "r"]
Upvotes: 2
Reputation: 12330
your scan
should have a parameter
that match the string you want to scan
otherwise it will return empty arrray
My case:
irb(main):039:0> "foo".scan("o")
=> ["o", "o"]
Your case
'foo'.scan '.'
# => []
There is no dot.
present on the 'foo'
string so scan
return empty array
Upvotes: 0