[REBOL] Re: How to remove words from objects?
From: carl:cybercraft at: 24-Jan-2004 11:49
On 13-Jan-04, Luke wrote:
> Dear list
> I'm trying to dynamically add and remove words from an
> object. So far I can do the add words as follows:
>>> obj: make object! [a: 1 b: 2]
>>> probe obj
> make object! [
> a: 1
> b: 2
> ]
>>> obj: make obj [c: 3]
>>> probe obj
> make object! [
> a: 1
> b: 2
> c: 3
> ]
>>>
> now I want to remove c from obj. How do I do that. I
> know I could dynamically reconstruct it from scratch, but
> that is not very elegant. There must be a better way to
> get back to obj being:
> make object! [a: 1 b: 2]
> Any ideas?
The third value in an object is its block. ie...
>> obj: make object! [a: 1 b: 2 c: 3]
>> third obj
== [a: 1 b: 2 c: 3]
So, you could make an object from a copy of that block with the values
you don't want removed. The following for instance creates a new
object from the above one with the last two values removed...
>> obj2: make object! head clear back back tail copy third obj
>> probe obj
make object! [
a: 1
b: 2
c: 3
]
>> probe obj2
make object! [
a: 1
b: 2
]
Not sure how it'd work for more complex objects though.
--
Carl Read