class Array
def contains_entry?(entry)
if self-[entry]==self
return false
else
return true
end
end
end
a=["one","two",3,4]
p a.contains_entry?("three")
The idea is to see whether the array stays the same when you take
away something from it - if yes, that something wasn't in the array
in the first place.
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
···
On Nov 15, 2007 4:59 AM, Nadeesha Meththananda <neranjan125@gmail.com> 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???
------------------------------------------------------------------------
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:
class Array
def contains_entry?(entry)
if self-[entry]==self
return false
else
return true
end
end
end
a=["one","two",3,4]
p a.contains_entry?("three")
The idea is to see whether the array stays the same when you take
away something from it - if yes, that something wasn't in the array
in the first place.
class Array
def contains_entry?(entry)
if self-[entry]==self
return false
else
return true
end
end
a=["one","two",3,4]
p a.contains_entry?("three")
The idea is to see whether the array stays the same when you take
away something from it - if yes, that something wasn't in the array
in the first place.
No, you definitely don't want to do that, for many reasons. As Jari
said, just use include?
if array.include?(entry)
David
--
Upcoming training by David A. Black/Ruby Power and Light, LLC:
* Advancing With Rails, Berlin, Germany, November 19-22
* Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.rubypal.com for details!
Datum: Thu, 15 Nov 2007 19:51:58 +0900
Von: "David A. Black" <dblack@rubypal.com>
An: ruby-talk@ruby-lang.org
Betreff: Re: Array checking ???
Hi --
>
> -------- Original-Nachricht --------
>> Datum: Thu, 15 Nov 2007 18:59:07 +0900
>> Von: Nadeesha Meththananda <neranjan125@gmail.com>
>> An: ruby-talk@ruby-lang.org
>> Betreff: Array checking ???
>
>>
>>
>> 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???
>> --
>> Posted via http://www.ruby-forum.com/\.
>
> Dear Nadeesha,
>
> you can use
>
> class Array
> def contains_entry?(entry)
> if self-[entry]==self
> return false
> else
> return true
> end
> end
> end
>
> a=["one","two",3,4]
> p a.contains_entry?("three")
>
> The idea is to see whether the array stays the same when you take
> away something from it - if yes, that something wasn't in the array
> in the first place.
No, you definitely don't want to do that, for many reasons. As Jari
said, just use include?
if array.include?(entry)
David
--
Upcoming training by David A. Black/Ruby Power and Light, LLC:
* Advancing With Rails, Berlin, Germany, November 19-22
* Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.rubypal.com for details!
Dear Nadeesha,
indeed, Jari's solution is way better. Shorter, more elegant.
I wouldn't have written
a post had Jari's response already been on the list.