Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ruby中的Access实例变量_Ruby_Instance Variables - Fatal编程技术网

ruby中的Access实例变量

ruby中的Access实例变量,ruby,instance-variables,Ruby,Instance Variables,我正在尝试用ruby运行下面的代码。我得到@selectcells的一个零值。有什么建议吗 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

我正在尝试用ruby运行下面的代码。我得到@selectcells的一个零值。有什么建议吗

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
    enodebid=@sourceenb
    earfcn_dl=@earfcn_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}'"

    @selectcells=@selectnodes1.num_rows


    end
 end


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

ap @selectcells

当我添加一个ap@selectcells,如下所示时,我得到了一个有效值。看起来我无法从外部访问实例变量@selectcells的值

    @selectcells=@selectnodes1.num_rows
    ap @selectcells

向类添加getter以使其可访问。它毕竟是一个实例变量,您试图将其作为一个全局变量:

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

我添加了新方法。现在我得到了错误-
”:3:Fixnum(NoMethodError)的未定义方法
selectcells)-顺便说一句,3是@selectcells def selectcells@selectcells的正确值end@ssharma
d
应该是
Neighbor\u finder
的一个实例。我已经更新了答案。