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.
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.
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?
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.
=> :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?
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.
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.
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.