2017-04-11 11 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 

I以下のように私はAPの@selectcellsを追加すると、有効な値を得ます。それはちょうど私が外部から@selectcellsインスタンス変数の値にアクセスすることができないようです。

@[email protected]_rows 
    ap @selectcells 

答えて

1

アクセシブルにするためにゲッターを追加します。私は新しいメソッドを追加

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

:経由

def selectsells @selectsells end # or: attr_reader :selectsells 

とアクセスそれを:それはすべての後にインスタンス変数あり、そしてあなたはそれがグローバルなものであるかのようにそれを取得しようとしています。 ':未定義のメソッド 'selectcells 3' のために:Fixnumか(NoMethodError) - ところで3 @selectcells の正しい値がデフ 終了 – ssharma

+0

@ssharma' D 'をすることになっている @selectcellsをselectcellsされる '

- 今、私はエラーを取得しています'Neighbor_finder'のインスタンスになります。私は答えを更新しました。 – mudasobwa

関連する問題