moon
moon

Reputation: 11

Syntax error unexpected token '('

I kept running to this unexpected error token. I wanted the program to run without invoking ruby. For instances, instead of ruby program1.rb, i should be able to program1.rb poem.txt.

This is the error message:

 program1.rb --backwards poem.txt
./program1.rb: line 1: syntax error near unexpected token `('
./program1.rb: line 1: `def backlines(line_array)'

This is my code:

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

Upvotes: 0

Views: 1556

Answers (2)

corn3lius
corn3lius

Reputation: 5005

#!/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

this should start you off if your call it like ./program1.rb --backwards file

you also had line-array , line_array and line_Array which should al be one variable i think.

Upvotes: 0

chiggins
chiggins

Reputation: 11

Have you executed ruby in your script at the top? eg:

#!/usr/bin/ruby

Upvotes: 1

Related Questions