Symbol#re_s

T

Trans

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.
 
T

Thomas B.

Thomas said:
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.
 
T

Trans

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.
 
B

Bernard Kenik

Thomas said:
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
 
S

Stephen Celis

Hi,

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
 
T

Trans

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.
 
T

Thomas B.

Thomas said:
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.
 
S

Stephen Celis

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

Hi,

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
 
T

Trans

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top