how to format string output using % in ruby? - ruby

I want to format the output of a string in ruby. I can do it using % but I cannot figure it out.
Instead of 201107070928 I want to output 2011\07\070928
puts "%.4s\\%.2s\\%s" % "201107070928"
gives me an error:`%': too few arguments (ArgumentError)

That's not how you use it for formatting a date. The way to use % formatting is:
puts "%.4s\\%.2s\\%s" % ["1","2","3"]
So, you need multiple parameters - one for each of the format specifiers.
If you need to print & format a date from a string-date, first convert the string to a date/time object and then use strftime:
Presuming the input is date and a time:
Time.parse("201107070928").strftime("%Y\\%m\\%d%H%M").

I'm going to put this in an answer rather than a comment just because I'm more impressed with the possibility. Using String#unpack, we can handle a 4 digit / 2 digit / anything with:
"201107070928345345345".unpack("a4a2a*").join('/')
=> "2011/07/070928345345345"

Related

How to chop ruby string having a random number

My string is - main_string = 'FA117RWD-20 ABC Program (version 10.0)'
In this string I just want to read FA117RWD-20 and in this string version is random which will be coming from somewhere, its version 10 right now it can be version 20 at some other time also the part that I want to read i.e. FA117RWD-20 will also be coming from somewhere randomly so is not of fix length, basically I want to chop everything that is coming after this string. How should I do it?
Thanks
main_string = 'FA117RWD-20 ABC Program (version 10.0)'
main_string[/\S+/]
#=> "FA117RWD-20"
The regular expression matches one or more characters that are not whitespace.
Another way is the following1.
main_string[0..main_string.index(' ')-1]
1 One can use three dots and write main_string[0...main_string.index(' ')], but I prefer to always use two dots.
you can just use #split
'FA117RWD-20 ABC Program (version 10.0)'.split(' ').first

Need to custom format a ruby date object?

how do I format a ruby date as the following?
sep14.2016
It looks like the format is 3 characters of month followed by 2 characters of day with a period and a year.
Try This
Date.new(2016,9,14).strftime('%b%d.%Y')
d = Date.parse('14 September 2016')
to get your date object.
d.strftime('%b%d.%Y') #=> Sep14.2016
This will format your date like you wanted. Keep in mind d is not modified, so it is still a Date object. You need to assign the result of the strftime method to any variable.
You can use the downcase method to remove any capitalization.

Get the current date/time in Ruby as string without delimiters?

Is there any good way to get the current date/time in Ruby as a string without separators, not having to use Time.now.strftime('%Y%m%d'), etc.?
The output I'm looking for is something like "20151002112001" or similar, with all digits and no separators, human-readable form, not Unix time.
I found out that by using Active Support, this can be easily done:
require 'active_support/core_ext/time/conversions'
Time.now.to_formatted_s(:number) # => "20151002112419"
Since I already depend on Active Support, this turned out to be the quickest and easiest way to get this done by far.
You can use .to_i on a Time object to get a number representing Unix-time.
You can convert Date or DateTime objects to Time with .to_time. For example:
string = "#{Date.today.to_time.to_i}"
# "1443769200"
If you need to get certain attributes but don't like strftime, you can use methods of Date. For example, you might do something like
date = Date.today
string = "#{date.year}-#{date.month}-#{date.day}"
# "2015-10-2"
This all works without Active Support, but you still need to require 'date'.

Jmeter - how to add prefix to a counter. Similar to a random variable

Currently in the random variable I can put a prefix or suffix in the output format of the variable. However this nice feature is not available for a simple counter controller. Concatenating string+${counter} every time when i use the variable is not a good option form me since i do this a lot.
Is there any way to achieve prefix+counter in a way random variable do this?
Thanks.
Are you talking about Counter Config Element ?
If so it is possible using Number Format attribute:
http://jmeter.apache.org/usermanual/component_reference.html#Counter
See:
Format Optional format, e.g. 000 will format as 001, 002 etc.
This is passed to DecimalFormat, so any valid formats can be used.
If there is a problem interpreting the format, then it is ignored.
[The default format is generated using Long.toString()]
On 'Number format' field of Counter, write your pattern with 0...0 represents for value count up.
Example:
'Number format' pattern: prefix_000_suffix
Real value: prefix_001_suffix, prefix_002_suffix, prefix_003_suffix,...
Hope this helps!

Replacing manually written date with a string containing it

I have these 2 things I am working with:
CSV.foreach('datafile.csv','r') {|row| D_Location << row[0]}
puts Date.new(2003,05,02).cwday
In the first line I would like to change the datafile.csv to something like a string so I can change one string and it changes for all of these codes. I have many, each controlling 1 csv column.
In the second one I would like to replace the actual date written, and replace it with a string. This is so that can be automatic, because the string will be generated based on other criteria.
I trust the mods will ban me if I'm being too much of a noob hehe. Then I'll toughen up and find these answers myself eventually. But so far I've solved a lot, but not this. Thanks in advance!
Make a function which takes in a string representing a weekday, and returns a number. Call this function later in your code:
Date.new(2003, 05, yourfun('Tuesday')).cwday
For the first part of your question, you're already working with a string. I think what you mean is that you want it to be in a variable:
csv_file = 'datafile.csv'
CSV.foreach(csv_file,'r') {|row| D_Location << row[0]}
For the second part of your question, Date.parse() works with strings, but they need to be in a format that it can recognize. If your date strings use commas, you can replace them with hyphens:
date_str = "2003,05,02"
Date.parse(date_str.gsub(",", "-")).cwday # => 5
It's not clear where your date strings will be coming from or what format they'll be in, but the general concepts you need to understand are that you can use variables, and that you can transform strings.

Resources