Reputation:
Given this snippet:
require 'date'
raw_data = ["Mon\n 9:00 - 1:00pm, 2:00 - 6:00pm",
"Tue\n 10:00 - 1:00pm, 2:00 - 7:30pm",
"Wed\n 9:00 - 1:00pm, 2:00 - 6:00pm",
"Thu\n 9:00 - 1:00pm, 2:00 - 6:00pm",
"Fri\n 9:00 - 1:00pm, 2:00 - 6:00pm",
"Sat",
"Sun"]
raw_data.each do |element|
a = element.gsub(/\n/,',').gsub(/\s+/, '').split(',')
day = a[0].to_s.downcase!
"day: #{day}"
map = { "mon" => "monday","tue" => "tuesday", "wed" => "wednesday", "thu" => "thursday", "fri" => "friday", "sat" => "saturday", "sun" => "sanday" }
map.each {|k,v| day.gsub!(k,v) }
day_from = a[1]
day_from = day_from.to_s.split('-')[0]
day_to = a[2]
day_to = day_to.to_s.split('-')[1]
day_to = day_to.gsub(/pm/,'') unless day_to.nil?
# not working??
day_to = day_to.gsub(/\s+/,'') unless day_to.nil?
p day_to
end
How to get this output:
# desired
# "6:00"
# "7:30"
# "6:00"
# "6:00"
# "6:00"
# nil
# nil
Now this code returns:
#
# " 6:00"
# " 7:30"
# " 6:00"
# " 6:00"
# " 6:00"
# nil
# nil
google says to use:
# not working??
day_to = day_to.gsub(/\s+/,'') unless day_to.nil?
but this solution somehow does not work...
UPDATE
code with hex code
and new input values
require 'date'
require 'hex_string'
raw_data = ["\n Mon\n 8:30 - 6:00pm\n ",
"\n Tue\n 8:30 - 6:00pm\n ",
"\n Wed\n 8:30 - 6:00pm\n ",
"\n Thu\n 8:30 - 6:00pm\n ",
"\n Fri\n 8:30 - 6:00pm\n ",
"\n Sat\n 8:30 - 4:00pm\n ",
"\n Sun\n 10:00 - 3:00pm\n "
]
puts raw_data.size
raw_data.each do |element|
a = element.gsub(/\n/,',').gsub(/\s+/, '').split(',')
day = a[1].to_s.downcase!
#puts "day: #{day}"
map = { "mon" => "monday","tue" => "tuesday", "wed" => "wednesday", "thu" => "thursday", "fri" => "friday", "sat" => "saturday", "sun" => "sanday" }
map.each {|k,v| day.gsub!(k,v) }
day_from = a[2]
day_from = day_from.to_s.split('-')[0]
day_from.to_s.strip unless day_from.nil?
p "day regular: #{day_from}"
p "day from(hex): #{day_from.to_hex_string}"
p "-----------------------------------------"
day_to = a[2].to_s.split('-')[1]
day_to = day_to.gsub(/pm/,'') unless day_to.nil?
# not working
day_to = day_to.gsub(/\s+/,'') unless day_to.nil?
p "day regular: #{day_to}"
p "day_to(hex): #{day_to.to_hex_string}"
p "-----------------------------------------"
end
"day regular: 8:30"
"day from(hex): c2 a0 c2 a0 38 3a 33 30"
"-----------------------------------------"
"day regular: 6:00"
"day_to(hex): c2 a0 c2 a0 36 3a 30 30"
"-----------------------------------------"
"day regular: 8:30"
"day from(hex): c2 a0 c2 a0 38 3a 33 30"
"-----------------------------------------"
"day regular: 6:00"
"day_to(hex): c2 a0 c2 a0 36 3a 30 30"
"-----------------------------------------"
"day regular: 8:30"
"day from(hex): c2 a0 c2 a0 38 3a 33 30"
"-----------------------------------------"
"day regular: 6:00"
"day_to(hex): c2 a0 c2 a0 36 3a 30 30"
"-----------------------------------------"
"day regular: 8:30"
"day from(hex): c2 a0 c2 a0 38 3a 33 30"
"-----------------------------------------"
"day regular: 6:00"
"day_to(hex): c2 a0 c2 a0 36 3a 30 30"
"-----------------------------------------"
"day regular: 8:30"
"day from(hex): c2 a0 c2 a0 38 3a 33 30"
"-----------------------------------------"
"day regular: 6:00"
"day_to(hex): c2 a0 c2 a0 36 3a 30 30"
"-----------------------------------------"
"day regular: 8:30"
"day from(hex): c2 a0 c2 a0 38 3a 33 30"
"-----------------------------------------"
"day regular: 4:00"
"day_to(hex): c2 a0 c2 a0 34 3a 30 30"
"-----------------------------------------"
"day regular: 10:00"
"day from(hex): 31 30 3a 30 30"
"-----------------------------------------"
"day regular: 3:00"
"day_to(hex): c2 a0 c2 a0 33 3a 30 30"
"-----------------------------------------"
Upvotes: 1
Views: 8094
Reputation: 29369
I am not sure why it's not working for you. I ran your script and it worked for me.
But you can try strip
, which removes leading and trailing white spaces:
day_to.strip! unless day_to.nil?
Upvotes: 0
Reputation: 79803
It looks like you have some non-breaking space characters in your file (C2 A0
is the UTF-8 encoding for U+00A0, which is non breaking space).
In Ruby’s regular expressions \s
doesn’t match non-breaking spaces, which is why your code doesn’t appear to work (strip
also doesn’t remove them). You can use the \p{Space}
character property instead, or the POSIX bracket expression [[:space:]]
.
day_to = day_to.gsub(/\p{Space}/,'') unless day_to.nil?
or
day_to = day_to.gsub(/[[:space:]]/,'') unless day_to.nil?
Upvotes: 5