Related
I am trying to use the case statement:
week # => "03 – 09 MAR 2019"
first_day = week.slice(0..2) # => 03
last_day = week.slice(5..7) # => 09
month = week.slice(8..11) # => MAR
year = week.slice(12..17) # => 2019
puts month # >> MAR
case month
when 'JAN' then month_num = '01'
when 'FEB' then month_num = '02'
when 'MAR' then month_num = '03'
when 'APR' then month_num = '04'
when 'MAY' then month_num = '05'
when 'JUN' then month_num = '06'
when 'JUL' then month_num = '07'
when 'AGO' then month_num = '08'
when 'SEP' then month_num = '09'
when 'OCT' then month_num = '10'
when 'NOV' then month_num = '11'
when 'DEC' then month_num = '12'
else month_num = 'ERROR'
end
puts month_num # >> ERROR
However, the case statement always goes to the else branch.
Why is the var month_num equal to the string "ERROR" instead of "03"?
You are using puts to examine what you have, and therefore you are missing to observe whitespaces in your results. You actually have:
week.slice(0..2) # => "03 "
week.slice(5..7) # => "09 "
week.slice(8..11) # => "MAR "
week.slice(12..17) # => "2019"
To observe what you have, it is better to use p rather than puts.
You have the wrong range. Actually, there is no reason to use ranges here. It is much easier to use the second argument to specify the length:
week.slice(0, 2) # => "03"
week.slice(5, 2) # => "09"
week.slice(8, 3) # => "MAR"
week.slice(12, 4) # => "2019"
Your month is "MAR "
Try
month = week.slice(8..10)
And makes sense, from 8 to 10 inclusive are three characters. Same for the other parts.
Seems like you want to parse a string containing data in a specific format. Instead of relying on absolute indices, you could use a regular expression to match the date format, e.g:
PATTERN = /
(?<first_day>\d{2}) # 2-digit first day
\s* # optional whitespace
[–-] # delimiter(s)
\s*
(?<last_day>\d{2}) # 2-digit last day
\s*
(?<month>\w{3}) # 3-letter month name
\s*
(?<year>\d{4}) # 4-digit year
/ix
To extract the data:
str = '03 – 09 MAR 2019'
m = str.match(PATTERN)
#=> #<MatchData "03 – 09 MAR 2019" first_day:"03" last_day:"09" month:"MAR" year:"2019">
m[:first_day] #=> "03"
m[:last_day] #=> "09"
m[:month] #=> "MAR"
m[:year] #=> "2019"
The results could further be fed into Date.strptime:
require 'date'
from = m.values_at(:first_day, :month, :year).join(' ') #=> "03 MAR 2019"
to = m.values_at(:first_day, :month, :year).join(' ') #=> "09 MAR 2019"
Date.strptime(from, '%d %b %Y') #=> #<Date: 2019-03-03 ...>
Date.strptime(to, '%d %b %Y') #=> #<Date: 2019-03-09 ...>
Or _strptime if you're just interested in the raw values:
Date._strptime(from, '%d %b %Y')
#=> {:mday=>3, :mon=>3, :year=>2019}
How to convert date in to string words in ruby
Input date
2-12-2002
Output
Two December two thousand two
require 'humanize'
day, month, year = '2-12-2002'.split('-')
month_numeric_range = (1..12).to_a.map(&:to_s)
month_words = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
month_numeric_to_words = Hash[month_numeric_range.zip(month_words)]
day.to_i.humanize.capitalize + ' ' + months_numeric_to_words[month] + ' ' + year.to_i.humanize.capitalize
# => "Two December Two thousand and two"
This is a partial option using Class: Date:
require 'date'
date_string = "2-12-2002"
date = Date.strptime(date_string, "%d-%m-%Y")
p date.day # => 2
p date.month # => 12
p date.year # => 2002
p date.strftime("%d %B %Y") # => "02 December 2002"
If you want also to spell numbers, check this out:
Using Ruby convert numbers to words?
Number to English Word Conversion Rails
I am missing something obvious. I have a string that is tab separated in a text file. I read it into an argument
Here is what it looks like in the text file:
hello world foo bar
So each of those words has a tab between them in the text file.
I read it into a variable
line = ""
File.open("some_file", "r+") do |file|
line = file.gets
end
Now I simply want to split up the words by the tab separation:
word1, word2, word3, word4 = line.split("\t")
However what is happening is that it is putting ALL the words in the first variable, leaving the other variables with nil
p word1
=> "hello world foo bar"
p word2
=> nil
p word3
=> nil
p word4
=> nil
What am I missing? A word should be within each of those variables.
This is because your string does not contain "\t" in it (but rather spaces):
words = 'hello world foo bar'
words.split(' ')
#=> ["hello", "world", "foo", "bar"]
If it really would contain tabs:
"hello\tworld"
you then would indeed be able to split it as intended:
"hello\tworld".split("\t")
#=> ["hello", "world"]
I'm trying to do the above. For example:
"This is a sentence I'm currently writing, potentially with punctuation dotted in: item1, item2, item3. That is all."
should split into individual words:
This
is
a
sentence
I'm
and so on.
I'm just struggling to write the regexp. I know this would be pretty easy using a delimiter or two, but trying to learn more about regexp.
Just split your input according to one or more space characters.
> "This is a sentence I'm currently writing, potentially with punctuation dotted in: item1, item2, item3. That is all.".split(/\s+/)
=> ["This", "is", "a", "sentence", "I'm", "currently", "writing,", "potentially", "with", "punctuation", "dotted", "in:", "item1,", "item2,", "item3.", "That", "is", "all."]
> "This is a sentence I'm currently writing, potentially with punctuation dotted in: item1, item2, item3. That is all.".split()
=> ["This", "is", "a", "sentence", "I'm", "currently", "writing,", "potentially", "with", "punctuation", "dotted", "in:", "item1,", "item2,", "item3.", "That", "is", "all."]
OR
Match one or more non-space characters.
> "This is a sentence I'm currently writing, potentially with punctuation dotted in: item1, item2, item3. That is all.".scan(/\S+/)
=> ["This", "is", "a", "sentence", "I'm", "currently", "writing,", "potentially", "with", "punctuation", "dotted", "in:", "item1,", "item2,", "item3.", "That", "is", "all."]
use split
2.0.0-p481 :001 > a="This is a sentence I'm currently writing, potentially with punctuation dotted in: item1, item2, item3. That is all."
=> "This is a sentence I'm currently writing, potentially with punctuation dotted in: item1, item2, item3. That is all."
2.0.0-p481 :002 > a.split
=> ["This", "is", "a", "sentence", "I'm", "currently", "writing,", "potentially", "with", "punctuation", "dotted", "in:", "item1,", "item2,", "item3.", "That", "is", "all."]
2.0.0-p481 :003 >
OR USE LOOP TO FRAME A WORD ON EACH LINE
2.0.0-p481 :036 > a="This is a sentence I'm currently writing, potentially with punctuation dotted in: item1, item2, item3. That is all."
=> "This is a sentence I'm currently writing, potentially with punctuation dotted in: item1, item2, item3. That is all."
2.0.0-p481 :037 > a.split.each{ |i| puts "#{i}"}
This
is
a
sentence
I'm
currently
writing,
potentially
with
punctuation
dotted
in:
item1,
item2,
item3.
That
is
all.
=> ["This", "is", "a", "sentence", "I'm", "currently", "writing,", "potentially", "with", "punctuation", "dotted", "in:", "item1,", "item2,", "item3.", "That", "is", "all."]
2.0.0-p481 :038 >
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I want to go through an array in an array with arrays in those.
The array should contain 5 arrays, and in each of those arrays are five more arrays, the generation of the numbers is fine, but when I go through each of the arrays to map them with something else, I only get back an array with 5 arrays in it.
def random_map
#row = []
#col = []
#map = []
5.times do |row|
5.times do |col|
#c = rand(3)
#d = [#c]
#col << #d
end
#row << #col
#col = []
end
#map << #row
#map.map! do |row|
row.map! do |col|
col.map! do |element|
case(element[0])
when 0
element[0] = "BG"
when 1
element[0] = "B1"
when 2
element[0] = "TR"
end
end
end
end
end
Anyone know what's up with the mapping?
I'm not positive, but I think this is what you're trying to do:
def random_map
Array.new(5) do
Array.new(5) do
Array.new(5) do
%w{BG B1 TR}.sample
end
end
end
end
Running it generates an Array of 5 Arrays of 5 Arrays of 5 Strings, chosen at random form the three strings "BG", "B1", and "TR":
random_map
# => [[["B1", "BG", "BG", "B1", "B1"],
["B1", "B1", "BG", "TR", "B1"],
["B1", "B1", "TR", "TR", "TR"],
["B1", "TR", "TR", "BG", "B1"],
["BG", "TR", "BG", "TR", "TR"]],
[["B1", "TR", "TR", "BG", "TR"],
["BG", "B1", "TR", "BG", "BG"],
["B1", "B1", "BG", "TR", "BG"],
["BG", "BG", "TR", "B1", "B1"],
["B1", "BG", "BG", "BG", "BG"]],
[["B1", "BG", "BG", "B1", "TR"],
["BG", "BG", "B1", "B1", "TR"],
["B1", "BG", "B1", "TR", "TR"],
["BG", "TR", "B1", "B1", "BG"],
["TR", "TR", "BG", "TR", "B1"]],
[["BG", "B1", "BG", "BG", "BG"],
["B1", "B1", "TR", "TR", "B1"],
["BG", "B1", "TR", "B1", "TR"],
["TR", "TR", "TR", "BG", "B1"],
["TR", "B1", "BG", "TR", "BG"]],
[["B1", "B1", "BG", "BG", "TR"],
["TR", "TR", "B1", "BG", "BG"],
["TR", "BG", "B1", "BG", "TR"],
["BG", "B1", "TR", "BG", "TR"],
["B1", "TR", "BG", "B1", "B1"]]]
I'm specifically not using the instance variables #row, #col, #map, #c, or #d because it's not clear that you actually want to be setting them; it looks like you're using them as simply local variables.