[#1110] alias — ts <decoux@...>
6 messages
2003/06/01
[#1122] Exception::new — ts <decoux@...>
6 messages
2003/06/05
[#1140] Rubicon questions — Johan Holmberg <holmberg@...>
6 messages
2003/06/14
[#1147] Copying RVALUE — why the lucky stiff <ruby-core@...>
Hello, everyone. Hope you are all doing well.
18 messages
2003/06/17
[#1155] Re: Copying RVALUE
— matz@... (Yukihiro Matsumoto)
2003/06/20
Hi,
[#1157] Re: Copying RVALUE
— why the lucky stiff <ruby-core@...>
2003/06/20
Yukihiro Matsumoto (matz@ruby-lang.org) wrote:
[#1160] Re: Copying RVALUE
— Tanaka Akira <akr@...17n.org>
2003/06/20
In article <20030620153706.GA65136@rysa.inetz.com>,
[#1161] Re: Copying RVALUE
— why the lucky stiff <ruby-core@...>
2003/06/20
Tanaka Akira (akr@m17n.org) wrote:
[#1162] Re: Copying RVALUE
— Ryan Davis <ryand-ruby@...>
2003/06/20
[#1163] Re: Copying RVALUE
— Mathieu Bouchard <matju@...>
2003/06/21
[#1148] 'unexpected break' when captured block calls break — george.marrows@...
Proc-closures capture any block passed to their enclosing scope: the script
4 messages
2003/06/18
[#1173] class.c code cleanup (rb_class_*_instance_methods) — Matthew Dempsky <jivera@...>
Hi, I'm new to this mailing list so I don't know the procedure for
15 messages
2003/06/22
[#1174] Re: [Patch] class.c code cleanup (rb_class_*_instance_methods)
— nobu.nokada@...
2003/06/22
Hi,
[#1175] Re: [Patch] class.c code cleanup (rb_class_*_instance_methods)
— Matthew Dempsky <jivera@...>
2003/06/22
On Sun, 2003-06-22 at 05:36, nobu.nokada@softhome.net wrote:
[#1176] Re: [Patch] class.c code cleanup (rb_class_*_instance_methods)
— nobu.nokada@...
2003/06/22
Hi,
[#1193] Re: [Patch] class.c code cleanup (rb_class_*_instance_methods)
— Matthew Dempsky <jivera@...>
2003/06/25
On Sun, 2003-06-22 at 07:41, nobu.nokada@softhome.net wrote:
[#1194] Re: [Patch] class.c code cleanup (rb_class_*_instance_methods)
— nobu.nokada@...
2003/06/25
Hi,
[#1196] Re: [Patch] class.c code cleanup (rb_class_*_instance_methods)
— Matthew Dempsky <jivera@...>
2003/06/25
On Wed, 2003-06-25 at 02:01, nobu.nokada@softhome.net wrote:
[#1197] Re: [Patch] class.c code cleanup (rb_class_*_instance_methods)
— matz@... (Yukihiro Matsumoto)
2003/06/25
Hi,
[#1198] Re: [Patch] class.c code cleanup (rb_class_*_instance_methods)
— nobu.nokada@...
2003/06/25
Hi,
[#1177] Re: In 1.8.0 nil.to_s is not the same as "" — ts <decoux@...>
14 messages
2003/06/22
[#1178] Re: In 1.8.0 nil.to_s is not the same as ""
— nobu.nokada@...
2003/06/22
Hi,
[#1179] Re: In 1.8.0 nil.to_s is not the same as ""
— ts <decoux@...>
2003/06/22
>>>>> "n" == nobu nokada <nobu.nokada@softhome.net> writes:
Standard type conversion mechanism
From:
Ryan Pavlik <rpav@...>
Date:
2003-06-23 19:45:05 UTC
List:
ruby-core #1191
OK, I'm posting this because a few people on #ruby-lang thought it
should be posted, so here it is.
A number of us desire a more standard, more scalable type conversion
mechanism. By "type conversion", I mean you've got one thing (say, an
Integer), and you want it to be another (say, a String).
Currently we do this:
x = 42
s = x.to_s
The problem is this is both a loose convention and it doesn't scale well
at all. A case-in-point of the former:
x = nil
s = ""
s << x
=> TypeError: cannot convert nil into String
Yet, there's a NilClass#to_s... but this looks for NilClass#to_str.
There's not even internal consistency. This leads us to the second
point; it doesn't scale. In order to provide a conversion from each
type to another, it requires (type^2) methods in each class. (Granted,
you're not likely to have a conversion from every type to every other,
but this is a worst-case.)
There isn't a good way of naming these methods, either. If I have
A::B::C and want it as an A::B::D, should I have A::B::C#to_a_b_d?
Should I start adding methods to classes that have little relation to
other classes, just for quick conversions? This breaks a form of
containership, and requires one class know about another one, even
completely unrelated.
To summarize, here are the cons of the current system:
* No standardization. (#to_i, #to_a, #to_s, #to_str, what about
other classes?)
* Poor scaling. (Lots of #to_* methods)
* Inelegant. (Requires one class "know" about another class)
OK, enough complaining. I have a working solution: a ConversionTable.
Currently, you can find the implementation here:
http://mephle.org/conversion.rb
Here's an example:
require 'conversion'
ConversionTable.add(Integer, String) { |i| i.to_s }
ConversionTable.add(Integer, Boolean) { |i| i != 0 }
ConversionTable.add(String, Integer) { |s| s.to_i }
x = 42
p x.as(String)
p x.as(Boolean)
s = "42"
p s.as(Integer)
p s.can_convert?(Integer)
p s.can_convert?(Array)
This provides the following advantages:
* Standard. There's no ambiguity about which method to call;
things are based entirely on the class/module itself.
* Scalable. Adding conversions adds them in one place; no "method
pollution".
* Elegant. Conversion "glue" is outside the class itself,
requiring neither class to know about the others.
* Easy to add new conversions.
* Scalable part 2. Theoretically, "conversion inferencing" can be
done. That is, given A => B, and B => C, you can ask for A => C
and it'll figure out how. (Note the current implementation does
not handle this.)
* Conversions can be queried.
* Fits over existing methods and doesn't break backward
compatibility.
I have an interest in developing some further dimensions to this, as I'm
contemplating semantics/unit and context-sensitive programming in Ruby.
(So you can say, "this is an integer in bytes, give me a string
representation in kilobytes" and it does the magic for you.) I don't
have any immediate plans to formalize this as a module (since it lacks
the further functionality I'd like, and I don't have the time to finish
it), but I know a number of people who are pressing to have (at least)
the basic conversion mechanism as a more built-in standard.
Thoughts, comments, etc, encouraged.
--
Ryan Pavlik <rpav@users.sf.net>
"Another *perfectly* calculated space-time
splice-n-splice. Now to get back to... wait a second.
I forgot to carry the TWO!" - 8BT