I have:

array = ["this ends in log", "this ends in dog", "this is a log"]

I want to discard strings that end in the same word. How far off is my attempt?

array.reject { |i| i[0] =~ /\s\w$/ == i[1] =~ /\s\w$/ }

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/lPOGHXmhnwIJ.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Nicolas Sebrecht at Mar 9, 2012 at 3:32 pm

    The 08/03/12, simpleton wrote:
    I have:

    array = ["this ends in log", "this ends in dog", "this is a log"]

    I want to discard strings that end in the same word. How far off is my attempt?

    array.reject { |i| i[0] =~ /\s\w$/ == i[1] =~ /\s\w$/ }
    Three things come to my mind:

    #1

    Iterator i will take array elements as value:

    i = "this ends in log"
    i = "this ends in dog"
    i = "this is a log"

    So, you can work on variable i directly in the block.

    #2

    In your block, you must iterate the whole array instead of comparing
    only adjacent elements of the array.
    Of course, iterating the "whole array" is not optimised at all but it
    will run fine while the current implementation is buggy.

    #3

    Once a "last word" from string becomes uniq, you aren't going to delete
    the last uniq element.

    You need more advanced stuff than a simple comparison expression in the
    block.
    Also, you might need another array declared before the block where to
    store the duplicated "last word" in order to know that the last uniq
    "last word" was in fact a duplicated "last word".

    A simpler approach would be to:
    - first iterate the array to detect duplicated "last word" and store
    them (or their index) in another_array;
    - iterate the array again and remove known elements included in another_array.


    --
    Nicolas Sebrecht

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedMar 8, '12 at 5:57p
activeMar 9, '12 at 3:32p
posts2
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Simpleton: 1 post Nicolas Sebrecht: 1 post

People

Translate

site design / logo © 2022 Grokbase