Check if char in string?

how do i do this:

str = “you muppet”

if “y” in str:
print “its in there”

whats the best source for documentation on Ruby? i googled and checked
the homepage of ruby but there isnt really a clear one or im jus
duuumb.

globalrev wrote:

how do i do this:

str = “you muppet”

if “y” in str:
print “its in there”

whats the best source for documentation on Ruby? i googled and checked
the homepage of ruby but there isnt really a clear one or im jus
duuumb.

str = “you muppet”
puts “its in there” if str.include? ‘y’

On Wednesday 07 May 2008, globalrev wrote:

whats the best source for documentation on Ruby? i googled and checked
the homepage of ruby but there isnt really a clear one or im jus
duuumb.

http://www.ruby-doc.org/docs/ProgrammingRuby/

RDoc Documentation (reference for the core classes)

RDoc Documentation (reference for the standard library)

Stefano

you can use also Regular Expression:

!(“ciao” =~ /i/).nil?
#=> true

On 7 Maj, 18:32, Marc H. [email protected] wrote:

duuumb.

str = “you muppet”
puts “its in there” if str.include? ‘y’


Posted viahttp://www.ruby-forum.com/.

hmm it only works for one character though. i want to do:
if x in ‘aeiouy’:
do this
else:
do that

On 7 Maj, 19:39, [email protected] (matt neuburg) wrote:

if str =~ /y/

m.


matt neuburg, phd = [email protected],Matt Neuburg’s Home Page
Leopard -http://www.takecontrolbooks.com/leopard-customizing.html
AppleScript -Amazon.com
Read TidBITS! It’s free and smart.http://www.tidbits.com

but none of that is what i want to do.

i want to chekc if a char is in a string.
not check if a char equals another char.

globalrev [email protected] wrote:

how do i do this:

str = “you muppet”

if “y” in str:
print “its in there”

I always use regex in this situation:

if str =~ /y/

m.

The expression he gave uses the =~ operator, not the == operator. It
will
work if you try it in IRB (which I suggest you do before you post next
time)…

Dan

globalrev wrote:

On 7 Maj, 19:39, [email protected] (matt neuburg) wrote:

if str =~ /y/

but none of that is what i want to do.

i want to chekc if a char is in a string.
not check if a char equals another char.

Try the suggestion, you may be surprised :slight_smile:

globalrev [email protected] wrote:

if str =~ /y/

but none of that is what i want to do.

i want to chekc if a char is in a string.
not check if a char equals another char.

Try it!

m.

but none of that is what i want to do.

i want to chekc if a char is in a string.
not check if a char equals another char.

ok, you don’t know regexp

do it

On 7 Maj, 22:04, RedWiz [email protected] wrote:

but none of that is what i want to do.

i want to chekc if a char is in a string.
not check if a char equals another char.

ok, you don’t know regexp

do it

but
if str =~ /aeiouy/
do foo
end

doesnt work. or i mena it works but iw ant to chekc if it is a vowel
not if contains the expr aeiouy.
how do i delimit the expressins?

On Wed, May 7, 2008 at 1:40 PM, globalrev [email protected] wrote:

I always use regex in this situation:
Read TidBITS! It’s free and smart.http://www.tidbits.com

but none of that is what i want to do.

i want to chekc if a char is in a string.
not check if a char equals another char.

Along with the other suggestions, it might help you to read up on
regular expressions and what Ruby considers as true for a condition.

3 == false # false
3 == true # false

if 3; puts ‘hi’; end # hi
if {}; puts ‘hi’; end # hi
if []; puts ‘hi’; end # hi

Todd

globalrev wrote:

if str =~ /aeiouy/
do foo
end

doesnt work. or i mena it works but iw ant to chekc if it is a vowel
not if contains the expr aeiouy.
how do i delimit the expressins?

str =~ /[aeiouy]/

This checks if str contains one of those chars. Regular expressions are
worth learning more about.

globalrev wrote:

if str =~ /aeiouy/
do foo
end

doesnt work. or i mena it works but iw ant to chekc if it is a vowel
not if contains the expr aeiouy.
how do i delimit the expressins?

if str =~ /[aeiouy]/
do foo
end

[aeiouy] is a “character class”. The regular expression tests if any
character in the string is in the class.

On Wed, May 7, 2008 at 4:48 PM, Tim H. [email protected] wrote:

not if contains the expr aeiouy.
how do i delimit the expressins?

if str =~ /[aeiouy]/
do foo
end

[aeiouy] is a “character class”. The regular expression tests if any
character in the string is in the class.

Get a good book on regular expressions. Trust me, it will seriously
help you in the long run for most any type of programming/system
interaction. If you want a quick look at some syntax,
http://www.regular-expressions.info might help. Note that, this is
only one of many ways to do what you are trying to do, but I think it
is the best way for this case.

hth,
Todd

this way:
if str[x].chr =~ /[aeiouy]/
i can see if a char is any of the chars aeioyu

but i want to see if its not…
if str[x].chr !=~ /[aeiouy]/

doesnt work though…

and why cant i print [ in the windows ruby prompt?

Hi,

globalrev wrote:

this way:
if str[x].chr =~ /[aeiouy]/
i can see if a char is any of the chars aeioyu

but i want to see if its not…
if str[x].chr !=~ /[aeiouy]/

doesnt work though…

You can do like this:
if str[x].chr !~ /[aeiouy]/

Or
unless str[x].chr =~ /[aeiouy]/

Regards,
Park H.

On 2008-05-08, globalrev [email protected] wrote:

this way:
if str[x].chr =~ /[aeiouy]/
i can see if a char is any of the chars aeioyu

but i want to see if its not…
if str[x].chr !=~ /[aeiouy]/

how about something like:

if not str[x].chr =~ /[aeiouy]/ then print “YO\n” end

OR

if str[x].chr !~ /[aeiouy]/ then print “YO\n” end

On Wed, May 7, 2008 at 5:19 PM, Todd B. [email protected] wrote:

Get a good book on regular expressions. Trust me, it will seriously
help you in the long run for most any type of programming/system
interaction. If you want a quick look at some syntax,
http://www.regular-expressions.info might help. Note that, this is
only one of many ways to do what you are trying to do, but I think it
is the best way for this case.

Hmm…hmm (cough). I was talking to globalrev, not Tim :confused:

Todd