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'

···

--
Posted via http://www.ruby-forum.com/\.

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

RDoc Documentation (reference for the core classes)

RDoc Documentation (reference for the standard library)

Stefano

···

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.

I always use regex in this situation:

if str =~ /y/

m.

···

globalrev <skanemupp@yahoo.se> wrote:

how do i do this:

str = "you muppet"

if "y" in str:
        print "its in there"

--
matt neuburg, phd = matt@tidbits.com, Matt Neuburg’s Home Page
Leopard - http://www.takecontrolbooks.com/leopard-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com

you can use also Regular Expression:

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

···

On 5/7/08, Stefano Crocco <stefano.crocco@alice.it> wrote:

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.

Programming Ruby: The Pragmatic Programmer's Guide

RDoc Documentation (reference for the core classes)

RDoc Documentation (reference for the standard library)

Stefano

--
Go outside! The graphics are amazing!

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

···

On 7 Maj, 18:32, Marc Heiler <sheve...@linuxmail.org> wrote:

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'

--
Posted viahttp://www.ruby-forum.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.

···

On 7 Maj, 19:39, m...@tidbits.com (matt neuburg) wrote:

globalrev <skanem...@yahoo.se> 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.

--
matt neuburg, phd = m...@tidbits.com,Matt Neuburg’s Home Page
Leopard -http://www.takecontrolbooks.com/leopard-customizing.html
AppleScript -http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart.http://www.tidbits.com

globalrev 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:

···

On 7 Maj, 19:39, m...@tidbits.com (matt neuburg) wrote:

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

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

···

On 5/7/08, globalrev <skanemupp@yahoo.se> wrote:

On 7 Maj, 19:39, m...@tidbits.com (matt neuburg) wrote:
> globalrev <skanem...@yahoo.se> 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.
>
> --
> matt neuburg, phd = m...@tidbits.com,Matt Neuburg’s Home Page
> Leopard -http://www.takecontrolbooks.com/leopard-customizing.html
> AppleScript -http://www.amazon.com/gp/product/0596102119
> 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.

Try it!

m.

···

globalrev <skanemupp@yahoo.se> wrote:

On 7 Maj, 19:39, m...@tidbits.com (matt neuburg) wrote:
> globalrev <skanem...@yahoo.se> 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/
>

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.

--
matt neuburg, phd = matt@tidbits.com, Matt Neuburg’s Home Page
Leopard - http://www.takecontrolbooks.com/leopard-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com

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

···

On Wed, May 7, 2008 at 1:40 PM, globalrev <skanemupp@yahoo.se> wrote:

On 7 Maj, 19:39, m...@tidbits.com (matt neuburg) wrote:

> globalrev <skanem...@yahoo.se> 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.
>
> --
> matt neuburg, phd = m...@tidbits.com,Matt Neuburg’s Home Page

> Leopard -http://www.takecontrolbooks.com/leopard-customizing.html
> AppleScript -http://www.amazon.com/gp/product/0596102119
> 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.

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 7 Maj, 22:04, RedWiz <red...@inwind.it> 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

globalrev wrote:

···

On 7 Maj, 22:04, RedWiz <red...@inwind.it> 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?

str =~ /[aeiouy]/

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

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

globalrev wrote:

···

On 7 Maj, 22:04, RedWiz <red...@inwind.it> 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?

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.

--
RMagick: http://rmagick.rubyforge.org/
RMagick 2: http://rmagick.rubyforge.org/rmagick2.html

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

···

On Wed, May 7, 2008 at 4:48 PM, Tim Hunter <TimHunter@nc.rr.com> wrote:

globalrev wrote:

> On 7 Maj, 22:04, RedWiz <red...@inwind.it> 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?
>
>

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.

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

Todd

···

On Wed, May 7, 2008 at 5:19 PM, Todd Benson <caduceass@gmail.com> wrote:

On Wed, May 7, 2008 at 4:48 PM, Tim Hunter <TimHunter@nc.rr.com> wrote:
> globalrev wrote:
>
> 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.

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.chr =~ /[aeiouy]/
i can see if a char is any of the chars aeioyu

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

doesnt work though...

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

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

Regards,
Park Heesob

···

--
Posted via http://www.ruby-forum.com/\.

how about something like:

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

OR

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

···

On 2008-05-08, globalrev <skanemupp@yahoo.se> wrote:

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

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

--