Ruby shuffle方法没有';不能使用json解析的数组
我有一个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
{
"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
数组。所以你应该要么使用bangshuffle
方法,该方法将更改原始的问题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
数组。所以你应该要么使用bangshuffle
方法,该方法将更改原始的问题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
确实在洗牌数组,但它会返回洗牌后的数组<代码>洗牌代码>洗牌数组并在适当的位置更新它。