From: Rick DeNatale Date: 2007-11-16T00:57:49+09:00 Subject: Re: Array checking ???????????????????? On Nov 15, 2007 4:59 AM, Nadeesha Meththananda wrote: > > > hi > > i have written a array like this. > > my_array = ["one","two","three","four"] > > no i want to check a given string is can be found from the above array. > > eg: > > if ( my_array.map{"one"} ) > return true; > else > return false; > end > > > so here if i use "five" insted of "one" it should return false( as i > think) but it does no happen. > > (simply want to check a given string is in the array or not) > > so if any body can help???? Others have told you what works. Let's talk about why this doesn't. What does the map method do? Here's the ri doc array.map {|item| block } -> an_array ------------------------------------------------------------------------ Invokes _block_ once for each element of _self_. Creates a new array containing the values returned by the block Normally you have an argument in the block attached to map, to receive each element of the array, you didn't but Ruby let's you get away with that. Now let' run your code and see what the map returns: my_array = ["one","two","three","four"] my_array.map{"one"} # => ["one", "one", "one", "one"] So it's just an array with the same number of elements, each with a value of "one". And since it's neither false, nor nil, this will always be treated as true in a boolean context. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/