Skip to content

Instantly share code, notes, and snippets.

@samstokes
Created November 18, 2009 03:28

Revisions

  1. samstokes revised this gist Nov 21, 2009. 3 changed files with 3 additions and 3 deletions.
    2 changes: 1 addition & 1 deletion regex-groups-global.rb
    Original file line number Diff line number Diff line change
    @@ -3,4 +3,4 @@
    else
    raise "bad email"
    end
    # => "example.com"
    # => "example.com"
    2 changes: 1 addition & 1 deletion regex-groups-matchobject.rb
    Original file line number Diff line number Diff line change
    @@ -4,4 +4,4 @@
    else
    raise "bad email"
    end
    # => "example.com"
    # => "example.com"
    2 changes: 1 addition & 1 deletion regex-groups-subscript.rb
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,2 @@
    "[email protected]"[/@(.*)/, 1] or raise "bad email"
    # => "example.com"
    # => "example.com"
  2. samstokes revised this gist Nov 20, 2009. 4 changed files with 12 additions and 5 deletions.
    5 changes: 3 additions & 2 deletions regex-groups-global.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    if "[email protected]" =~ /@(.*)/
    $1
    else
    "bad email"
    end # => "example.com"
    raise "bad email"
    end
    # => "example.com"
    5 changes: 3 additions & 2 deletions regex-groups-matchobject.rb
    Original file line number Diff line number Diff line change
    @@ -2,5 +2,6 @@
    if match
    match[1]
    else
    "bad email"
    end # => "example.com"
    raise "bad email"
    end
    # => "example.com"
    4 changes: 4 additions & 0 deletions regex-groups-moreoneliners.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    $1 if "[email protected]" =~ /@(.*)/ or raise "bad email" # => "example.com"

    require 'andand'
    /@(.*)/.match("[email protected]").andand[1] # => "example.com"
    3 changes: 2 additions & 1 deletion regex-groups-subscript.rb
    Original file line number Diff line number Diff line change
    @@ -1 +1,2 @@
    "[email protected]"[/@(.*)/, 1] || "bad email" # => "example.com"
    "[email protected]"[/@(.*)/, 1] or raise "bad email"
    # => "example.com"
  3. samstokes revised this gist Nov 20, 2009. 4 changed files with 12 additions and 1 deletion.
    5 changes: 5 additions & 0 deletions regex-groups-global.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    if "[email protected]" =~ /@(.*)/
    $1
    else
    "bad email"
    end # => "example.com"
    6 changes: 6 additions & 0 deletions regex-groups-matchobject.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    match = /@(.*)/.match("[email protected]")
    if match
    match[1]
    else
    "bad email"
    end # => "example.com"
    1 change: 1 addition & 0 deletions regex-groups-subscript.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    "[email protected]"[/@(.*)/, 1] || "bad email" # => "example.com"
    1 change: 0 additions & 1 deletion regex-groups.rb
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    "[email protected]"[/@(.*)/, 1] # => "example.com"
  4. samstokes revised this gist Nov 18, 2009. 2 changed files with 1 addition and 3 deletions.
    1 change: 1 addition & 0 deletions regex-groups.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    "[email protected]"[/@(.*)/, 1] # => "example.com"
    3 changes: 0 additions & 3 deletions test.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +0,0 @@
    class ThisIsRuby < Hash
    def foo; 42; end
    end
  5. samstokes created this gist Nov 18, 2009.
    3 changes: 3 additions & 0 deletions test.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    class ThisIsRuby < Hash
    def foo; 42; end
    end