? Check whether a variable is a string in Ruby

Is there anything more idiomatic than the following?foo.class == String I think you are looking for instance_of?. is_a? and kind_of? will return true for instances from derived classes. class X <
 ? Common Ruby Idioms

One thing I love about ruby is that mostly it is a very readable language (which is great for self-documenting code)However, inspired by this question: Ruby Code explainedand the description of how ||
 ? Best ruby idiom for "nil or zero"

I am looking for a concise way to check a value to see if it is nil or zero. Currently I am doing something like:if (!val || val == 0) # Is nil or zeroendBut this seems very clumsy. Objects have
 ? Ruby idiom for default values

Is there a better way to write this in Ruby, without writing "bar" twice?foo = bar > 0 ? bar : 1 Not a generic use case, but:foo = [bar, 1].max [XXX]$ irb>> x ||= "default"=> "defaul
 ? Ruby - cleanest way to return a value if truthy, or execute arbitrary code

I'm coding something in Ruby where, given a value foo output from a method call, I want to:Return foo if foo is truthyLog an error and return a default value if foo is falsy.The simplest naive way to
 ? Script a ruby command-line app; best way to do this?

I have a command line Ruby app I'm developing and I want to allow a user of it to provide code that will run as a filter on part of the process. Basically, the application does this:read in some data
 ? ruby capitalize doesnt work for the first word of the title

the code below is to capitalize all words except for 'littleWords' and the first word of the title. (even if it belongs to the littleWords, the first word should be capitalized.) def titleize (word)
 ? Replace all non capitalised words with ruby

I would like to replace all non capitalised words in a text with "-".length of the word.For instance I have the following Text (German): Florian Homm wuchs als Sohn des mittelständischen Handwerksunt
 ? Ruby - remove underscores and capitalize

Okay so I'm trying to remove the underscores, as seen in some of the holidays (for example,fourth_of_july). Then I want to capitalize each of the words.Expected result: fourth_of_july > Fourth Of July
 ? How to check whether a string contains a substring in Ruby?

I have a string variable with content as follows:varMessage = "hi/thsid/sdfhsjdf/dfjsd/sdjfsdn\n" "/my/name/is/balaji.so\n" "call::myFunction(int const&)\n"
 ? How to check whether a string contains a substring in Ruby?

I have a string variable with content as follows:varMessage = "hi/thsid/sdfhsjdf/dfjsd/sdjfsdn\n" "/my/name/is/balaji.so\n" "call::myFunction(int const&)\n"
 ? How to check whether a string contains a substring in Ruby?

I have a string variable with content as follows:varMessage = "hi/thsid/sdfhsjdf/dfjsd/sdjfsdn\n" "/my/name/is/balaji.so\n" "call::myFunction(int const&)\n"
 ? Equivalent python 'in' on ruby

Im new to ruby, and i want to try translate a code python to ruby.What would be a simple, and similarly on this.print 'it has' if 'ten' in 'tentacle' else 'None' Ruby String has method include? so
 ? How to count a string elements' occurrence in another string in ruby?

How can I check how many times a phrase occurs in a string?For example, let's say the phrase is donutstr1 = "I love donuts!"#=> returns 1 because "donuts" is found once.str2 = "Squirrels do love nu
 ? Convert String to Regexp in Ruby

I frequently need to convert a String into a Regexp. For many strings, Regexp.new(string) is sufficient. But if string contains special characters, they need to be escaped:string = "foo(bar)"regex = R
 ? Array includes string ruby?

How to check if array includes string element?address = 'London is a capital of GB' def get_location(address) all_cities = ['Amsterdam','Moscow','Krakow','London'] city = all_cities.index {|x|

Page 1 of 534  |  Show More Pages:  Top Prev Next Last