This question already has answers here:
How to test if a string is basically an integer in quotes using Ruby
(19 answers)
Closed 9 years ago.
I am trying to test if my variables are Integer, here is the code :
if (params[:int1].is_a?(Integer) && params[:int2].is_a?(Integer))
add(params[:int1], params[:int2])
else
puts "Need two integers"
end
If you know why it doesn't works, you have all my attention.
params= { int1: "1" }
puts params[:int1].class
> String
Your params hash probably contains string values instead of Integers. If you want to check if a string is a valid integer, you can try validating it against a regex like so:
if /\d+/=~ params[:int1]
# do stuff
end
params[] stores only strings. You need to cast them to integers.
Try something like:
params[:int1].empty? ? raise EmptyIntegerException : my_int1 = params[:int1].to_i
Related
This question already has answers here:
VBS using LIKE to compare strings "Sub or Function not defined"
(4 answers)
Closed 1 year ago.
I have a IF statement where I need to use LIKE to determin if the result set contains an e-mail address, and this is similar to the session ADMail .. the initial code I have tried with is :
UserEmailForTask = Session("ADmail")
IF objGetTaskEmailsSettings("IT_Email") LIKE %UserEmailForTask% OR Session("ADdepartment") = "IT" THEN
Dim IT_User_Tasks
IT_User_Tasks = "YES"
END IF
objGetTaskEmailsSettings("IT_Email") can contain eigther "user1#mail.com" or have multiple emails inside it like "user1#mail.com,user2#mail.com,user3#mail.com" where I need to check if the email in UserEmailForTask is included.
Obviously I will get a fail on %UserEmailForTask%when using this, but how do I solve this?
The InStr function returns the position of the first occurrence of one string within another. Use it to check if the given string contains the search string:
UserEmailForTask = "..."
IF InStr(objGetTaskEmailsSettings("IT_Email"),UserEmailForTask)>0
OR Session("ADdepartment") = "IT" THEN
Dim IT_User_Tasks
IT_User_Tasks = "YES"
END IF
This question already has answers here:
How do I find if a string starts with another string in Ruby?
(4 answers)
Closed 3 years ago.
image_basename = 'fr-ca-test.png'
Langs = {'ca', 'fr-CA', 'en-CA'}
Langs.each do |locale_code|
return locale_code /(\b|\_|-)#{locale_code}(\b|\_|-)/i.match(image_basename)
end
end
When the filename contains fr-CA or en-CA. I would like to returns fr-CA not Ca.
How I can fix my regex?
I wouldn't use a regexp in this simple example. Using start_with? will very likely be faster and IMHO it is easier to read and to understand:
image_basename = 'fr-ca-test.png'
LANGUAGES = ['fr-CA', 'en-CA', 'ca']
LANGUAGES.find { |code| image_basename.start_with?(code.downcase) }
#=> "fr-CA"
This question already has answers here:
Ruby combining an array into one string
(4 answers)
Closed 6 years ago.
i have an array like this
text_arr = ["hello","how","are","you"]
and i want to convert this to string like this
text = "hello how are you"
How can i do this with Ruby ?
Try this one
text = text_arr.join(' ')
This question already has answers here:
How to format a number 1000 as "1 000"
(12 answers)
Closed 8 years ago.
I was working on a method to add commas to a number that is passed. I.E. separate_commas(1000) would return "1,000" or separate_commas(100000) would return "100,000"...etc.
Now that I've solved it, I'm wondering how I could refactor without regular expressions. Would appreciate suggestions, thank you in advance. Ruby 2.1.1p76
def separate_comma(x)
x=x.to_s
len=-4
until len.abs > x.length
x.insert(len, ',')
len-=4
end
return x
end
Not exactly pretty, but it is a little more ruby-esque
num.to_s.reverse
.split('').each_slice(3).to_a
.map{|num| num.reverse.join('')}.reverse.join(',')
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert character code to what I want?
I am positive this has to be a duplicate, but all search results were in other languages or were the reverse (character to code point).
e.g.
charCode = 96
string = # ... ?
You could do it with:
string = charCode.chr
The .chr method on Fixnum.
96.chr #=> "c"