irb

2013-02-07 27 views
10

में आरएसपीसी उम्मीदों का उपयोग कैसे करें I irb में [1,2,3].should include(1) का उपयोग करना चाहते हैं। मैंने कोशिश की:irb

~$ irb 
1.9.3p362 :001 > require 'rspec/expectations' 
=> true 
1.9.3p362 :002 > include RSpec::Matchers 
=> Object 
1.9.3p362 :003 > [1,2,3].should include(1) 
TypeError: wrong argument type Fixnum (expected Module) 
    from (irb):3:in `include' 
    from (irb):3 
    from /home/andrey/.rvm/rubies/ruby-1.9.3-p362/bin/irb:16:in `<main>' 

लेकिन यह it's a valid case हालांकि काम नहीं करता है। मैं [1,2,3].should include(1) का उपयोग कैसे कर सकता हूं?

उत्तर

13

आप करीब हैं, लेकिन शीर्ष स्तर पर include पर कॉल करके आप Module#include पर कॉल करेंगे। इसके आस-पास पहुंचने के लिए आपको मूल शामिल विधि को हटाने की आवश्यकता है ताकि RSpec के include को इसके बजाय बुलाया जा सके।

> method :include 
=> #<Method: main.include> 

ठीक है:

सबसे पहले यह पता लगाने की जहां प्रणाली include से आता हैं। ऐसा लगता है कि यह main में परिभाषित किया गया है। यह रूबी शीर्ष-स्तरीय वस्तु है। तो चलो का नाम बदलने के जाने और मूल हटाने में शामिल हैं:

> class << self; alias_method :inc, :include; remove_method :include; end 

अब हम व्यापार करने के लिए नीचे लाने के लिए कर सकते हैं:

> require 'rspec' 
> inc RSpec::Matchers 
> [1,2,3].should include(1) 
=> true