0

For example i've class like :

class QueryDSL
  def initialize(&block)
    instance_eval &block
  end

  def ==(value)
    "bla bla '#{value}'"
  end

  def test(param)
    param + param
  end
end

and class Query like :

class Query
  def self.where(&block)
    QueryDSL.new(&block)
  end
end

I suspect when execute :

Query.where{test == 9}

the output should be :

"bla bla 9 bla bla 9"

But i've got exception like :

`test': wrong number of arguments (0 for 1) (ArgumentError)

Is there any mistake from my code ? Thanks

4

1 回答 1

0

Your problem is that you're calling the QueryDSL#test method with no arguments when it is defined to take one argument, hence the

`test': wrong number of arguments (0 for 1) (ArgumentError)

error.

Either change the test method to not take arguments or to have a default for param or supply the argument in your block:

Query.where { test(6) == 9 }

That still won't call your == operator though, test doesn't return a QueryDSL instance so the == operator for whatever it does return will be used.

于 2012-04-30T04:55:55.600 回答