开发者

How can I measure the length of a long string in Ruby? SciTE and command prompt aren't working.

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.

开发者_Go百科

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.


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜