[#23657] [Bug #1550] String#lstrip! raises RuntimeError on Frozen String Despite Making No Changes — Run Paint Run Run <redmine@...>

Bug #1550: String#lstrip! raises RuntimeError on Frozen String Despite Making No Changes

13 messages 2009/06/01

[#23729] [Bug #1583] Time + String no Longer Raises TypeError? — Run Paint Run Run <redmine@...>

Bug #1583: Time + String no Longer Raises TypeError?

14 messages 2009/06/05

[#23770] [Bug #1595] rake unusable on windows install — Robert Gonzalez <redmine@...>

Bug #1595: rake unusable on windows install

10 messages 2009/06/09

[#23869] [Bug #1640] [PATCH] Documentation for the Rational Class — Run Paint Run Run <redmine@...>

Bug #1640: [PATCH] Documentation for the Rational Class

12 messages 2009/06/16

[#23903] [Bug #1648] Rational#div Raises NoMethodError for Invalid Argument — Run Paint Run Run <redmine@...>

Bug #1648: Rational#div Raises NoMethodError for Invalid Argument

9 messages 2009/06/17

[#23977] [ANN] meeting log of RubyDeveloperKaigi20090622 — "Yugui (Yuki Sonoda)" <yugui@...>

Hi,

41 messages 2009/06/23
[#23979] Re: [ANN] meeting log of RubyDeveloperKaigi20090622 — Run Paint Run Run <runrun@...> 2009/06/23

Thanks for the update. :-)

[#24173] Re: [ANN] meeting log of RubyDeveloperKaigi20090622 — "NARUSE, Yui" <naruse@...> 2009/07/07

Sorry for late response,

[#24174] Re: [ANN] meeting log of RubyDeveloperKaigi20090622 — Luis Lavena <luislavena@...> 2009/07/07

On Tue, Jul 7, 2009 at 12:12 AM, NARUSE, Yui<naruse@airemix.jp> wrote:

[#24242] Re: [ANN] meeting log of RubyDeveloperKaigi20090622 — Charles Oliver Nutter <headius@...> 2009/07/09

On Mon, Jul 6, 2009 at 10:18 PM, Luis Lavena<luislavena@gmail.com> wrote:

[#24010] [Bug #1685] Some windows unicode path issues remain — B Kelly <redmine@...>

Bug #1685: Some windows unicode path issues remain

26 messages 2009/06/24
[#29189] [Bug #1685] Some windows unicode path issues remain — Yuki Sonoda <redmine@...> 2010/04/01

Issue #1685 has been updated by Yuki Sonoda.

[#29200] Re: [Bug #1685] Some windows unicode path issues remain — Bill Kelly <billk@...> 2010/04/01

Yuki Sonoda wrote:

[#29892] Re: [Bug #1685] Some windows unicode path issues remain — Bill Kelly <billk@...> 2010/04/29

Hi,

[#24058] [Bug #1696] http downloads are unuseably slow — Steven Hartland <redmine@...>

Bug #1696: http downloads are unuseably slow

19 messages 2009/06/27

[#24063] [Feature #1697] Object#<=> — Marc-Andre Lafortune <redmine@...>

Feature #1697: Object#<=>

15 messages 2009/06/28

[ruby-core:24005] Object#to: Possible Refinements on the .try_convert Methods for Type Coercion

From: Run Paint Run Run <runrun@...>
Date: 2009-06-24 02:57:27 UTC
List: ruby-core #24005
Musing on type conversion, I hit on the idea of an Object#to method
which took a String, Symbol, or class name and tried to coerce the receiver
into that type, returning nil on error. I initially wrote the
following:

 class Object
    COERCABLE = {String   => 's',    Float    => 'f',   Integer    => 'i',
                 Complex  => 'c',    Rational => 'r',   Array      => 'a',
                 Proc     => 'proc', Symbol   => 'sym', Enumerator => 'enum'}

    def to(arg)
      name = arg.is_a?(Class) ? COERCABLE[arg] : arg.to_s.sub(/^to_/,'')
      method = ('to_' + name.to_s).to_sym
      return unless respond_to? method
      coerced = send method
      return unless coerced.is_a?(COERCABLE.invert[name] || Object)
      coerced
    end
  end

  20.to(Complex)      #=> (20+0i)
  20.to(:c)                #=> (20+0i)
  20.to(Enumerable) #=> nil

Having done this, I discovered the .try_convert methods that some core
classes had (the name implied to me that they were for character-set
conversion, so I'd never looked at them). I'm posting anyway because
this approach may offer some advantages.

(I used the "explicit conversion" form of the #to_* methods, e.g.
#to_s rather than #to_str, because if somebody calls #to(String),
that's as explicit a conversion you can get. .try_convert mainly uses
the implicit form, but an `ack` of `1.9 source shows inconsistencies).

It's an instance method, because it feels more natural to me to say
obj.to(Foo) than Foo.try_convert(obj). It also means that it's subject
to inheritance, so objects that want to redefine the semantics of
their type conversion can override #to.

It puts the _object_ at the heart of the operation; not the target of
the conversion. Philosophical arguments aside, this would also allow
you to specify multiple conversion targets in a single #to_call, and
have them tried in order of preference.

It's also closer to the #to_* idiom we're already used to. Indeed,
with a symbol argument #to(:a) is only two characters longer than
#to_a, yet performs type-checking and avoids NoMethodErrors.

It doesn't raise exceptions, so it can be chained safely. .try_convert
can raise a TypeError. It doesn't matter to me whether a coercion
failed because the object didn't implement the right method, or said
method returned the wrong thing. .try_convert forces you to care,
which seems self defeating because it's principle advantage was
avoiding NoMethodErrors, thus rescue clauses, but by raising
TypeErrors, rescue must come to your rescue once again.

The flexibility of accepting a symbol name argument allows you to
specify that you want to use, say, #to_ary rather than #to_a to
perform the conversion. It also makes the process extensible for more
types.

It's more supportive of #to_* methods that don't imply a resultant
type. For example. #to_path returns a String, but you wouldn't use
String.try_convert(path). IO.try_convert(path) could try #to_path
after #to_io, but that's a kludge because now you have to think in
terms of IO objects, and rely on a side effect, just to get your
conversion.

.try_convert requires each "target" class to implement a .try_convert
method, which seems most ungainly. My approach of using a constant is
perhaps worse, of course, but it still seems preferable because the
constant could simply be used to short-circuit common cases, letting
us use introspection to dispatch .try_convert/#to calls to unknown
classes. Again, this is only possible because the source of the
conversion is to the receiver. (A possible extension of this approach
is to also allow classes to define a .from method, that would accept
an object to coerce from as an argument, and raise a TypeError if it
can't. IOW, an alternative constructor that could be called by #to,
giving classes more control over how foreign objects were coerced).

Anyway, just an idea. :-)

--
Run Paint Run Run

In This Thread

Prev Next