小编joh*_*ohk的帖子

Student:Class的未定义方法'each'

我想加载一个文件,将其内容拆分为数组,并将该类应用于内容.

class Student
    def initialize( name, grade )
        @name = name
        @grade = grade
        @grade = @grade.to_i
        @newgrade = @grade*1.45
    end

    def show()
        return "#{@name} ,#{@grade} , #{@newgrade}" 
    end
end

# Opening the file into an array
arr = File.open("exam_results.txt", "r+")
allStudents = Array.new

for a in arr
    b = a.split(",")
    name = b[0]
    score = b[1]
    allStudents << Student.new(@name, @grade)
end

for i in Student
    puts show()
end
Run Code Online (Sandbox Code Playgroud)

我越来越

Student的未定义方法'each':Class(NoMethodError)

在第28行,即puts show()行.有关如何进一步了解这一点的任何线索?

ruby arrays class nomethoderror

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

对数组进行排序,并选择最小数字?

我创建了几个包含多个整数的数组.现在我希望整数排序,最低.比如说,我有一个数组:6,6,1,2,4,4,我希望它被排序:1,2,4,4,6,6.另外,无论如何,我可以让ruby识别4个最低值,并以某种方式显示它们?我试图用.show来解决这个问题,但是由于我对编程很新,所以我对收到的结果感到困惑.

ruby arrays sorting

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

节点聊天给投掷者;//未处理的错误事件

所以我建立了一个小型的基本聊天系统:

服务器代码:

    var io = require('socket.io').listen(8000);

// open the socket connection
io.sockets.on('connection', function (socket) {

   // listen for the chat even. and will recieve
   // data from the sender.
   socket.on('chat', function (data) {

      // default value of the name of the sender.
      var sender = 'unregistered';

      // get the name of the sender
      var name = socket.nickname;
         console.log('Chat message by ', name);
         console.log('error ', err);
         sender = name;


      // broadcast data recieved from the sender
      // to others who are …
Run Code Online (Sandbox Code Playgroud)

html error-handling undefined node.js server

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

Ruby - 不会将文件加载到数组中

我有ruby的问题,它不会将我拥有的文件加载到指定的数组中.

class Dancer
  def Initialize (couplenumber, score1, score2, score3, score4, score5, score6, score7)
    @couplenumber = couplenumber
    @score1 = score1
    @score2 = score2
    @score3 = score3
    @score4 = score4
    @score5 = score5
    @score6 = score6
    @score7 = score7
  end

  def show()
    return "Couple Number: #{@couplenumber}. Scores: #{@score1}, #{@score2}, #{@score3}, #{@score4}, #{@score5}, #{@score6}, #{@score7}."
  end
end

results = File.open("danceresult.txt", "r+")
dancescores = []

# Splitting dance scores with "," and putting into arrays.
for dancers in results
  a = dancers.split(",")
  couplenumber = …
Run Code Online (Sandbox Code Playgroud)

ruby arrays

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