Hi there,
I'm working on a DB that has the rows structured as a ruby class called
Result, structured as follows:
class Result
attr_accessor :name, :link, :messages
end
very simple one.
The catch is that there are a lot of doubles in terms of LINK attribute.
I'm trying to create a method that erases all the Result(s) that shares
the same :link attribute with another Result.
def erase_doubles
@results.each do |result|
#I REALLY DON'T HAVE IDEA ABOUT HOW TO BUILD IT :)
end
end
on 2012-12-10 16:49
on 2012-12-10 17:51
On Mon, Dec 10, 2012 at 7:49 AM, Leo M. <lists@ruby-forum.com> wrote: > I'm working on a DB that has the rows structured as a ruby class called > Result, structured as follows: > > class Result > attr_accessor :name, :link, :messages > end > The catch is that there are a lot of doubles in terms of LINK attribute. > I'm trying to create a method that erases all the Result(s) that shares > the same :link attribute with another Result. So you need to write two methods: 1) one that accesses the DB to remove a specified row, and 2) one that identifies which rows to remove. For #2 you need to define which duplicate you want to keep: first? last? based on some other criteria? This might be a good time to start writing tests :-) HTH,
on 2012-12-10 22:00
Leo M. wrote in post #1088536: > > def erase_doubles > > @results.each do |result| > > #I REALLY DON'T HAVE IDEA ABOUT HOW TO BUILD IT :) > > end > class Result attr_accessor :name, :link, :messages def initialize(name, link, msg) @name = name @link = link @messages = msg end end links = ['a', 'b', 'a', 'c', 'c'] names = ["Joe", "Bob", "Cathy", "Doug", "Carol"] messages = ["mars", "venus", "jupiter", "mercury", "earth"] links_enum = links.each names_enum = names.each msg_enum = messages.each results = names.map do |link| Result.new(names_enum.next, links_enum.next, msg_enum.next) end p results --output:-- [#<Result:0x00000101093290 @name="Joe", @link="a", @msg="mars">, #<Result:0x000001010931f0 @name="Bob", @link="b", @msg="venus">, #<Result:0x00000101093150 @name="Cathy", @link="a", @msg="jupiter">, #<Result:0x000001010930b0 @name="Doug", @link="c", @msg="mercury">, #<Result:0x00000101093010 @name="Carol", @link="c", @msg="earth">] hash ={} results.each do |r| hash[r.link] = r end p hash.values --output:-- [#<Result:0x00000101093150 @name="Cathy", @link="a", @msg="jupiter">, #<Result:0x000001010931f0 @name="Bob", @link="b", @msg="venus">, #<Result:0x00000101093010 @name="Carol", @link="c", @msg="earth">]
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.