崇高的文字无法理解gets.chomp [英] Sublime text can't understand gets.chomp

查看:97
本文介绍了崇高的文字无法理解gets.chomp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Sublime Text用ruby编写了这个简单的程序,由于某种原因,如果我使用Sublime text内置系统来构建它,则会收到以下错误消息

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)

如果我使用cmd运行它,它将运行完美.

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

推荐答案

Sublime无法自行处理交互式输入.您可以通过 SublimeREPL 运行脚本,也可以创建自定义的

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"
}

将其另存为Packages/User/Ruby_cmd.sublime-build,其中Packages是选择Preferences -> Browse Packages...时打开的目录.选择Tools -> Build System -> Ruby_cmd,然后使用 Ctrl B 运行文件.

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.

这应该适用于XP以上版本的所有Windows版本.但是,它不适用于OSX或Linux,因为它们没有startcmd程序...

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...

这篇关于崇高的文字无法理解gets.chomp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆