davetron5000 / optparse-plus

Start your command line scripts off right in Ruby
http://davetron5000.github.com/optparse-plus
Apache License 2.0
521 stars 54 forks source link

"no implicit conversion of false into String" when using sh #107

Closed mbigras closed 7 years ago

mbigras commented 7 years ago

I created a small example using Methadone's sh method following the example on the wiki. The repo at mbigras/sh-weirdness.

It seems like check if sh "touch foo" == 0 doesn't work.

I get the following error message no implicit conversion of false into String

If I assign it's value to variable then everything works as expected, as shown below:

t;dr:

the diff looks like:

-  if sh "touch foo" == 0
+  result = sh "touch foo"
+  if result == 0
     puts "touch successful"
     # do other things
   else

long version:

Before putting sh above if

following example on: https://github.com/davetron5000/methadone/wiki/Tutorial_LoggingAndDebugging

app.rb:

require 'methadone'

include Methadone::CLILogging
include Methadone::SH
include Methadone::Main

main do
  puts "Hello world!"

  if sh "touch foo" == 0
    puts "touch successful"
    # do other things
  else
    puts "Something, went wrong: #{$!}"
  end

end

go!

result:

➜  sh-weirdness master ✗ ls
Gemfile      Gemfile.lock README.md    app.rb
➜  sh-weirdness master ✗ ruby app.rb
Hello world!
no implicit conversion of false into String
➜  sh-weirdness master ✗ ls
Gemfile      Gemfile.lock README.md    app.rb

error msg: no implicit conversion of false into String

After putting sh above if

app.rb:

require 'methadone'

include Methadone::CLILogging
include Methadone::SH
include Methadone::Main

main do
  puts "Hello world!"

  result = sh "touch foo"
  if result == 0
    puts "touch successful"
    # do other things
  else
    puts "Something, went wrong: #{$!}"
  end

end

go!

result:

➜  sh-weirdness master ✗ ls
Gemfile      Gemfile.lock README.md    app.rb
➜  sh-weirdness master ✗ ruby app.rb
Hello world!
touch successful
➜  sh-weirdness master ✗ ls
Gemfile      Gemfile.lock README.md    app.rb       foo

no error.

mbigras commented 7 years ago

The fix was to add parenthesis when calling sh still not sure why. I also updated the wiki

require 'methadone'

include Methadone::CLILogging
include Methadone::SH
include Methadone::Main

main do
  puts "Hello world!"

  if sh("touch foo") == 0
    puts "touch successful"
    # do other things
  else
    puts "Something, went wrong: #{$!}"
  end
end
go!