Check Whether a Variable Is a String in Ruby

Check whether a variable is a string in Ruby

I think you are looking for instance_of?. is_a? and kind_of? will return true for instances from derived classes.

class X < String
end

foo = X.new

foo.is_a? String # true
foo.kind_of? String # true
foo.instance_of? String # false
foo.instance_of? X # true

How to check if a variable is a number or a string?

There are several ways:

>> 1.class #=> Fixnum
>> "foo".class #=> String
>> 1.is_a? Numeric #=> true
>> "foo".is_a? String #=> true

How to check whether a string includes a given string variable?

You should try this

number.include?(digit.to_s)

You didn't convert the digit to string.

Hope this solves your problem.

How to check whether a string contains a substring in Ruby

You can use the include? method:

my_string = "abcdefg"
if my_string.include? "cde"
puts "String includes 'cde'"
end

Check if a string variable is in a set of strings

Which one is better? The question can't be easily answered, because they don't all do the same things.

x == 'abc' || x == 'def' || x == 'ghi'
%w(abc def ghi).include? x

compare x against fixed strings for equality. x has to be one of those values. Between those two I tend to go with the second because it's easier to maintain. Imagine what it would look like if you had to compare against twenty, fifty or one hundred strings.

The third test:

x ~= /abc|def|ghi/

matches substrings:

x = 'xyzghi'
(x =~ /abc|def|ghi/) # => 3

so it isn't the same as the first two.

EDIT: There are some things in the benchmarks done by nash that I'd do differently. Using Ruby 1.9.2-p180 on a MacBook Pro, this tests 1,000,000 loops and compares the results of anchoring the regex, using grouping, along with not splitting the %w() array each time through the loop:

require 'benchmark'
str = "test"

n = 1_000_000
Benchmark.bm do |x|
x.report { n.times { str == 'abc' || str == 'def' || str == 'ghi' } }
x.report { n.times { %w(abc def ghi).include? str } }
x.report { ary = %w(abc def ghi); n.times { ary.include? str } }
x.report { n.times { str =~ /abc|def|ghi/ } }
x.report { n.times { str =~ /^abc|def|ghi$/ } }
x.report { n.times { str =~ /^(abc|def|ghi)$/ } }
x.report { n.times { str =~ /^(?:abc|def|ghi)$/ } }
x.report { n.times { str =~ /\b(?:abc|def|ghi)\b/ } }
end
# >> user system total real
# >> 1.160000 0.000000 1.160000 ( 1.165331)
# >> 1.920000 0.000000 1.920000 ( 1.920120)
# >> 0.990000 0.000000 0.990000 ( 0.983921)
# >> 1.070000 0.000000 1.070000 ( 1.068140)
# >> 1.050000 0.010000 1.060000 ( 1.054852)
# >> 1.060000 0.000000 1.060000 ( 1.063909)
# >> 1.060000 0.000000 1.060000 ( 1.050813)
# >> 1.050000 0.000000 1.050000 ( 1.056147)

How to check if a string is one of several distinct values?

You could use include?:

if ['val1', 'val2', 'val3', 'val4'].include?(string)

Ruby way to check if a string is not blank?

string = ""

unless string.to_s.strip.empty?
# ...
end

Check if local variable is defined given it's name as string in ruby

You can do it using eval:

a = 'cat'
eval("defined?(#{'a'})")
=> "local-variable"
eval("defined?(#{'b'})")
=> nil

Disclaimer: This answer makes use of eval, so it can be dangerous if you don't strictly control the string you want to pass into it. And definitely you shouldn't do it this way if these strings come from user input.

How to properly check if a variable is in array using Ruby?

I tried these two lines

var_tea = 'Tea'
mycheck = ['Soap', 'Tea', 'Sugar'].include? var_tea

and mycheck is true

My guess is that you obviously don't have that variable in the line above, it's probably coming from a request, and that's not exactly Tea, maybe tea. Try to print var_tea before that check.

puts var_tea.inspect


Related Topics



Leave a reply



Submit