Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Well, I learned about the cover? Method, so that's a big plus for a typical Saturday...but I'm confused as to why include? for ranges doesn't just act as an alias for cover?

I can't think of a Range that is not meant to be continuous...I would test out some cases right now if my computer weren't grinding away at a database insertion process...

edit: OK, tried it out....as I suspected, Ranges that consists of Strings will have different behavior:

    [16] pry(main)> ('aa'..'zz').include?('c')
    => false
    [17] pry(main)> ('aa'..'zz').cover?('c')
    => true
    [18] pry(main)> ('aa'..'zz').include?('cc')
    => true
    [19] pry(main)> ('aa'..'zz').cover?('cc')
    => true
    [20] pry(main)> ('aa'..'zz').cover?('cccccc')


Ranges are always continuous, but `cover?` uses the comparison operators, while `include?` iterates over the items in the range in some cases. `include?` is practical in cases where you don't want to implement the comparison operators.

See https://news.ycombinator.com/item?id=6076608




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: