Sublime text can't understand gets.chomp

半腔热情 提交于 2019-12-01 11:44:45

问题


I wrote this simple program in ruby using Sublime Text and for some reason if I build it using Sublime text inbuilt system then I get the following error

`deposit': undefined method `chomp' for nil:NilClass (NoMethodError)

Its running perfectly if I run it using cmd.

class BankAccount
    def initialize(name)
        @transactions = []
        @balance = 0
    end
    def deposit
        print "How much do you want to deposit?"
        amount = gets.chomp
        @balance += amount.to_f
        puts "$#{amount} is deposited"
    end
    def show_balance
        puts "Your balance is #{@balance}"
    end
end
bank_account = BankAccount.new("Rohit Begani")
bank_account.class # => BankAccount
bank_account.deposit
bank_account.show_balance

回答1:


Sublime can't handle interactive input on its own. You can either run your script through SublimeREPL or create a customized build system to open a command prompt and then run the code. Fortunately, this isn't too difficult. Create a new file in Sublime with the following contents:

{
    "cmd": ["start", "cmd", "/k", "c:/ruby193/bin/ruby.exe", "$file"],
    "selector": "source.ruby",
    "shell": true,
    "working_dir": "$file_dir"
}

Save it as Packages/User/Ruby_cmd.sublime-build, where Packages is the directory opened when selecting Preferences -> Browse Packages.... Select Tools -> Build System -> Ruby_cmd, and run your file with CtrlB.

This should work on all versions of Windows from XP on up. It is not intended for OSX or Linux, though, as they don't have the start and cmd programs...



来源:https://stackoverflow.com/questions/19332183/sublime-text-cant-understand-gets-chomp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!