2013-04-16 25 views
2

我正在做這樣的事情來檢查一個參數是否在一個集合中,並設置一個默認值,如果它不是。它看起來不必要的羅嗦:確保一個值在一個集合中,並設置一個默認值,如果它不是

allowed_types = [:poem, :product, :news, :facebook, :tweet] 
@type = params[:type] 
@type = :poem unless allowed_types.include?(@type) 

什麼是最完美的Ruby方式?這個怎麼樣

+1

你有興趣創建新的抽象嗎?這是我使用的一個:'@type = params [:type] .whitelist(allowed_types)|| :poem'。 – tokland

+0

@tokland現在看到我的!怎麼樣? :) –

+0

這屬於http://codereview.stackexchange.com –

回答

5

allowed_types = [:poem, :product, :news, :facebook, :tweet] 
@type = allowed_types.include?(params[:type]) ? params[:type] : :poem 

看出,我們在這裏,我要說的是,恕我直言,這將是有益的類似Hash#fetch陣列的方法;是這樣的:

class Array 
    # `fetch` is already taken (index fetching) 
    def fetch_value(something, ifnone = nil) 
    include?(something) ? something : ifnone 
    end 
end 

[:poem, :product, :news].fetch_value(:news, :poem) #=> :news 
[:poem, :product, :news].fetch_value(:salad, :poem) #=> :poem 

所以有人可以這樣做:

@type = allowed_types.fetch_value(params[:type], :poem) 
+2

三元是一個很好的解決方案。 – superluminary

+2

StackOverflow實現了一個實時語法錯誤檢查:它被稱爲@Mischa =) – mdesantis

+0

說實話,它看起來很複雜。包括!看起來像你想爲數組做任何類型的事情而不是檢查數組中的值。最好使用Hash的獲取方法。 – oivoodoo

2

你可以把它簡化這樣的:

allowed_types = [:poem, :product, :news, :facebook, :tweet] 
@type = allowed_types.include?(params[:type]) ? params[:type] : :poem 

但是你真正應該做的是驗證,儘量保持所有那裏的邏輯。也許是這樣的:

validates :type, inclusion: { in: %w(poem product news facebook tweet) } 

對於params我認爲你是在導軌或synatra。

+0

我也會驗證,但我也想這樣做:發送(:「new _#{@ type}」),並且不想冒着url攻擊的風險。 – superluminary

+0

現在看到我的!怎麼樣? :) –

1

你可以像這樣的模式:

class ModelName < ActiveRecord::Base 
    ALLOWED_TYPES = Hash[[:poem, :product, :news, :facebook, :tweet].map{|v| [v,v]}] # { :poem => :poem ... 
end 
控制器

您可以使用該ALLOWED_TYPES獲取方法:

ModelName::ALLOWED_TYPES.fetch(params[:type], :poem) # it will set as default poem 
+0

這是非常好的。 – superluminary

2
a = [1,2,4,5] 
var = a.detect(proc{"not present"}){ |x| x == 55 } 
p var #=> "not present" 

a = [1,2,4,5] 
var = a.detect(proc{"not present"}){ |x| x == 4 } 
p var #=> 4 

a = [1,2,4,5] 
var = a.detect(proc{"not present"}){ |x| x == 1 } 
p var #=> 1 
+0

@ProGNOMmers我改變了我的想法。 :) –

+0

好!我也喜歡'detect'解決方案,這是對Enumerable模塊的一個很好的使用。我可以刪除我的評論:) – mdesantis

+1

@ProGNOMmers非常感謝你:)我總是關心別人的評論。 :) :) –

1

使用標識哈希和Hash#fetch

allowed_types = Hash[ [:poem, :product, :news, :facebook, :tweet].map{|e| [e,e]} ] 
@type = allowed_types.fetch(@type, :poem) 
相關問題