Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.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 shuffle方法没有';不能使用json解析的数组_Ruby_Json - Fatal编程技术网

Ruby shuffle方法没有';不能使用json解析的数组

Ruby shuffle方法没有';不能使用json解析的数组,ruby,json,Ruby,Json,我有一个json文件,如下所示: { "data": "12-14.02.2013", "uwagi": ["Pierwsza","Druga","Trzecia"], "tytul": "Kolokwium 1", "przedmiot": "Muzykologia", "questions" : [ { "punkty": 2, "pytanie" : "Czy lubi Pan reggae?", "odpowiedz" : "Ta

我有一个json文件,如下所示:

{
  "data": "12-14.02.2013",
  "uwagi": ["Pierwsza","Druga","Trzecia"],
  "tytul": "Kolokwium 1",
  "przedmiot": "Muzykologia",
  "questions" : [
    {
     "punkty": 2,
     "pytanie" : "Czy lubi Pan reggae?",
     "odpowiedz" : "Tak, bardzo",
     "verbatim" : "if (a){b=5}"
    },
    {
     "punkty": 3,
     "pytanie" : "Czy lubi Pan hip-hop?",
     "odpowiedz" : "Nieszczególnie"
    },
    {
     "punkty": 5,
     "pytanie" : "Czy lubi Pan disco polo?",
     "odpowiedz" : "Nie"
    }]
}
require 'json'

file = open("pytania.json")
json = file.read

parsed = JSON.parse(json)

questionsArr = parsed["questions"]
questionsArr.shuffle
puts questionsArr
ruby代码如下:

{
  "data": "12-14.02.2013",
  "uwagi": ["Pierwsza","Druga","Trzecia"],
  "tytul": "Kolokwium 1",
  "przedmiot": "Muzykologia",
  "questions" : [
    {
     "punkty": 2,
     "pytanie" : "Czy lubi Pan reggae?",
     "odpowiedz" : "Tak, bardzo",
     "verbatim" : "if (a){b=5}"
    },
    {
     "punkty": 3,
     "pytanie" : "Czy lubi Pan hip-hop?",
     "odpowiedz" : "Nieszczególnie"
    },
    {
     "punkty": 5,
     "pytanie" : "Czy lubi Pan disco polo?",
     "odpowiedz" : "Nie"
    }]
}
require 'json'

file = open("pytania.json")
json = file.read

parsed = JSON.parse(json)

questionsArr = parsed["questions"]
questionsArr.shuffle
puts questionsArr
提问后不会排序。洗牌

我试过了。按{rand}排序并洗牌解析[“问题”],但没有成功


怎么了?

你需要
洗牌数组

require 'json'

file = open("pytania.json")
json = file.read

parsed = JSON.parse(json)

questionsArr = parsed["questions"]
questionsArr.shuffle!
puts questionsArr
单独使用
shuffle
方法将返回一个新的shuffled数组,并且不会更改原始
questionsArr
数组。所以你应该要么使用bang
shuffle
方法,该方法将更改原始的
问题arr
数组,如上图所示,或将
shuffle
的结果放入新变量中,并按如下方式使用:

require 'json'

file = open("test.json")
json = file.read

parsed = JSON.parse(json)

questionsArr = parsed["questions"]
shuffledArr = questionsArr.shuffle
puts shuffledArr

你需要
洗牌数组

require 'json'

file = open("pytania.json")
json = file.read

parsed = JSON.parse(json)

questionsArr = parsed["questions"]
questionsArr.shuffle!
puts questionsArr
单独使用
shuffle
方法将返回一个新的shuffled数组,并且不会更改原始
questionsArr
数组。所以你应该要么使用bang
shuffle
方法,该方法将更改原始的
问题arr
数组,如上图所示,或将
shuffle
的结果放入新变量中,并按如下方式使用:

require 'json'

file = open("test.json")
json = file.read

parsed = JSON.parse(json)

questionsArr = parsed["questions"]
shuffledArr = questionsArr.shuffle
puts shuffledArr

你想对数组进行排序或洗牌吗?我尝试使数组元素顺序随机你想对数组进行排序或洗牌吗?我尝试使数组元素顺序随机这肯定是因为Swift及其感叹号的意思,谢谢:)详细说明--
洗牌
确实是在洗牌数组,但它正在返回洗牌数组<代码>洗牌
洗牌并更新数组。这肯定是因为Swift及其感叹号的意思,谢谢:)详细说明--
shuffle
确实在洗牌数组,但它会返回洗牌后的数组<代码>洗牌洗牌数组并在适当的位置更新它。