ArgumentError on Ruby - PullRequest
       3

ArgumentError on Ruby

0 голосов
/ 22 апреля 2020

Я пытался запустить приведенный ниже код Ruby, но продолжаю получать следующую ошибку:

Failed: ArgumentError: неверное количество аргументов (задано 1, ожидается 0)

stacktrace: ./basic_read_write.rb:3:in write_data_to_file' /tmp/test.rb:5:in блок (2 уровня) в

# writes the number of lines then each line as a string.

def write_data_to_file()
   a_file=File.new("mydata.txt","w")
   a_file.puts('5')
   a_file.puts('Fred')
   a_file.puts('Sam')
   a_file.puts('Jill')
   a_file.puts('Jenny')
   a_file.puts('Zorro')
   a_file.close

end


def read_data_from_file()
  a_file=File.new("mydata.txt","r")

  count = a_file.gets.to_i
  puts count.to_s
  for i in 0..count.to_i
    puts a_file.gets
  end 
  a_file.close


def main
  # open for writing
  write_data_to_file()


   # open for reading
  read_data_from_file()

end

main
end 

1 Ответ

1 голос
/ 22 апреля 2020

Приведенный выше код выглядит хорошо, за исключением оператора end в методах. Каждый метод заканчивается концом. Согласно вашему коду, def read_data_from_file () имеет конец в конце после def main.

def write_data_to_file()
   a_file=File.new("mydata.txt","w")
   a_file.puts('5')
   a_file.puts('Fred')
   a_file.puts('Sam')
   a_file.puts('Jill')
   a_file.puts('Jenny')
   a_file.puts('Zorro')
   a_file.close

end


def read_data_from_file()
  a_file=File.new("mydata.txt","r")

  count = a_file.gets.to_i
  puts count.to_s
  for i in 0..count.to_i
    puts a_file.gets
  end 
  a_file.close
end

def main
  # open for writing
  write_data_to_file()


   # open for reading
  read_data_from_file()

end

main

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...