What happened to any? in Ruby 1.9

0 views
Skip to first unread message

Tim Morgan

unread,
Sep 16, 2009, 3:41:22 PM9/16/09
to tul...@googlegroups.com
OK, so I see one of my favorite string methods "any?" is now missing from Ruby 1.9. I'm hurt.

$ irb1.8
>> ''.any?
=> false
>> exit

$ irb1.9
irb(main):001:0> ''.any?
NoMethodError: undefined method `any?' for "":String
    from (irb):1
    from /usr/bin/irb1.9:12:in `<main>'
irb(main):002:0> exit

1. Why in the world would this be removed?
2. Any harm in defining it myself?
3. Is there an alternative, blessed way of determining if a string has something inside? Do I really have to change all my usage of any to !empty?


Justin Richter

unread,
Sep 16, 2009, 5:34:19 PM9/16/09
to tul...@googlegroups.com
http://www.rhinocerus.net/forum/lang-ruby/436631-ruby-1-9-doesnt-hace-string-any.html


On Thu, Feb 26, 2009 at 6:33 PM, I=F1aki Baz Castillo <i...@aliax.net> wrote=
:
> El Viernes, 27 de Febrero de 2009, I=F1aki Baz Castillo escribi=F3:
>> Hi, I'm trying:
>> =A0 ruby 1.9.0 (2007-08-30 patchlevel 0) [i486-linux]
>> in Ubuntu.
>>
>> I've realized that there is no method String#any?
>> Why? maybe it exists in a more recent version of 1.9?
>> Which would be the replacement method if not?

>
> Ok I've found that in Ruby 1.9 String is no more an Enumerable (and "any?=

" is
> defined in Enumerable module).


Right, so you just need to be specific about what you want to enumerate ove=
r:

>> "foo\nbar".lines.any? { |e| e =3D=3D "bar" }

=3D> true
>> "foo\nbar".chars.any? { |e| e =3D=3D "b" }

=3D> true
>> "foo\nbar".bytes.any? { |e| e =3D=3D 97 }

=3D> true
>> 97.chr

=3D> "a"


-greg

--=20
Technical Blaag at: http://blog.majesticseacreature.com
Non-tech stuff at: http://metametta.blogspot.com
"Ruby Best Practices" Book now in O'Reilly Roughcuts:
http://rubybestpractices.com

Reply With Quote
Reply all
Reply to author
Forward
0 new messages