0

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
Holger Just
  • 52,918
  • 14
  • 115
  • 123
moon
  • 11
  • 7
  • You're running this file from the command line without passing it to `ruby`? Sounds then like you need a shebang http://stackoverflow.com/questions/10376206/what-is-the-preferred-bash-shebang – Michael Berkowski Nov 08 '16 at 21:13
  • 2
    you are missing an `end` in main method – usha Nov 08 '16 at 21:21
  • There's no need to do `*line_array =`, that's an anti-pattern. Just use `line_array =`. Likewise, don't define a `main` method, that's pointless. It's understood that Ruby never has such a thing and this bucks convention. You should also use [OptionParser](http://ruby-doc.org/stdlib-2.3.1/libdoc/optparse/rdoc/OptionParser.html) to handle command-line arguments. – tadman Nov 08 '16 at 21:29
  • Which ver of ruby are you running? the splat "*" only exist in ruby 2.0 – Du D. Nov 08 '16 at 21:30
  • @tadman I see, I've fixed it and when I executed the code, it didn't return any output. The file poem.txt that I used to test, only contains 3 lines – moon Nov 08 '16 at 21:37

2 Answers2

1

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

#!/usr/bin/ruby
chiggins
  • 11
  • 3
  • I've tried but I got a syntax error, unexpected end-of-input, expecting keyword_end :( – moon Nov 08 '16 at 21:16
  • 2
    "I've tried but I got a syntax error, unexpected end-of-input, expecting keyword_end" And rightly so! You _are_ missing a keyword `end`. So this proves that putting the shebang line at the top causes your script to work _correctly_! It is parsed as ruby and the ruby parser is telling you something you need to fix. – matt Nov 08 '16 at 21:23
  • I've put the shebang line at the top, no errors returned, but also there was no output – moon Nov 08 '16 at 21:27
  • I don't see anywhere in your code where it would output (ie: with `puts` or `print`). If you wanted this to output, you'd either want those methods (`backlines`, etc) to output your results, or return something to `main` for it to output. (Did those two options make sense?) – chiggins Nov 08 '16 at 21:39
  • @chiggins like this? elsif ARGV[0] == "--backwards" puts backwards(line_array) – moon Nov 09 '16 at 00:11
  • I was thinking either outputting something from the method (expedient), or returning something from the method and outputting after the conditionals. So that would be something like `def backwards; "backwards!"; end` and then have `elsif ARGV[0] == "--backwards"; retval = backwards();`. Then you'd `puts retval` at the end of main. – chiggins Nov 11 '16 at 04:34
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

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.

corn3lius
  • 4,857
  • 2
  • 31
  • 36
  • Thanks, I think the code worked but it doesnt returned any output. The poem.txt which I used to test, contains three lines. – moon Nov 08 '16 at 21:38
  • you need to do something in your functions.?! @guest12 – corn3lius Nov 08 '16 at 21:40
  • When i execute ./program1.rb --backwards poem.txt, it should return output of turning all the lines in the poem.txt to backwards – moon Nov 08 '16 at 21:43
  • You need to fill in the `backwards` function it currently does nothing.? – corn3lius Nov 08 '16 at 21:45