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


Groups > comp.lang.ruby > #6987

Re: Convert MatchData to Integer

Newsgroups comp.lang.ruby
Date 2014-06-03 03:22 -0700
References <6d4b5bb3-7b43-4222-8924-504b04f1bd6c@googlegroups.com> <bv443qF687bU1@mid.individual.net>
Message-ID <b826794f-0b44-4dfc-bbb7-c929284f133d@googlegroups.com> (permalink)
Subject Re: Convert MatchData to Integer
From qoexcel@gmail.com

Show all headers | View raw


вторник, 3 июня 2014 г., 0:11:38 UTC+4 пользователь Robert Klemme написал:
> 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

Thank you very much, Robert!! Yes, now i have number (fixnum)
I have also problem, i try to add this value to array element :
convert_loadDOM = self.loadDOMstat
    n1 = convert_loadDOM[/Loading DOM finished: (\d+) ms/, 1]
    files_load_DOM[i]= files_load_DOM[i] + Integer(n1) 

    and i have error:
     undefined method `+' for nil:NilClass (NoMethodError)  

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