Skip to content
Browse files

Add explanation for =~ operator.

  • Loading branch information...
1 parent 5e34804 commit 32c152311b02892e0e8c6c73273e75b7d07a184a @radar radar committed Sep 20, 2010
Showing with 14 additions and 0 deletions.
  1. +5 −0 explanations/tilde_match.md
  2. +2 −0 fixtures/tilde_match.rb
  3. +7 −0 processor.rb
View
5 explanations/tilde_match.md
@@ -0,0 +1,5 @@
+`=~` operator
+
+The `=~` operator is used when we wish to compare a string to a regular expression. The string and regular expression may be placed on either side of this operator, Ruby does not care.
+
+If the regular expression matches the string then this operator returns the digit representing the character position of the first match, beginning from 0. If there is no match then `nil` is returned.
View
2 fixtures/tilde_match.rb
@@ -0,0 +1,2 @@
+"hello world" =~ /hello/
+/hello/ =~ "hello world"
View
7 processor.rb
@@ -158,6 +158,13 @@ def process_for(exp)
mark(:for) + result.join("\n")
end
+ # "string" =~ /regex/
+ def process_match3(exp)
+ mark(:tilde_match) + super
+ end
+
+ alias_method :process_match2, :process_match3
+
CALLS = [:require, :attr_accessor, :attr_reader, :attr_writer, :include, :extend]
SPECIALS = [:colon2]

0 comments on commit 32c1523

Please sign in to comment.
Something went wrong with that request. Please try again.