Found another question that seemed to be solved by using .sprintf('%02d', 1) to solve this, but I get: 'private method `sprintf' called for 7:Fixnum'.
Does this mean sprintf cant be called on an integer or am I getting it wrong in the calling?
Just looking to pad single digit returns with a leading 0.
Thanks in advance for any help on this!
You might want to try using sprintf like this:
sprintf('%02d', 1)
# => "01"
ie
sprintf('%02d', model.count)
Not calling .sprintf on the number but rather inputing it as a parameter into sprintf()
you may want to try "0#{num}"[-2..-1] or "%02d" % 1
Related
An example url that I'm trying to collect the values from has this pattern:
https://int.soccerway.com/matches/2021/08/18/canada/canadian-championship/hfx-wanderers/blainville/3576866/
The searched value always starts at the seventh / and ends at the ninth /:
/canada/canadian-championship/
The method I know how to do is using LEFT + FIND and RIGHT + FIND, but it is very archaic, I believe there is a better method for this need.
Another alternative:
="/"&textjoin("/", 1, query(split(A1, "/"), "Select Col7, Col8"))&"/"
The searched value always starts at the seventh / and ends at the ninth /:
Here's another way you can do it:
="/"®exextract(A1,"(?:.*?/){7}(.*?/.*?/)")
You can use =REGEXTRACT() to match part of the string with a regular expression:
For example, If A1 = https://int.soccerway.com/matches/2021/08/18/canada/canadian-championship/hfx-wanderers/blainville/3576866/ ,
then
=REGEXEXTRACT(A1, "\/[^\/]*\/[^\/]*\/[^\/]*\/[^\/]*\/[^\/]*\/[^\/]*(\/[^\/]*\/[^\/]*\/)")
returns
/canada/canadian-championship/
Explanation: \/ is '/' escaped. [^\/]* matches any non '/' character 0 or more times. \/[^\/]* is repeated 6 times. () captures a specific part of the string as a group to be returned. Finally (\/[^\/]*\/[^\/]*\/) matches the essential part we want.
Little bit different approach.
=REGEXEXTRACT(SUBSTITUTE(SUBSTITUTE(A1,"/","|",9),"/","|",7),"\|(.*?)\|")
I need a print function that writes Loop i / round, where both values i and round have to have at least four digits (12 -> 0012). It must have the format-method too.
I found the paste formatC and other ways to add leading zeroes but I can't make them work for this case..
It needs to be like this:
print('Loop {} / {}'.format('i', 'round'))
But with four digit numbers as I said.
number_str = str(a_number)
zero_filled_number = number_str.zfill(4)
a_number is 12.
zero_filled_number will give you 0012
Use print("{:02d}".format(1)) as found in this answer.
In a rails 3.2 , ruby 1.9.3 app
Trying to perform a simple -1 action on an integer:
doing this on the model:
order_details[:quantity].to_i - 1
and getting the ArgumentError: invalid radix -1
Tried to look this up online , and found very little documentation.
Any help, pls?
I'm assuming order_details[:quantity] is a String (though the answer is the same regardless).
String#to_i takes an optional argument for the base the number is to be interpreted as. For instance "10101".to_i(2) will parse as base 2, (giving the decimal 21 as a result). Your line of code is being interpreted as
order_details[:quantity].to_i(-1)
and since a base of negative one (-1) makes no sense, it's giving you that error. The solution is to put parentheses around order_details[:quantity].to_i so that it's evaluated first:
(order_details[:quantity].to_i) - 1
Edit:
Or, make sure there's a space separating - from the two arguments (or no spaces on either side) and Ruby should parse it correctly. It might be that your actual code is written as order_details[:quantity].to_i -1 (note no space between - and 1) causing it to read -1 and then pass it as an argument to to_i.
I think your problem is that your code really looks like this:
order_details[:quantity].to_i -1 # with the minus sign right next to the one
Ruby is parsing this as:
order_details[:quantity].to_i(-1)
Method parameters do not (always) need to be wrapped in parenthesis in Ruby, and to_i takes a parameter that specifies the base in which you are counting.
So you could convert base 16 into our normal base ten like:
"0xA".to_i(16)
iamnotmaynard correctly identified it as a syntax error, but I think it's that you need to separate the - and 1. You could put parenthesis around the first element (it works), but that's more short-circuiting improper syntax instead of supplying proper syntax.
Try separating the elements out without parenthesis:
order_details[:quantity].to_i - 1 # with the space between the 1 and minus sign
hi i am using latex and texmaker to do the following:
$\mathcal{a( X, Y )= a_i \circ a_j}$
which i expect to get
a(X,Y)= a subscript {i} circle a subscript {j}
but instead i get weird signs instead for a's (on the right side of equation), i and j ...can you tell me why? thanks...
The problem is that \mathcal only works on upper case letters.
never mind i did
$a(X,Y)=a_i \circ a_j$
it solved the problem.
thanks anyways!
Ok. Given the example of:
http://example.com/news/3226-some-post-title.html
I'm trying to get the 3226. This regexp: http://interaktywnie.com/newsy/(.*).html
doesn't seem to work. Please help.
Thanks.
You can just use:
/\/(\d+)-(.*)\.html$/
This will grab the digits (\d) after the '/' and put the digits into the first variable once it finds them.
A great place to test regular expression is http://rubular.com/.
You want this:
/http:\/\/example.com\/news\/(\d+)-.+\.html/
\d is any digit. Also, the following site is very useful for regular expressions in ruby:
http://www.rubular.com
"http://example.com/news/3226-some-post-title.html".split("/").last.to_i
Try this pattern:
/http:\/\/example\.com\/news\/(\d+)-.+\.html/
So:
match = /http:\/\/example\.com\/news\/(\d+)-.+\.html/.match("http://example.com/news/3226-some-post-title.html")
puts match[1]