如何在 Ruby 中测量长字符串的长度? SciTE 和命令提示符不起作用。
我编写了一个程序来测量我的打字速度。作为其中的一部分,我需要它来计算我输入的字符数。我这样做了
text = gets.chomp
puts text.length.to_s
不幸的是,我无法让它在很长的字符串上工作。
在 SciTE 编辑器中,.length 无法正常工作,因此它不是给我字符串的长度,而是给我输入的所有内容的字符数,包括更正的错误 - 如果我输入错误“Hrello”并更正它到“Hello”,它仍然会返回 6 而不是 5。
我用 google 搜索了这个,建议的修复方法是从命令提示符运行该程序。在命令提示符中,.length 工作正常,但事实证明我无法输入超过 264 个字符。
所以我尝试用 Shoes: 在程序上放置一个 GUI,
Shoes.app :width => 300, :height => 300 do
button "Start." do
text = ask "Type here."
para text.length.to_s
end
end
结果发现 Shoes 的输入框有更短的字符限制。
我正在运行 Windows 7、Ruby 1.9.2、SciTe 版本 2.29 和 Shoes Policeman Revision 1514。
我怎样才能运行这个程序,以便它能够正确测量很长字符串的长度?我很乐意接受任何修复命令提示符或 Shoes 字符限制、SciTE bug 的解决方案,或者只是提供一种执行 ruby 程序的不同方法的建议。
I've written a program that measures my typing speed. As part of this, I need it to count how many characters I've typed. I did that with
text = gets.chomp
puts text.length.to_s
Unfortunately, I can't get this working for a long string.
In the SciTE editor, .length doesn't work properly, so instead of giving me the length of the string, it gives me the character count of everything I've typed, including corrected mistakes - if I typo "Hrello" and correct it to "Hello", it'll still return 6 instead of 5.
I googled this, and the suggested fix was to run the program from the command prompt instead. In the command prompt, .length works fine, but it turned out that I can't type in more than 264 characters.
So I tried to put a GUI on the program with Shoes:
Shoes.app :width => 300, :height => 300 do
button "Start." do
text = ask "Type here."
para text.length.to_s
end
end
and discovered that Shoes' input box has an even shorter character limit.
I'm running Windows 7, Ruby 1.9.2, SciTe version 2.29 and Shoes Policeman Revision 1514.
How can I run this program so it'll correctly measure the length of a really long string? I'd be happy with any solution that fixes the command prompt or Shoes character limit, the SciTE bug, or just a suggestion for a different way to execute ruby programs where this will work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个简单的 Web 应用程序怎么样?这是一个简单的 Sinatra 应用程序,它可以完全满足您的要求,并且具有很大的字符限制。
要运行该应用程序,您可以使用像
ruby sinatra_example.rb
这样简单的东西来使用内置的 Web 服务器。或者,您可以使用多个 Web 服务器中的任何一个来部署此应用程序。如果您需要计时器,这应该很容易通过 javascript 完成并包含在表单提交中。
What about a simple web app? Here is a simple Sinatra app that accomplishes exactly what you have asked with a very large character limit.
To run the app you can use something as simple as
ruby sinatra_example.rb
to use a built-in web server. Or, you can deploy this app using any of several web servers.If you need timers this should be easy to accomplish through javascript and include in the form submit.
好吧,你的问题的标题不准确,但让我们看看:
使用命令提示符有很多选项,你应该考虑在 ruby 中运行一个简单的脚本。
在 Windows 命令行上,尝试输入 ruby C:/path_to_folder_program/program.rb
如果它无法执行,您可以在 ruby 文件夹中找到一些名为 ruby 的可执行文件,并且应该从该路径上的命令提示符中像上面一样运行它。
但我问你,为什么是红宝石?其他更易于访问和用户友好的编程语言(例如 JavaScript)会表现得更好,并且更容易使您的程序易于访问。
Ok, your question is not accurately titled, but lets see:
There is a very broad number of options of using command prompt, and you should consider running a simple script in ruby on it.
On command line from windows, try typing ruby C:/path_to_folder_program/program.rb
If it won`t execute, you can find on ruby folder some executable called ruby and should, from command prompt on that path, run it like above.
But let me ask you, why ruby? Other more accessible and user-friendly programming languages, like javascript would behave better and would be easier to make your program accessible.
- 编辑-
看来鞋子可以处理更多字符,请使用
edit_box
而不是ask
:在鞋子中:
无论如何,在尝试鞋子之前,我用我知道的进行了练习,这里是:
在 javascript 中:
在 Ruby 中使用 Qt: (复制与 javascript 中相同的想法)
- EDIT -
Seems shoes can handle more chars, use
edit_box
instead ofask
:In Shoes:
Anyway, before trying shoes I did the exercise with that I knew, here it is:
In javascript:
In Ruby using Qt: (replicating the same idea as in the javascript one)