ruby 中的 'where' 行为是什么?
What is the 'where' behavior in ruby?
我正在尝试编写一个模块来通过我的所有测试。它看起来像是一个哈希数组。我想弄清楚 'where' 逻辑是什么?所以我可以尝试编写一个模块来通过所有测试。
测试:
require 'test/unit'
class WhereTest < Test::Unit::TestCase
def setup
@boris = {:name => 'Boris The Blade', :quote => "Heavy is good. Heavy is reliable. If it doesn't work you can always hit them.", :title => 'Snatch', :rank => 4}
@charles = {:name => 'Charles De Mar', :quote => 'Go that way, really fast. If something gets in your way, turn.', :title => 'Better Off Dead', :rank => 3}
@wolf = {:name => 'The Wolf', :quote => 'I think fast, I talk fast and I need you guys to act fast if you wanna get out of this', :title => 'Pulp Fiction', :rank => 4}
@glen = {:name => 'Glengarry Glen Ross', :quote => "Put. That coffee. Down. Coffee is for closers only.", :title => "Blake", :rank => 5}
@fixtures = [@boris, @charles, @wolf, @glen]
end
def test_where_with_exact_match
assert_equal [@wolf], @fixtures.where(:name => 'The Wolf')
end
def test_where_with_partial_match
assert_equal [@charles, @glen], @fixtures.where(:title => /^B.*/)
end
def test_where_with_mutliple_exact_results
assert_equal [@boris, @wolf], @fixtures.where(:rank => 4)
end
def test_with_with_multiple_criteria
assert_equal [@wolf], @fixtures.where(:rank => 4, :quote => /get/)
end
def test_with_chain_calls
assert_equal [@charles], @fixtures.where(:quote => /if/i).where(:rank => 3)
end
end
仔细看你的第一个测试:
@fixtures.where(:name => 'The Wolf')
@fixtures
对象属于 class Array
,您正在对其调用名称为 where
的方法。因此,要使此测试通过,您必须在 Array 或其父 class 之一上定义一个名为 where
的方法。作为第一次尝试,您可以这样写:
class Array
def where(options)
end
end
我想这就是你要问的。编写上面的代码后,您应该从测试中得到不同的错误消息。您的工作是在 where
方法中编写代码以使您的所有测试都通过。
我正在尝试编写一个模块来通过我的所有测试。它看起来像是一个哈希数组。我想弄清楚 'where' 逻辑是什么?所以我可以尝试编写一个模块来通过所有测试。
测试:
require 'test/unit'
class WhereTest < Test::Unit::TestCase
def setup
@boris = {:name => 'Boris The Blade', :quote => "Heavy is good. Heavy is reliable. If it doesn't work you can always hit them.", :title => 'Snatch', :rank => 4}
@charles = {:name => 'Charles De Mar', :quote => 'Go that way, really fast. If something gets in your way, turn.', :title => 'Better Off Dead', :rank => 3}
@wolf = {:name => 'The Wolf', :quote => 'I think fast, I talk fast and I need you guys to act fast if you wanna get out of this', :title => 'Pulp Fiction', :rank => 4}
@glen = {:name => 'Glengarry Glen Ross', :quote => "Put. That coffee. Down. Coffee is for closers only.", :title => "Blake", :rank => 5}
@fixtures = [@boris, @charles, @wolf, @glen]
end
def test_where_with_exact_match
assert_equal [@wolf], @fixtures.where(:name => 'The Wolf')
end
def test_where_with_partial_match
assert_equal [@charles, @glen], @fixtures.where(:title => /^B.*/)
end
def test_where_with_mutliple_exact_results
assert_equal [@boris, @wolf], @fixtures.where(:rank => 4)
end
def test_with_with_multiple_criteria
assert_equal [@wolf], @fixtures.where(:rank => 4, :quote => /get/)
end
def test_with_chain_calls
assert_equal [@charles], @fixtures.where(:quote => /if/i).where(:rank => 3)
end
end
仔细看你的第一个测试:
@fixtures.where(:name => 'The Wolf')
@fixtures
对象属于 class Array
,您正在对其调用名称为 where
的方法。因此,要使此测试通过,您必须在 Array 或其父 class 之一上定义一个名为 where
的方法。作为第一次尝试,您可以这样写:
class Array
def where(options)
end
end
我想这就是你要问的。编写上面的代码后,您应该从测试中得到不同的错误消息。您的工作是在 where
方法中编写代码以使您的所有测试都通过。