小编fli*_*one的帖子

将json解析为对象ruby

我查看了不同的资源,但仍然对如何将json格式解析为自定义对象感到困惑

class Resident
  attr_accessor :phone, :addr

  def initialize(phone, addr)
      @phone = phone
      @addr = addr
  end
end    
Run Code Online (Sandbox Code Playgroud)

和JSON文件

{
  "Resident": [
    {
      "phone": "12345",
      "addr":  "xxxxx"
    }, {
      "phone": "12345",
      "addr": "xxxxx"
    }, {
      "phone": "12345",
      "addr": "xxxxx"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

将json文件解析为3 Resident对象数组的正确方法是什么?

ruby json

27
推荐指数
3
解决办法
4万
查看次数

邻接列表和图表

我正在尝试通过邻接列表构建图形,这意味着我需要所有节点的列表,并且在每个节点类中,我还需要一个数据结构来保存所有相邻节点.只是想知道最好的结构是什么(快速搜索目标节点类).数组是否有效?

ruby algorithm

10
推荐指数
1
解决办法
4463
查看次数

在ruby中使用散列排序的正确方法

我是ruby的新手,我正在尝试编写一个dijkstra函数,但我的哈希排序似乎根本不起作用

def distance(start_code, end_code, map)
#initialize hash for distance 
#distance are initialized to -1
dist_hash=Hash.new()
start_hash=Hash.new()
parent_hash=Hash.new()
close_list=Array.new()
find=-1
map.citylist.each do |e|
  dist_hash[e]=[+1.0/0.0]
end

start_hash[start_code]=0
parent_hash[start_code]=start_code

while (start_hash.empty?)==false


   #sort the hash
  start_hash.sort_by {|k,v| v}
  puts 'value'      
  puts start_hash.values()  
   #pop the first item in the hash
  h=start_hash.shift()
  curr_key=h[0]
  curr_val=h[1]
  curr_city=map.findcity(curr_key)
  close_list<<curr_city.code

   #for every one in adjacent list
  curr_city.get_adj_city().each do |e|


     #if it in the close list then igonore
    if close_list.include?(e)==false  
       #if it is not in the start_hash then add to start …
Run Code Online (Sandbox Code Playgroud)

ruby hash

3
推荐指数
2
解决办法
4738
查看次数

关于指针数组的基础

我试图struct some_struct在C中声明一个指针数组

我可不可以做:

some_struct* arr[10];
Run Code Online (Sandbox Code Playgroud)

代替:

some_struct** arr=(some_struct**)malloc(10*sizeof(some_struct*));
Run Code Online (Sandbox Code Playgroud)

有什么区别?

c arrays pointers

1
推荐指数
1
解决办法
71
查看次数

标签 统计

ruby ×3

algorithm ×1

arrays ×1

c ×1

hash ×1

json ×1

pointers ×1