Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > question about regexp

Reply
Thread Tools

question about regexp

 
 
gerberdata
Guest
Posts: n/a
 
      01-26-2012
Can someone explain what is going on in this code
REGEXP = {
:time_in_pain => /\A(\d*\.?\d*)\s*(.+)\Z/,
:number_only => /\A(\d*\.?\d*)\Z/,
:days => /\Ad(ay(s)?)?\Z/i,
:weeks => /\Aw(eek(s)?)?\Z/i,
:months => /\Am(onth(s)?)?\Z/i,
:years => /\Ay(ear(s)?)?\Z/i
}

def time_in_pain=(time_in_pain)
@time_in_pain = time_in_pain.strip
if match = REGEXP[:time_in_pain].match(@time_in_pain)
number, units = match.captures
units.downcase!

units = case units
when REGEXP[:days] then "days"
when REGEXP[:weeks] then "weeks"
when REGEXP[:months] then "months"
when REGEXP[:years] then "years"
else
nil
end

# Use highest possible precision - Float if possible, otherwise
Integer
unless units.nil?
if ["days", "weeks"].include?(units)
self.pain_length_in_days = (number.to_f.send(units) /
1.day).round
else
self.pain_length_in_days = (number.to_i.send(units) / 1.day)
end
end
elsif REGEXP[:number_only].match(@time_in_pain)
self.pain_length_in_days = @time_in_pain.to_i
end
end
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      01-27-2012
On 26.01.2012 20:17, gerberdata wrote:
> Can someone explain what is going on in this code
> REGEXP = {
> :time_in_pain => /\A(\d*\.?\d*)\s*(.+)\Z/,
> :number_only => /\A(\d*\.?\d*)\Z/,
> :days => /\Ad(ay(s)?)?\Z/i,
> :weeks => /\Aw(eek(s)?)?\Z/i,
> :months => /\Am(onth(s)?)?\Z/i,
> :years => /\Ay(ear(s)?)?\Z/i
> }
>
> def time_in_pain=(time_in_pain)
> @time_in_pain = time_in_pain.strip
> if match = REGEXP[:time_in_pain].match(@time_in_pain)
> number, units = match.captures
> units.downcase!
>
> units = case units
> when REGEXP[:days] then "days"
> when REGEXP[:weeks] then "weeks"
> when REGEXP[:months] then "months"
> when REGEXP[:years] then "years"
> else
> nil
> end
>
> # Use highest possible precision - Float if possible, otherwise
> Integer
> unless units.nil?
> if ["days", "weeks"].include?(units)
> self.pain_length_in_days = (number.to_f.send(units) /
> 1.day).round
> else
> self.pain_length_in_days = (number.to_i.send(units) / 1.day)
> end
> end
> elsif REGEXP[:number_only].match(@time_in_pain)
> self.pain_length_in_days = @time_in_pain.to_i
> end
> end


Looks like someone is superfluously storing Regexp instances in a Hash.
Or is the Hash changed in some other place?

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
new RegExp().test() or just RegExp().test() Matěj Cepl Javascript 3 11-24-2009 02:41 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Programmatically turning a Regexp into an anchored Regexp Greg Hurrell Ruby 4 02-14-2007 06:56 PM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:59 PM



Advertisments