gì @ đứng tại các mã Ruby sau:gì @ đứng trong một tên hàm của Ruby
module TestRocket
extend Module.new { attr_accessor :out }
def _test(a, b); send((call rescue()) ? a : b); end
def [email protected]; _show _test :_pass, :_fail end
def [email protected]; _show _test :_fail, :_pass end
def [email protected]; _show _pend; end
def [email protected]; _show _desc; end
def _show(r); (TestRocket.out || $>) << r; r end
def _pass; " OK\n"; end
def _fail; " FAIL @ #{source_location * ':'}\n"; end
def _pend; "PENDING '#{call}' @ #{source_location * ':'}\n"; end
def _desc; " FIRE '#{call}'!\n"; end
end
Proc.send :include, TestRocket
Sau đó, điều này được sử dụng như:
+-> { Die.new(2) }
--> { raise }
+-> { 2 + 2 == 4 }
như thế nào @ biến thành ' -> 'trong tên hàm?
câu hỏi hay. Tôi chưa bao giờ thấy điều này. Nhưng nếu bạn thử và chạy '+ -> {2 + 2 == 4}' mà không có nó, bạn sẽ gặp lỗi 'NoMethodError: phương thức undefined' + @ 'cho # '. Vì vậy, tôi đoán đây là một phương pháp kỳ diệu từ ruby 1.9 –
Kho lưu trữ của TestRocket là https://github.com/peterc/testrocket –