2017-04-11 463 views
-2

我想在ruby中運行下面的代碼。 @selectcells的值爲零。有什麼建議麼?在Ruby中訪問實例變量

class Neighbor_finder 


@@path = "/home/xyz" 

def initialize(enodebid,earfcn_dl,distance,spread,mcc,mnc) 
@site_details=Hash.new 
@radius = distance 
@earfcn_dl=earfcn_dl 
@spread=spread 
@sourceenb=enodebid 
@plmn=mcc+mnc 
@mcc=mcc 
@mnc=mnc 
puts "here" 
end 

def enodeb_cell_finder 
    [email protected] 
    [email protected]_dl 
    con=Mysql.new 'localhost','root','root','celldb' 
    puts "here11" 
    @site_details=con.query "SELECT * FROM enodeb WHERE enodeb_id LIKE '%#{enodebid}%'" 

    u = @site_details.fetch_hash 
    ap u 
    con.close 

    @con1 = Mysql.new 'localhost','root','root','celldb' 
    @selectnodes1 = @con1.query "SELECT * FROM cell_db_eutran_cells WHERE sitename LIKE '%#{enodebid}%' AND earfcn_dl LIKE '#{earfcn_dl}'" 

    @[email protected]_rows 


    end 
end 


d=Neighbor_finder.new("936144","2581",20,60,'311','850').enodeb_cell_finder 

ap @selectcells 

當我添加如下所示,我得到了一個有效的值的AP @selectcells。看來我無法從外部訪問實例變量@selectcells的值。

@[email protected]_rows 
    ap @selectcells 

回答

1

將一個吸氣劑添加到該類中以使其可訪問。這是實例變量畢竟,和你的努力得到它,彷彿它是一個全球性的:通過

def selectsells 
    @selectsells 
end 
# or: attr_reader :selectsells 

和訪問:

d = Neighbor_finder.new("936144","2581",20,60,'311','850') 
ap d.enodeb_cell_finder 
ap d.selectsells 
+0

我增加了新的方法。現在我得到的錯誤 - '

':未定義的方法'selectcells' 3:Fixnum對象(NoMethodError) - BTW 3 @selectcells的正確值 高清selectcells @selectcells 結束 – ssharma

+0

@ssharma'D'應該是「Neighbor_finder」的一個實例。我已經更新了答案。 – mudasobwa