No, that's not what I meant.
Firstly, eval is more dangerous than is wanted. For example, "[3, 4]" can
deserialise as [3, 4], but "3 + 4" should give an error as it would never be
the output of foo.inspect
Secondly, it should also handle regular objects with instance variables. For
example,
irb(main):001:0> class Foo; attr_accessor :a, :b, :c; end
=> nil
irb(main):002:0> obj = Foo.new; obj.a="one"; obj.b="two"; obj.c="three"; obj.inspect
=> "#<Foo:0xb7cd6f70 @c=\"three\", @b=\"two\", @a=\"one\">"
That last string would allow an object of class Foo to be reconstructed
(obviously with a different object_id)
Clearly it won't work for graph-like constructs:
irb(main):001:0> a = nil
=> nil
irb(main):002:0> a = [1, 2]
=> [1, 2]
irb(main):003:0> b = [a, a]
=> [[1, 2], [1, 2]]
irb(main):004:0> a << b
=> [1, 2, [[...], [...]]]
irb(main):005:0> b.inspect
=> "[[1, 2, [...]], [1, 2, [...]]]"
irb(main):006:0>
YAML does all that of course, but it's not inspect.
I just think that using 'inspect' would allow basic objects (with simple
nesting) to be stuffed into a database column in a simple and obvious way,
if there were a safe way to reconstruct them from it.
nil
123
"123\n"
[123, "456"]
{"foo"=>["bar","baz"]}
#<Foo: @var="val">
Regards,
Brian.
···
On Thu, Apr 05, 2007 at 02:42:04AM +0900, Alex Young wrote:
Brian Candler wrote:
>On Thu, Apr 05, 2007 at 01:05:44AM +0900, Dustin Anderson wrote:
>>is there a simple way in ruby to build an array from a string like this:
>>
>>block1,block2,block3 to an array that is like this:
>>
>>["block1", "block2", "block3"]
>
>I always thought that Object#inspect could be considered as a (crude) form
>of object serialisation, and it would be really useful to have a reverse
>"uninspect" operation to recreate the object from it again. Has anyone
>written such a parser?
>
For the basic types, eval will do it to a tolerable degree.