Issue #5554 has been reported by Tsuyoshi Sawada. ---------------------------------------- Feature #5554: A method that applies self to a Proc if self is a Symbol http://redmine.ruby-lang.org/issues/5554 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
on 2011-11-02 19:52
on 2011-11-03 01:42
なかだです。
(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"
on 2011-11-03 09:58
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
http://redmine.ruby-lang.org/issues/5554
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
on 2011-11-03 11:41
Issue #5554 has been updated by Yukihiro Matsumoto. Status changed from Open to Feedback ごめんなさい。「本当に意図したのは」という部分の意図が読み取れません。 どのような局面でどのように嬉しいか(たとえば劇的にシンプルに記述できるから)を表現してください。 ただ単に「こんな風に書きたいと私が感じる」だとたぶん不十分です。 自分でメソッド定義するかActiveSupportに提案すれば良いわけで。 ---------------------------------------- Feature #5554: A method that applies self to a Proc if self is a Symbol http://redmine.ruby-lang.org/issues/5554 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
on 2012-11-20 13:00
Issue #5554 has been updated by mame (Yusuke Endoh). Status changed from Feedback to Rejected 長期間フィードバックがない+望み薄と思われるので閉じます。 -- Yusuke Endoh <mame@tsg.ne.jp> ---------------------------------------- Feature #5554: A method that applies self to a Proc if self is a Symbol https://bugs.ruby-lang.org/issues/5554#change-33200 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
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.