(string).end_with? not giving me desire result

Hello All,

I written small function which will check whether string is ending with
number or not.

Below is my code but I’m not getting the desire result.

def check_dot_num(x)
#puts “Dot name is : #{x}”
if “#{x}”.include? “.”
puts “Dot present in directory name: #{x}”
puts “***”
puts “#{x}”.end_with?([/\d/])
if “#{x}”.end_with?([/\d/])
puts “End with Number : #{x}”
else
puts " directory not ending with number : #{x} \n"
end
end
end

I’m passing different values to #{x} are:
cer.000
New Folder
cer.00r
cer.001

But in any case it return value as false (i.e. not able to detect number
value at the end of string.)

Could anyone please help me detecting the problem with my code.

Thanks,
Raj

end_with does not work with regex

use
m=/d+$/.match("#{x}")

if no match m is nil
if match m is a matchData object, and m[1] returns the num

raju sathliya wrote in post #1010410:

if str[-1].match(/\d/)

7stud the problem is that does not work on ruby <1.9

oh you are right, it must be

m=/\d+$/.match("#{x}")

Note that yours doesn’t work on any ruby version. :frowning:

Hans M. wrote in post #1010415:

end_with does not work with regex

use
m=/d+$/.match("#{x}")

if no match m is nil
if match m is a matchData object, and m[1] returns the num

Thanks Hans for hint !!

Now my script is working fine but with below changes

m="#{x}".match(/\d+$/)