Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #6986

Re: Convert MatchData to Integer

From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.ruby
Subject Re: Convert MatchData to Integer
Date 2014-06-02 22:11 +0200
Message-ID <bv443qF687bU1@mid.individual.net> (permalink)
References <6d4b5bb3-7b43-4222-8924-504b04f1bd6c@googlegroups.com>

Show all headers | View raw


On 06/02/2014 10:07 PM, qoexcel@gmail.com wrote:
> Hi Everyone,i want to get integer from string, something like this:
>
>             <Loading DOM finished: 41 ms>
>
> using command            /\d+/.match(self.loadDOMstat)
>
> , but actually i don't have integer, i have MatchData .
> How can i convert MatchData to integer in Ruby?Moreover you can give me a hint, how to get number  from this string..

Generally you use method Integer() to convert String into an int:

irb(main):001:0> s = "123"
=> "123"
irb(main):002:0> n = Integer(s)
=> 123
irb(main):003:0> n.class
=> Fixnum

In your case you can do this:

irb(main):004:0> s = '<Loading DOM finished: 41 ms>'
=> "<Loading DOM finished: 41 ms>"
irb(main):005:0> n = s[/<Loading DOM finished: (\d+) ms/, 1]
=> "41"
irb(main):006:0> n = Integer(n)
=> 41

Cheers

	robert

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Convert MatchData to Integer qoexcel@gmail.com - 2014-06-02 13:07 -0700
  Re: Convert MatchData to Integer Robert Klemme <shortcutter@googlemail.com> - 2014-06-02 22:11 +0200
    Re: Convert MatchData to Integer qoexcel@gmail.com - 2014-06-03 03:22 -0700
      Re: Convert MatchData to Integer Michael Uplawski <michael.uplawski@uplawski.eu> - 2014-06-03 19:51 +0200
      Re: Convert MatchData to Integer Robert Klemme <shortcutter@googlemail.com> - 2014-06-03 22:53 +0200

csiph-web