如何在 Ruby 中测量长字符串的长度? SciTE 和命令提示符不起作用。

发布于 2024-12-07 19:38:06 字数 792 浏览 0 评论 0原文

我编写了一个程序来测量我的打字速度。作为其中的一部分,我需要它来计算我输入的字符数。我这样做了

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌逼全场 2024-12-14 19:38:06

如果有人建议我以不同的方式执行 ruby​​ 程序,我会很高兴。

一个简单的 Web 应用程序怎么样?这是一个简单的 Sinatra 应用程序,它可以完全满足您的要求,并且具有很大的字符限制。

require 'sinatra'

get '/' do
  %{<html>
      <body>
        <form method="post">
          <textarea name="typed"></textarea>
          <input type="submit">
        </form>
      </body>
    </html>
  }
end

post '/' do
  "You typed #{params['typed'].length} characters."
end

要运行该应用程序,您可以使用像 ruby sinatra_example.rb 这样简单的东西来使用内置的 Web 服务器。或者,您可以使用多个 Web 服务器中的任何一个来部署此应用程序。

如果您需要计时器,这应该很容易通过 javascript 完成并包含在表单提交中。

I'd be happy with [...] a suggestion for a different way to execute ruby programs where this will work.

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.

require 'sinatra'

get '/' do
  %{<html>
      <body>
        <form method="post">
          <textarea name="typed"></textarea>
          <input type="submit">
        </form>
      </body>
    </html>
  }
end

post '/' do
  "You typed #{params['typed'].length} characters."
end

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.

谁的年少不轻狂 2024-12-14 19:38:06

好吧,你的问题的标题不准确,但让我们看看:

使用命令提示符有很多选项,你应该考虑在 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.

面犯桃花 2024-12-14 19:38:06

- 编辑-
看来鞋子可以处理更多字符,请使用 edit_box 而不是 ask

在鞋子中:

Shoes.app do
    @txt = edit_box
    button("How many"){ alert(@txt.text.size) }
end

无论如何,在尝试鞋子之前,我用我知道的进行了练习,这里是:

在 javascript 中:

<script>
function start_stop(){
    var txt = document.getElementById('txt');
    var btn = document.getElementById('btn');
    if( txt.disabled ){
        txt.value = '';
        txt.disabled = false;
        btn.value = 'Stop';
        txt.focus();
        startTime = new Date().getSeconds();
    } else {
        txt.disabled = true;
        btn.value = 'Start again';
        timeNow = new Date().getSeconds();
        alert(txt.value.length + " characters in " + (timeNow - startTime) + " seconds.");
    }
}
</script>
<input type='button' id='btn' onclick='start_stop()' value='Start'>
<textarea id='txt' rows='8' cols='80' disabled></textarea>

在 Ruby 中使用 Qt: (复制与 javascript 中相同的想法)

require 'Qt'

class MyWidget < Qt::Widget
    slots :start_stop
    def initialize
        super
        setFixedSize(400, 120)

        @btn = Qt::PushButton.new("Start")
        @txt = Qt::TextEdit.new ; @txt.readOnly = true

        vbox =  Qt::VBoxLayout.new
        vbox.addWidget @btn
        vbox.addWidget @txt
        setLayout vbox

        connect(@btn, SIGNAL("clicked()"), self, SLOT(:start_stop))
    end
    def start_stop
        if @txt.readOnly
            @txt.plainText = ''
            @txt.readOnly = false
            @btn.text = "Stop"
            @txt.setFocus
            @startTime = Time.now
        else
            @txt.readOnly = true
            @btn.text = "Start again (#{@txt.plainText.size} chars #{(Time.now - @startTime).to_i} in seconds)"
        end
    end
end

app = Qt::Application.new(ARGV)
widget = MyWidget.new
widget.show
app.exec

- EDIT -
Seems shoes can handle more chars, use edit_box instead of ask:

In Shoes:

Shoes.app do
    @txt = edit_box
    button("How many"){ alert(@txt.text.size) }
end

Anyway, before trying shoes I did the exercise with that I knew, here it is:

In javascript:

<script>
function start_stop(){
    var txt = document.getElementById('txt');
    var btn = document.getElementById('btn');
    if( txt.disabled ){
        txt.value = '';
        txt.disabled = false;
        btn.value = 'Stop';
        txt.focus();
        startTime = new Date().getSeconds();
    } else {
        txt.disabled = true;
        btn.value = 'Start again';
        timeNow = new Date().getSeconds();
        alert(txt.value.length + " characters in " + (timeNow - startTime) + " seconds.");
    }
}
</script>
<input type='button' id='btn' onclick='start_stop()' value='Start'>
<textarea id='txt' rows='8' cols='80' disabled></textarea>

In Ruby using Qt: (replicating the same idea as in the javascript one)

require 'Qt'

class MyWidget < Qt::Widget
    slots :start_stop
    def initialize
        super
        setFixedSize(400, 120)

        @btn = Qt::PushButton.new("Start")
        @txt = Qt::TextEdit.new ; @txt.readOnly = true

        vbox =  Qt::VBoxLayout.new
        vbox.addWidget @btn
        vbox.addWidget @txt
        setLayout vbox

        connect(@btn, SIGNAL("clicked()"), self, SLOT(:start_stop))
    end
    def start_stop
        if @txt.readOnly
            @txt.plainText = ''
            @txt.readOnly = false
            @btn.text = "Stop"
            @txt.setFocus
            @startTime = Time.now
        else
            @txt.readOnly = true
            @btn.text = "Start again (#{@txt.plainText.size} chars #{(Time.now - @startTime).to_i} in seconds)"
        end
    end
end

app = Qt::Application.new(ARGV)
widget = MyWidget.new
widget.show
app.exec
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文