2014-04-03 104 views
1

從R插件導入數據到我的ruby腳本後,我有一個數組看起來像一個散列。有人知道這裏發生了什麼嗎?Ruby關聯數組?

r_edges.class 
#=> Array < Object 

r_edges[0] 
#=> returns data from index 0 as expected 

r_edges['GO:0010035'] 
# r_edges['GO:0010035']['edges'] also works 
#=> returns data at the index that is named 'GO:0010035', see the to_s output below  

r_edges 
#=> [ 0] [ 
[0] [ 
    [ 0] 2, 
    [ 1] 3, 
    [ 2] 4, 
    [ 3] 5, 
    [ 4] 6, 
    [ 5] 7, 
    [ 6] 8, 
    [ 7] 9, 
...... etc. 

r_edges.to_s 
#=> "[GO:0006260=[edges=[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 28, 29, 31, 32, 33, 35, 36, 37, 38, 39, 40, 41, 42, 43, 45, 46, 48, 51, 55, 56, 57, 58, 59, 60, 63, 67, 69, 71, 78, 81, 83, 84, 89, 90, 91, 92, 94, 95, 96, 97, 99, 100, 104, 108, 109, 112, 116, 117, 123, 124]], GO:0006271=[edges=[1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 28, 29, 36, 40, 46, 67]], GO:0022616=[edges=[1, 2, .... etc. 

我使用Ruby Rserve gem並用的R S4對象,像這樣有作用中的數據:

con = con=Rserve::Connection.new 
r_edges = con.eval("SOME S4 OBJECT").to_ruby 

它實際上是有幫助的,我得到了「命名的指數」的數據時,我循環遍歷數組,但我不知道如何訪問該值。

+0

你使用任何其他庫或任何東西?你如何導入數據?另外,什麼版本的Ruby? – iamnotmaynard

+0

Ruby本身具有像{:a =>'a',:b =>'b'}的散列支持。 – xis

+0

@iamnotmaynard更新。 – Josh

回答

2

它的行爲很有趣,因爲Ruby非常棒!它允許您將功能推入對象而無需進行子類化。

從Rserve寶石的rlist class

# Returns an Array with module WithNames included 
# * Unnamed list: returns an Array 
# * Named List: returns a Hash. Every element without explicit name receive 
# as key the number of element, 1-based 
# 
def to_ruby 
    data=to_a 
    data.extend WithNames 
    [email protected] 
    data 
end 

哪個WithNames module猛推到數組,這對[]幾個方法(環顧四周線121)。

+0

ahhhh!我在回購庫中搜索'class Array',看看他們是否打開它,但沒有考慮只擴展對象。 – Josh