Forum: Ruby-dev [ruby-trunk - Feature #5554][Open] A method that applies self to a Proc if self is a Symbol

Posted by Tsuyoshi Sawada (Guest)
on 2011-11-02 19:52
(Received via mailing list)
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
Posted by Nobuyoshi Nakada (nobu)
on 2011-11-03 01:42
(Received via mailing list)
なかだです。

(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"
Posted by Tsuyoshi Sawada (Guest)
on 2011-11-03 09:58
(Received via mailing list)
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
Posted by Yukihiro Matsumoto (Guest)
on 2011-11-03 11:41
(Received via mailing list)
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
Posted by mame (Yusuke Endoh) (Guest)
on 2012-11-20 13:00
(Received via mailing list)
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; 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
No account? Register here.