0

Given a file with Ruby 2.3.0p0:

#!/usr/bin/env ruby

# frozen_string_literal: true

# Exit cleanly from an early interrupt
Signal.trap("INT") { abort }

This is fine.

# frozen_string_literal: true

#!/usr/bin/env ruby

# Exit cleanly from an early interrupt
Signal.trap("INT") { abort }

will result in error:

syntax error near unexpected token `"INT"'
`Signal.trap("INT") { abort }'

Why?

Juanito Fatas
  • 9,419
  • 9
  • 46
  • 70

1 Answers1

4

A shebang has to appear on the file's initial line.

A file test.rb containing:

#!/usr/bin/env ruby    
# foo bar
puts "hello from #{RbConfig.ruby}"

will be run via Ruby:

$ ./test.rb
hello from /.../ruby-2.3.0/bin/ruby

But if test.rb contains: (1st and 2nd line swapped)

# foo bar
#!/usr/bin/env ruby
echo "hello from $SHELL"

it will be run as an ordinary shell script:

$ ./test.rb
hello from /.../bin/zsh

Therefore, the error you are getting is no Ruby error, it's from your shell.

Stefan
  • 109,145
  • 14
  • 143
  • 218