Ruby/Rspec assert that first 4 characters match given text - ruby

I have a timestamp
timestamp = 1466754627
And I want to assert that the first 4 characters are 1466 within a test. If i was to use
timestamp.includes('1466')
This will pass, but 1466 could appear anywhere and not necessarily the first 4 characters. How could i ensure that 1466 are the first 4 characters ?
Thanks

There's a start_with matcher:
expect('1466754627').to start_with('1466')
You might have to convert timestamp to a string via to_s if it is indeed an integer:
timestamp = 1466754627
expect(timestamp.to_s).to start_with('1466')

Related

How to write HQL expression in Hadoop to verify format of alphanumeric field in specific format such as X9999

How do I write HQL to determine if results from a field have 1st character as alpha and the following four are numeric. (i.e. - the format of the field is 'F5555', so I need to verify all the results returned from the query for this field are following the correct format.
You can try this:
select REGEXP_EXTRACT( 'd55555' , '^[A-Za-z ]?[0-9]{5}$', 0);
Now, in order to understand, please read this and see the next comments:
^ means the beginning of the string(in this mode we mark the beginning);
[A-Za-z] - means any letter: upper or lower case;
? - means that we want only 1 occurrence of the previous character class;
[0-9] - any digit from 0 to 9;
{5} - means that the previous character class ([0-9]) must appear 5 times exactly (no more, no less);
$ - end of the string;
Hope that you understood.

Characters at the end do not match

I need to match all the alphabets and numbers in a string str.
This is my code.
str.match(/^(AB)(\d+)([A-Za-z][0-9])?/)
When str = AB57933A [sic], it matches only AB57933, and not the characters appended after the numbers.
If I try with str = AB57933AbC [sic], it matches only AB57933; it only matches up to the last number, and not the characters after that.
In the way you have written it:
/^(AB)(\d+)([A-Za-z][0-9])/
you impose that the last character is between 0 and 9, you can replace it depending on your needs by if you do not expect digits after the last letter
/^(AB)(\d+)([A-Za-z]+)/
or by
/^(AB)(\d+)([A-Za-z0-9]+)/
if AB57933AbC12 are also accepted as valid input.
Last but not least, if you do not use back references you can omit the parenthesis as you do not need capturing groups

Ruby rspec check string begins with certain values

I have a string that begins with a set start and then is filled to 252 characters by randomly generated letters. (237 random chars - so that the end length is 252 characters)
For the purpose of testing the string starts off with TESTDATAMENDNOW and the rest is random capital letters.
How can I test that the characters are all capital letters
and that the string begins with TESTDATAMENDNOW
I have tried to use regex expressions to define this but I'm not too sure how to get them to work properly, and what I have tried so far seems not to be working.
EDIT: Clarity
expect(string).to match(/\ATESTDATAMENDNOW[A-Z]{237}\z/)
237 because 252 minus the length of "TESTDATAMENDNOW" is 237.
Here's another way that does not use a regex:
str = "TESTDATAMENDNOW"
expect(string[0,str.size]).eq(str)
expect(string.delete("ABCDEFGHIJKLMNOPQRSTUVWXYZ").eq("")
expect(string.size).eq(252)

Find a specific character in a string

I want to find a specific character in a given string of number for example if my input is:
1 4 5 7 9 12
Then for 4 the answer should be 1. My code is as follows:
secarr = second.split(" ")
answer = secarr.index(number) #here number is a variable which gets the character
puts answer
The above method works if I write "4" instead of number or any other specific character but does not work if I write a variable. Is there a method in ruby to do the same?
This is probably your variable number is an Integer, and secarr is an Array of Strings. Try to cast the number to string:
answer = secarr.index(number.to_s)

How to convert a number in a string to an integer

I have an output like "35%" from one command, and I stripped "%". Still, it's stored as a string. Is there a function to convert the string to integer?
You can simply do "35%".to_i which produces 35
For your exact problem:
puts 'true' if 35 == "35".to_i
output is:
true
Let's say your string is "35%". Start reading your string character by character. First your pointer is at '3'. Subtract '0'(ASCII 0) from this and multiply the result by 10. Go to the next character, '5' in this case and again subtract '0' but multiply the result by 1. Now add the 2 results and what you get is integer type 35. So what you are basically doing is subtracting '0' from each character and multiplying it by 10^(its position), until you hit your terminator(% here).

Resources