我正在尝试在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
答案 0 :(得分:1)
在类中添加一个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