Idiomatic Ruby

22 views
Skip to first unread message

Leandro TK

unread,
Apr 1, 2017, 2:02:38 AM4/1/17
to Ruby on Rails: Talk

Hello guys,


As all of you, I think Ruby a beautiful programming language. And I wrote a blog post about that.


I call it Idiomatic Ruby


Any comment or advice will alwasy be welcome :)

Scott Jacobsen

unread,
Apr 1, 2017, 9:41:01 AM4/1/17
to Ruby on Rails: Talk
Nice post. But you shouldn't use any? to test if a collection is empty. To see why try it on this array: [nil, false] and read the docs. Use empty? to test if an array is empty. 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/9deec1e6-c3d9-4882-ad92-9fda482f215e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Leandro TK

unread,
Apr 1, 2017, 10:44:40 AM4/1/17
to Ruby on Rails: Talk
Hey Scott,

You're absolutely right. If we want to ask an array if it is empty, we can use the .empty? method. But if we want to ask if the array has anything inside it, we can use .any? method :)



On Saturday, April 1, 2017 at 10:41:01 AM UTC-3, Scott Jacobsen wrote:
Nice post. But you shouldn't use any? to test if a collection is empty. To see why try it on this array: [nil, false] and read the docs. Use empty? to test if an array is empty. 
On Apr 1, 2017 12:02 AM, "Leandro TK" <leandr...@gmail.com> wrote:

Hello guys,


As all of you, I think Ruby a beautiful programming language. And I wrote a blog post about that.


I call it Idiomatic Ruby


Any comment or advice will alwasy be welcome :)

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Scott Jacobsen

unread,
Apr 1, 2017, 1:47:00 PM4/1/17
to Ruby on Rails: Talk
That's not what #any? is for, but it is a common misconception. Try running the code I suggested you run, and read the docs I suggested you read to see why. To be more clear, try running this - `[false].any?` and `[false].empty?` and see if you get what you expect.

To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0d002cb8-6d71-46b2-968c-2feae0e4d331%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages