[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Symbol#re_s

Trans

9/20/2008 12:20:00 PM

Came up with a great little extension today for the Symbol class. One
that also shows off Facets' Functor class:

require 'facets/functor'

class Symbol

# Convert symbol to string, apply string method
# and convert back to symbol via a fluent
# interface.
#
# :HELLO.re_s.downcase #=> :hello
#
def re_s
@re_s ||= Functor.new do |op, *a|
to_s.send(op, *a).to_sym
end
end

end

I picked the name 're_s' off the top of my head. I'm open to better
suggestions.

T.


9 Answers

TPReal

9/20/2008 3:26:00 PM

0

Thomas Sawyer wrote:
> Came up with a great little extension today for the Symbol class. One
> that also shows off Facets' Functor class:
>
> require 'facets/functor'
>
> class Symbol
>
> # Convert symbol to string, apply string method
> # and convert back to symbol via a fluent
> # interface.
> #
> # :HELLO.re_s.downcase #=> :hello
> #
> def re_s
> @re_s ||= Functor.new do |op, *a|
> to_s.send(op, *a).to_sym
> end
> end
>
> end
>
> I picked the name 're_s' off the top of my head. I'm open to better
> suggestions.
>
> T.

Nice idea. After some modification (using method_missing) it could be
used as a temporary patch on Ruby 1.8, as in Ruby 1.9 symbols are going
to respond to most string-like methods.

also T.
--
Posted via http://www.ruby-....

Trans

9/20/2008 6:44:00 PM

0


On Sep 20, 11:25=A0am, "Thomas B." <tpr...@gmail.com> wrote:

> Nice idea. After some modification (using method_missing) it could be
> used as a temporary patch on Ruby 1.8, as in Ruby 1.9 symbols are going
> to respond to most string-like methods.

I'm curious to see which methods. But I take it just some methods have
been added, no type of inheritance is going on, right? Ie. If we
extend String, it won't effect Symbol in any way. Or is there some
magic going on in 1.9 here?

T.

Bernard Kenik

9/22/2008 1:13:00 AM

0

Thomas B. wrote:
> Thomas Sawyer wrote:
>> Came up with a great little extension today for the Symbol class. One
>> that also shows off Facets' Functor class:
>>
>> require 'facets/functor'
>>
>> class Symbol
>>
>> # Convert symbol to string, apply string method
>> # and convert back to symbol via a fluent
>> # interface.
>> #
>> # :HELLO.re_s.downcase #=> :hello
>> #
>> def re_s
>> @re_s ||= Functor.new do |op, *a|
>> to_s.send(op, *a).to_sym
>> end
>> end
>>
>> end
>>
>> I picked the name 're_s' off the top of my head. I'm open to better
>> suggestions.
>>
>> T.
>
> Nice idea. After some modification (using method_missing) it could be
> used as a temporary patch on Ruby 1.8, as in Ruby 1.9 symbols are going
> to respond to most string-like methods.
>
> also T.


why not simply use:

irb(main):002:0> :HELLO.to_s.downcase.to_sym
=> :hello

--
Posted via http://www.ruby-....

Stephen Celis

9/22/2008 1:33:00 AM

0

Hi,

On Sep 21, 2008, at 8:12 PM, Bernard Kenik wrote:

> Thomas B. wrote:
>> Thomas Sawyer wrote:
>>> Came up with a great little extension today for the Symbol class.
>>> One
>>> that also shows off Facets' Functor class:
>>>
>>> require 'facets/functor'
>>>
>>> class Symbol
>>>
>>> # Convert symbol to string, apply string method
>>> # and convert back to symbol via a fluent
>>> # interface.
>>> #
>>> # :HELLO.re_s.downcase #=> :hello
>>> #
>>> def re_s
>>> @re_s ||= Functor.new do |op, *a|
>>> to_s.send(op, *a).to_sym
>>> end
>>> end
>>>
>>> end
>>>
>>> I picked the name 're_s' off the top of my head. I'm open to better
>>> suggestions.
>>>
>>> T.
>>
>> Nice idea. After some modification (using method_missing) it could be
>> used as a temporary patch on Ruby 1.8, as in Ruby 1.9 symbols are
>> going
>> to respond to most string-like methods.
>>
>> also T.
>
>
> why not simply use:
>
> irb(main):002:0> :HELLO.to_s.downcase.to_sym
> => :hello

What fun is a new version if you can't even re-factor?

irb(main):001:0> RUBY_VERSION
=> "1.9.0"
irb(main):002:0> :test.capitalize
=> :Test

A 1.8.x patch needn't rely on Facets, however:

class Symbol
def method_missing(*args, &block)
to_s.__send__(*args, &block).to_sym rescue super(*args, &block)
end
end

Stephen


Trans

9/22/2008 1:02:00 PM

0



On Sep 21, 9:33=A0pm, Stephen Celis <stephen.ce...@gmail.com> wrote:

> >> Nice idea. After some modification (using method_missing) it could be
> >> used as a temporary patch on Ruby 1.8, as in Ruby 1.9 symbols are =A0
> >> going
> >> to respond to most string-like methods.
>
> >> also T.
>
> > why not simply use:
>
> > irb(main):002:0> :HELLO.to_s.downcase.to_sym
> > =3D> :hello
>
> What fun is a new version if you can't even re-factor?
>
> =A0 =A0irb(main):001:0> RUBY_VERSION
> =A0 =A0=3D> "1.9.0"
> =A0 =A0irb(main):002:0> :test.capitalize
> =A0 =A0=3D> :Test
>
> A 1.8.x patch needn't rely on Facets, however:
>
> =A0 =A0class Symbol
> =A0 =A0 =A0def method_missing(*args, &block)
> =A0 =A0 =A0 =A0to_s.__send__(*args, &block).to_sym rescue super(*args, &b=
lock)
> =A0 =A0 =A0end
> =A0 =A0end

True, we can do this. And it sure is convenient, but this approach is
generally frowned upon because it can cover-up actual errors. But in
this case, maybe it's not significant? What do others think?

T.

TPReal

9/22/2008 1:09:00 PM

0

Thomas Sawyer wrote:
> On Sep 21, 9:33�pm, Stephen Celis <stephen.ce...@gmail.com> wrote:
>
>> > => :hello
>> � �class Symbol
>> � � �def method_missing(*args, &block)
>> � � � �to_s.__send__(*args, &block).to_sym rescue super(*args, &block)
>> � � �end
>> � �end
>
> True, we can do this. And it sure is convenient, but this approach is
> generally frowned upon because it can cover-up actual errors. But in
> this case, maybe it's not significant? What do others think?
>
> T.

I think that it's a better idea to have a constant table with names of
methods that should be ported from String to Symbol, and compare the
missing method's name against this table.

TPR.
--
Posted via http://www.ruby-....

Stephen Celis

9/22/2008 2:14:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

Hi,

On Mon, Sep 22, 2008 at 8:02 AM, Trans <transfire@gmail.com> wrote:
>
>
> True, we can do this. And it sure is convenient, but this approach is
> generally frowned upon because it can cover-up actual errors. But in
> this case, maybe it's not significant? What do others think?


Some frown upon monkey-patching, some frown upon blind rescues, but the fact
that we have them at our side is nice.

In this case, everything will be re-raised by the symbol if
String#instance_method doesn't return something that can be sent "to_sym".

However, I agree with TPR, and that as far as maintainable code goes, things
should be manifest.

Stephen

Trans

9/22/2008 2:54:00 PM

0



On Sep 22, 9:09=A0am, "Thomas B." <tpr...@gmail.com> wrote:

> I think that it's a better idea to have a constant table with names of
> methods that should be ported from String to Symbol, and compare the
> missing method's name against this table.

That limits the utility too much, because then one can't extend String
and have Symbol just work too.

I like the method_missing idea, but I fear it is slightly too
dangerous to make a standard behavior. Unless someone proves my fears
to be unfounded, then using something like #re_s is the safe bet.

T.

Liberal $500 million tax dollar disaster

3/26/2010 9:51:00 PM

0

CB wrote

>
> Islam is second-most practiced religion in the Republic of India after
> Hinduism, with more than 13.4% of the country's population
> http://en.wikipedia.org/wiki/Isla...
>
Wow! That's a convincing number. Just over 1 in 10! And how about their
situation in India?

It makes the deep south prior to the civil rights movement look like a cake
walk. The most read newspaper in the world is the Times of India, and most
of the readers (unlike Muslims) don't know Jesus Christ from a good curry.

The country has nothing to do with any of the 3 Abrhammic faiths. Islam,
Christianity and Judasim. Who can blame them?

They should ship the jobs to Chechnya, at least the Muslims there are white
like you; or maybe Indonesia, where they're Oriental. Most of you bigots
think that race is what it's all about.