2014-02-20 20 views
0

我有兩個自制的獨立寶石,但它們都具有相同的功能,例如foo,而foo用C實現,並且它位於相同的命名空間下,那麼是否有方法可以確定我是否需要這兩個寶石,我使用的版本是foo,還是確保我實際使用寶石foo功能之一的方法,有人建議使用cerr想要確定在兩個獨立的寶石中使用哪個函數

雖然我知道他們是多餘的,但到目前爲止,我不想刪除他們由於某些原因。

+0

能否請你添加更多的背景是什麼?請告訴我們,如果可以的話,爲什麼這兩個寶石使用相同的命名空間,爲什麼你不能改變它們,所以他們不這樣做,爲什麼你不能刪除其中的一個。 –

回答

1

我搜索了「ruby以編程方式查找函數定義」,並找到了這個答案:How can I get source code of a method dynamically and also which file is this method locate in

這一點,再加上一些簡單的IRB實驗使我相信,你想要做的事,如:

filename, lineno = method(:foo).source_location 

if filename == "blah" 
    puts "looks like :foo is defined in 'blah'" 
elsif filename == "blargh" 
    puts "looks like :foo is defined in 'blargh'" 
end 

這裏是source_location一些文檔:http://ruby-doc.org/core-1.9.3/Method.html#method-i-source_location

相關問題