我一直在运行这个意外的错误令牌。我希望程序在不调用ruby的情况下运行。例如,我应该能够ruby program1.rb
而不是program1.rb poem.txt
。
这是错误消息:
program1.rb --backwards poem.txt
./program1.rb: line 1: syntax error near unexpected token `('
./program1.rb: line 1: `def backlines(line_array)'
这是我的代码:
def backlines(*line_array)
end
def backwards(line_array)
end
def backchars(line_array)
end
def main
file = File.new(ARGV[1], "r") do |file|
line_array = file.readlines
*line_array = line_array.reverse
if ARGV[0] == "--backlines"
*backwards_poem = backlines(line-array)
#you can manipulate "backwards_poem" however you want
elsif ARGV[0] == "--backwards"
backwards(line_array)
elsif ARGV[0] == "--backchars"
backchars(line_Array)
end
# passing a *line_array into a function
end
main
答案 0 :(得分:1)
您是否在顶部的脚本中执行了ruby
?例如:
#!/usr/bin/ruby
答案 1 :(得分:0)
#!/usr/bin/ruby
def backlines(*line_array)
end
def backwards(line_array)
end
def backchars(line_array)
end
def main
puts ARGV
# File open not new ... this block requires the end below.
File.open(ARGV[1], "r") do |file|
line_array = file.readlines
*line_array = line_array.reverse
if ARGV[0] == "--backlines"
*backwards_poem = backlines(line_array)
#you can manipulate "backwards_poem" however you want
elsif ARGV[0] == "--backwards"
backwards(line_array)
elsif ARGV[0] == "--backchars"
backchars(line_array)
end
end
# passing a *line_array into a function
end
如果你打电话给./program1.rb --backwards file
你也有line-array,line_array和line_Array,我认为它应该是一个变量。