How can I execute strptime within a string using Ruby? [closed] - ruby

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have the following code:
line_item_interpolated = String.interpolate {line_item}
output_line = output_line + line_item_interpolated
It works fine for a case where: line_item = #{index}: #{item["value"]["time_string"]} for example.
But, if item['value']['time_string'] = '1453494900'for example, but I wanted that epoch time to be displayed as a formatted date using strptime, how would I do so by only setting the value for the string line_item
Where puts line_item_interpolated would print out a date, instead of the epoch-time above.

line_item = #{index}: #{Time.at(item["value"]["time_string"]})
You can use Time#at:
Time.at(1453494900)
#=> 2016-01-22 21:35:00 +0100

Related

How Do I Assign Values of 2 or more Fields in VBScript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Hi sorry for a basic question on VBScript but after scouring the internet. In a If statement I am trying to assign values to 2 fields but when I run this code which is part of an application it does not error neither does it work so I suspect I am doing wrong. Can anyone point me in the right direction as I am trying to assign values to CustomerInformation.CodeObject.Customer and. CustomerInformation.CodeObject.Carrier
Function CustomerInformation_OnLoad()
if (SystemVariables.CodeObject.Company = "D" or SystemVariables.CodeObject.Company = "T") and DispatchNoteDetails.CodeObject.Area = "UK" then
if CustomerInformation.CodeObject.Customer = "AAE02" then
CustomerInformation.CodeObject.Carrier = "Customer collects" and CustomerInformation.CodeObject.CarrierURN = "AA Driver"
end if
end if
End Function
Hi thanks all for the tips.
The code worked when changing to the following:
Function CustomerInformation_OnLoad()
if (SystemVariables.CodeObject.Company = "D" or SystemVariables.CodeObject.Company = "T")then
if CustomerInformation.CodeObject.Customer = "AAE02" then
CustomerInformation.CodeObject.Carrier = "Customer collects"
CustomerInformation.CodeObject.CarrierURN = "AA Driver"
end if
end if
End Function
Thanks again for the tips.

Random numbers in Ruby [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to make n random numbers from -0.5 from to 0.5
and I made a function like this
def create_noise(n)
end
I found an implementation of this but i don't think this works
randoms = Set.new
loop
randoms << rand(max)
return randoms.to_a if randoms.size >= n
You would just do
def create_noise(n)
n.times.collect { rand(-0.5..0.5) }
end
that will spit back an array like this:
[-0.034680737617880486, 0.34802029078157803, 0.1346483808607455, 0.12155616615186282, -0.41043213731234474]

Ruby: how to make an array of words, with the length of the array being random [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I use the excellent faker gem to generate random words for my models. Eg. product.name = Faker::Lorem.word
Sometimes I need to generate a sentence, and I want the length of the sentence to
vary each time.
How to achieve this with ruby?
How about:
result = rand(max_size).times.map { produce_word }
Since you have not provided enough information, this is my approach, [*1..100].sample will return a random number between 1 and 100, so looping that times the string which is returned bya method named get_word will get stored in the array word_array
word_array = []
[*1..100].sample.times do
word_array << get_word
end

Matching partial strings in a phrase [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I put up an intranet site that loops through a .csv dump of our customer database and uses a form to help look up account numbers.
I want to treat all of my keywords as wild card terms, but respect their order. For example, if I have company A: "The Monogramme Shoppe" and company B: "Monograms & More at The Shop", I want to return A and B options if I type "mono shop" in the form field. This code does that:
company_lookup = company_lookup.split(" ")
counter = company_lookup.length
company_lookup.each do |com|
if company.downcase.include? com.downcase
counter = counter - 1
end
end
if counter == 0
match_found = true
account_number = row[2].to_s
matches.push [account_number, company]
end
But if I type "mono the", I also get both results. There, I only want to get the B result.
Is there any way to use regular expressions to, say look for PartialKeyword1 and PartialKeyword2 in a string and return true if matched?
You can use the following code to construct a regular expression to match the company name, and then use this regular expression to find the matched record.
company_lookup = company_lookup.split(" ").map{|r| Regexp.quote(r)}.join('.*?')
if company =~ /#{company_lookup}/i
matches.push [row[2].to_s, company]
end
If performance is a big concern, or the data size is huge, you'd better try some full text search engine, such as Thinking Sphinx

Converting Fixnum to Array in ruby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to convert a fixnum to an array in ruby?
For Example:
s = SituationType.all(:conditions => {:name => 'did not match retrieved design - text misspelled'}).collect(&:id)
result: [10034, 10055]
sf = situation_type_id = SituationType.find_by_name('did not match retrieved design - text misspelled').id
result: 10034
s -sf says,
TypeError: can't convert Fixnum into Array
Do the following
s - [sf]
Since sf is an integer, It cannot do the subtraction unless you convert it into an array

Resources