Skip to content

Instantly share code, notes, and snippets.

@lanrion
Last active August 29, 2015 14:01
Show Gist options
  • Save lanrion/cef99e8e83c8476ffafc to your computer and use it in GitHub Desktop.
Save lanrion/cef99e8e83c8476ffafc to your computer and use it in GitHub Desktop.
正则表达式中(),所引起的全局变量的改变。

http://stackoverflow.com/questions/288573/1-and-1-in-ruby

  def method_missing(meth, *args, &block)
    if meth.to_s =~ /^find_by_(.+)$/
     # $1 即为(.+所得)
      run_find_by_method($1, *args, &block)
    else
      super # You *must* call super if you don't handle the
            # method, otherwise you'll mess up Ruby's method
            # lookup.
    end
  end

如果在正则表达式中,同时有多个(),则会依次赋值$1, $2, $3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment