Search code examples
rubynomethoderror

NoMethodError for gets.chomp in nil:NilClass


I am currently learning Ruby and I wrote a very simple program to practice, and tried to run it using TextWrangler's #! function.

    print "Number: "
    number = Integer(gets.chomp)

    puts ""

    print "Percentage: "
    percentage = Integer(gets.chomp)

    number_onepercent = number / 100

    number_percentage = percentage * number_onepercent

    puts "#{percentage}% of #{number} is #{number_percentage}"

I tried to check if there were any syntax errors, but there apparently where none. However, when I tried running it the following chunk of text was returned:

    Number: 
    untitled text:2:in `<main>': undefined method `chomp' for nil:NilClass (NoMethodError)

Would using .to_i instead of Integer() help?


Solution

  • Your code works like the way it is with Ruby 2.2.0 and most likely TextWrangler can't handle interactive input on its own. Try doing this in your terminal or find some TextWrangler package that could help you.