Il giorno Sun, 22 Jan 2012 05:46:00 +0900
josh smith < ... at hotmail dot com> ha scritto:
The first line asks the array ["recycler"] whether it includes the element
'recicle'. This means that you're calling the method Array#include? . If you
look at its documentation, you'll see that it returns true only if one of the
elements of the array is equal as the argument. In your example, the array
contains a single argument, "recycler", which is different from the argument
('recycle).
When calling "recycler".include?('recycle'), instead, you're calling the
method String#include?, which returns true if the string contains the
argument. Since the word "recycler" contains the word "recycle", in your
example, it returns true.
On Sat, Jan 21, 2012 at 10:03 PM, josh smith < ... at hotmail dot com> wrote:
Maybe you want to check whether there is any string which contains the
sequence "recycle". Then you can do:
On Sunday 22 January 2012 at 2:16 AM, josh smith wrote:
Hi,
The .include? method on an array and the one on a string have different functions.
For an array, .include? will check whether any of the elements are equal to the argument, while in a string it will check whether the argument is a substring of the string.
Comments
Re: DIfferent outputs?
By Stefano Crocco at 01/21/2012 - 16:58Il giorno Sun, 22 Jan 2012 05:46:00 +0900
josh smith < ... at hotmail dot com> ha scritto:
The first line asks the array ["recycler"] whether it includes the element
'recicle'. This means that you're calling the method Array#include? . If you
look at its documentation, you'll see that it returns true only if one of the
elements of the array is equal as the argument. In your example, the array
contains a single argument, "recycler", which is different from the argument
('recycle).
When calling "recycler".include?('recycle'), instead, you're calling the
method String#include?, which returns true if the string contains the
argument. Since the word "recycler" contains the word "recycle", in your
example, it returns true.
I hope this helps
Stefano
Re: DIfferent outputs?
By josh smith at 01/21/2012 - 17:03It does. Thank alot!
Re: DIfferent outputs?
By Robert Klemme at 01/23/2012 - 08:47On Sat, Jan 21, 2012 at 10:03 PM, josh smith < ... at hotmail dot com> wrote:
Maybe you want to check whether there is any string which contains the
sequence "recycle". Then you can do:
irb(main):003:0> ["recycler"].any? {|s| s.include?("recycle")}
=> true
Kind regards
robert
Re: DIfferent outputs?
By Vikhyat Korrapati at 01/21/2012 - 16:49On Sunday 22 January 2012 at 2:16 AM, josh smith wrote:
Hi,
The .include? method on an array and the one on a string have different functions.
For an array, .include? will check whether any of the elements are equal to the argument, while in a string it will check whether the argument is a substring of the string.
That is why the output is different.
Vikhyat Korrapati
<a href="http://vikhyat.net/" title="http://vikhyat.net/">http://vikhyat.net/</a>
Re: DIfferent outputs?
By josh smith at 01/21/2012 - 17:02thanks :)