2012-10-22 17 views
0

在Eclipse中使用Java將在什麼類/模塊,它一直是非常容易的,我確定在何處定義的方法:只需按Ctrl +單擊方法調用和它帶給你的在其包含的類中定義方法。很明顯,它使Eclipse發生了很多事情,但即使使用普通的Java,我們仍然可以重新導入語句。如何確定方法在

方法查找似乎與Rails開發非常困難。

下面是一個例子:

require 'test_helper' 

class UserStoriesTest < ActionDispatch::IntegrationTest 
    fixtures: products 

    LineItem.delete_all 
    Order.delete_all 
    ruby_book = products(:ruby) 

    get "/" 
    assert_response :success 
    assert_template "index" 

    cart = Cart.find(session[:cart_id]) 
    assert_equal 1, cart.line_items.size 
    assert_equal ruby_book, cart.line_items[0].product 

    get "/orders/new" 
    assert_response :success 
    assert_template "new" 
end 

比方說,我想借此在assert_equal方法一探究竟。如果我用Java和包含assert_equal類的工作很可能會通過import語句在這個類與它的位置明確指定(例如java.lang.SomeCoreClass)描述。因爲我不知道在哪裏的地獄這種方法定義,我最終做的是谷歌搜索「assert_equal」地發現,我在調查方法(可能和希望)在測試::單位定義::斷言

我很感興趣,從幾個人對他們的做法聽到確定方法的位置。

+0

你問哪個編輯器支持 「去定義」 CTRL單擊? RubyMine可以做到這一點。 – nurettin

回答

4

試試這個:

class Bong 
    def smoke 
    puts "smoke" 
    end 
end 

Bong.method(:smoke) 
Bong.method(:smoke).source_location 
+0

lmao最好回答有史以來。道具。 – kwikness

+0

一個相關的例子總是有幫助;) – ChuckE

1

您可以使用method(:<method_name>).owner跟蹤類的方法。在你的情況下,它將是method(:assert_equal).owner

method#owner

+0

是的,但這對我有幫助嗎?我不能只是啓動irb並鍵入assert_equal.owner。 – kwikness

+0

你可以在你的項目範圍內使用它來找出哪個類正在定義你的方法。如果你在irb中定義你的類,那麼你應該可以在irb中使用這個方法。 – Raghu

+0

在你的測試用例文件中只打印p方法(:assert_equal).owner。 – Raghu