2012-01-14 4 views
0

2つの配列にはアセットのオブジェクトが含まれていますが、今度は2番目の配列にはない最初の配列からオブジェクトだけを取り除きたいと思います。私は " - "右を使うべきですか?ここでオブジェクト値によるRuby配列の減算

は私のオブジェクトが

 

class Asset 
    attr_accessor :id, :url, :text, :img_url, :type, :created_at 

    def initialize(url, text, type, img_url, created_at) 
    @id, @url, @text, @img_url, @type, @created_at = "", url, text, img_url, type, created_at 
    end 

    def eql?(another_asset) 
    self_domain = UrlUtils::get_domain(self.url) 
    another_asset_domain = UrlUtils::get_domain(another_asset.url) 
    if self_domain == 'youtube' && another_asset_domain == 'youtube' 
     self_youtube_id = UrlUtils::get_parameter(self.url, "v") 
     another_asset_youtube_id = UrlUtils::get_parameter(another_asset.url, "v") 
     return self_youtube_id.eql?(another_asset_youtube_id) 
    end 
    return self.url.eql?(another_asset.url) 
    end 

    def hash 
     @created_at.hash + 32 * @url.hash 
    end 
end 
 

ある考え方は、すべてのURLが異なる可能性がありますユーチューブからURLを含めることができる1つの資産であるが、それは同じ映像ですので、私は(パラメータ「V」で各URLを比較する必要がyoutube_id)。

これは正しく差し引きをしないので、これは間違っています。

 

it "should substract duplicated youtube from mixed assets" do 
    mixed_assets = Array.new 
    all_assets = Array.new 

    google = Asset.new("http://www.google.com", "", "", "", Time.now) 
    youtube = Asset.new("http://www.youtube.com?v=1", "", "", "", Time.now) 
    mixed_assets.push(google) 
    mixed_assets.push(youtube) 

    another_youtube = Asset.new("http://www.youtube.com?v=1&a=1", "", "", "", Time.now) 
    all_assets.push(another_youtube) 

    mixed_assets = mixed_assets - all_assets 
    mixed_assets.length.should eql 1 
    mixed_assets[0].url.should eql "http://www.google.com" 

    end 
 

私は、Rubyのは非常に新しいだと私は私も「ハッシュ」メソッドを実装する必要がありますが、私はそれを行うにはどのように任意の例を見つけることができなかったいくつかの研究をしました。

ありがとうございました。

+0

だから、うまくいきませんか? –

+0

それは動作しません、テストはちょうどそれがGoogleの資産でなければならない1つの配列を返す必要があります。 – toy

答えて

2

配列減算はハッシュを使用して動作するので、正しいです。私はUrlUtilsが何であるかを知らないので、私はテストができませんでしたが、次のようなものは、あなたがAssetクラスに追加する必要がどのような可能性があります:あなたはまた、eql?方法が必要な場合があります

def hash 
    domain = UrlUtils::get_domain(self.url) 
    v = domain == 'youtube' ? UrlUtils::get_parameter(self.url, "v") : '' 
    domain.hash^v.hash 
end 

this postには、おそらくあなたが見たい情報がたくさんあります。それだけでなく、関連するトピックの束をカバーしています。

+0

それは働いた!どうもありがとう。 – toy

+0

@toy問題ありません。私はそれを少し更新しました。かなり正しくはありませんでした(それはまだではないかもしれませんが、それはもっと近いです:) –

関連する問題