Issue #5554 has been reported by Tsuyoshi Sawada.
Feature #5554: A method that applies self to a Proc if self is a Symbol
Author: Tsuyoshi Sawada
Status: Open
Priority: Normal
Assignee:
Category:
Target version:
Often, you want to apply a Proc to self if self is a Symbol, but not do
anything if otherwise. In this case, something I call Object#desymbolize
may be convenient:
proc = ->sym{
case sym
when :small_icon then "16pt"
when :medium_icon then "32pt"
when :large_icon then "64pt"
end
}
:small_icon.desymbolize(&proc) => "16pt"
"18pt".desymbolize(&proc) => "18pt"
An implementation may be as follows:
class Object
def desymbolize; self end
end
class Symbol
def desymbolize ≺ pr.call(self) end
end
なかだです。
(11/11/03 3:51), Tsuyoshi Sawada wrote:
"18pt".desymbolize(&proc) => "18pt"
"often"というのは疑問がありますし、特にSymbolに関係する機能とも思えませ
ん。Hashではどうでしょうか。
desymbol = Hash.new {|h, k| k}
desymbol[:small_icon] = “16pt”
desymbol[:medium_icon] = “32pt”
desymbol[:large_icon] = “64pt”
desymbol[:small_icon] #=> “16pt”
desymbol[“18pt”] #=> “18pt”
Issue #5554 has been updated by Tsuyoshi Sawada.
本当は、意図したのは、どのようなSymbolがあるか簡単には分からない場合、もしくは別のところで関連するメソッドが定義されていて、Symbolの一覧を複数の場所で更新しなければならなくなる場合で、例えば次のような場合です:
proc =>sym{some_method_chain.send(sym)} # sym can be :foo, :bar,
…, which are defined elsewhere
でも、中田さんのおっしゃているのも分かります。
Feature #5554: A method that applies self to a Proc if self is a Symbol
Author: Tsuyoshi Sawada
Status: Open
Priority: Normal
Assignee:
Category:
Target version:
Often, you want to apply a Proc to self if self is a Symbol, but not do
anything if otherwise. In this case, something I call Object#desymbolize
may be convenient:
proc = ->sym{
case sym
when :small_icon then "16pt"
when :medium_icon then "32pt"
when :large_icon then "64pt"
end
}
:small_icon.desymbolize(&proc) => "16pt"
"18pt".desymbolize(&proc) => "18pt"
An implementation may be as follows:
class Object
def desymbolize; self end
end
class Symbol
def desymbolize ≺ pr.call(self) end
end
Issue #5554 has been updated by Yukihiro M…
Status changed from Open to Feedback
ごめんなさい。「本当に意図したのは」という部分の意図が読み取れません。
どのような局面でどのように嬉しいか(たとえば劇的にシンプルに記述できるから)を表現してください。
ただ単に「こんな風に書きたいと私が感じる」だとたぶん不十分です。
自分でメソッド定義するかActiveSupportに提案すれば良いわけで。
Feature #5554: A method that applies self to a Proc if self is a Symbol
Author: Tsuyoshi Sawada
Status: Feedback
Priority: Normal
Assignee:
Category:
Target version:
Often, you want to apply a Proc to self if self is a Symbol, but not do
anything if otherwise. In this case, something I call Object#desymbolize
may be convenient:
proc = ->sym{
case sym
when :small_icon then "16pt"
when :medium_icon then "32pt"
when :large_icon then "64pt"
end
}
:small_icon.desymbolize(&proc) => "16pt"
"18pt".desymbolize(&proc) => "18pt"
An implementation may be as follows:
class Object
def desymbolize; self end
end
class Symbol
def desymbolize ≺ pr.call(self) end
end
Issue #5554 has been updated by mame (Yusuke E.).
Status changed from Feedback to Rejected
長期間フィードバックがない+望み薄と思われるので閉じます。
Feature #5554: A method that applies self to a Proc if self is a Symbol
Author: sawa (Tsuyoshi Sawada)
Status: Rejected
Priority: Normal
Assignee:
Category:
Target version:
Often, you want to apply a Proc to self if self is a Symbol, but not do
anything if otherwise. In this case, something I call Object#desymbolize
may be convenient:
proc = ->sym{
case sym
when :small_icon then "16pt"
when :medium_icon then "32pt"
when :large_icon then "64pt"
end
}
:small_icon.desymbolize(&proc) => "16pt"
"18pt".desymbolize(&proc) => "18pt"
An implementation may be as follows:
class Object
def desymbolize; self end
end
class Symbol
def desymbolize ≺ pr.call(self) end
end