[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Check if char in string?

skanemupp

5/7/2008 4:27:00 PM

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.
28 Answers

Marc Heiler

5/7/2008 4:33:00 PM

0

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-....

Stefano Crocco

5/7/2008 4:43:00 PM

0

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/Progra...

http://www.ruby-do... (reference for the core classes)

http://www.ruby-doc.o... (reference for the standard library)

Stefano





Sandro Paganotti

5/7/2008 4:52:00 PM

0

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.
>
>
> http://www.ruby-doc.org/docs/Progra...
>
> http://www.ruby-do... (reference for the core classes)
>
> http://www.ruby-doc.o... (reference for the standard library)
>
>
> Stefano
>
>
>
>
>
>


--
Go outside! The graphics are amazing!

skanemupp

5/7/2008 5:03:00 PM

0

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-....


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

matt

5/7/2008 5:40:00 PM

0

globalrev <skanemupp@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 = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

skanemupp

5/7/2008 6:38:00 PM

0

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,http://www.tidbits...
> Leopard -http://www.takecontrolbooks.com/leopard-custom...
> AppleScript -http://www.amazon.com/gp/product/...
> Read TidBITS! It's free and smart.http://www.t...



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.

Joel VanderWerf

5/7/2008 6:46:00 PM

0

globalrev wrote:
> On 7 Maj, 19:39, m...@tidbits.com (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 :)

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

Daniel Finnie

5/7/2008 6:48:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

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,http://www.tidbits...
> > Leopard -http://www.takecontrolbooks.com/leopard-custom...
> > AppleScript -http://www.amazon.com/gp/product/...
> > Read TidBITS! It's free and smart.http://www.t...
>
>
>
> 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

5/7/2008 6:53:00 PM

0

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.

Try it!

m.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

Todd Benson

5/7/2008 7:00:00 PM

0

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,http://www.tidbits...
>
> > Leopard -http://www.takecontrolbooks.com/leopard-custom...
> > AppleScript -http://www.amazon.com/gp/product/...
> > Read TidBITS! It's free and smart.http://www.t...
>
>
>
> 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