Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Dynamically Modifying objects!

From: edanaii:cox at: 21-Feb-2003 16:03

All, About a month ago, I asked if it were possible to dynamically modify an object. I got various answers, all good, btw, but all indicating that it is not a built-in part of the language. I decided to revisit the problem and here is my solution. ====================================================================== REBOL [ File: %Malleable.r Date: 20-Feb-2003/15:44:54-7:00 Title: "Make Malleable Object" Name: "Malleable Object" Version: 1 Author: "Ed Dana" Usage: {* Create - Test: Make Malleable! [ Name: "Will Robinson" ] * Add - Test: Test/_Add [ Status: "Lost in Space" ] * Remove - Test: Test/_Remove [ 'Status ] * Display- Test/_Data } Purpose: "Allows data do be added to an object dynamically." Type: 'Misc ] Malleable!: make object! [ _Add: func [Data [block!]][ Make Self Data ] _Class: 'Malleable _Data: func [/Local Blk Str][ Blk: Make Block! 0 ForEach Item Next First Self [ Str: To-String Item If Str/1 <> #"_" [ Append Blk Item ] ] ] _Remove: func [Minus [word! block!] /Local Str][ If Type? Minus = 'Word [Minus: To-Block Minus] Str: Make String! 0 ForEach Item Self/_Data [ If Not Find Minus Item [ Str: Rejoin [Str " " Item ": " Mold Get In Self Item] ] ] Make Malleable! To-Block Str ] ] ====================================================================== It works as follows: --> Create <--
>> Contact: Make Malleable! [ Name: "Fred Flintstone" ] >> Contact/_Data
== [Name]
>> Contact/Name
== "Fred Flintstone" --> Add <--
>> Contact: Contact/_Add [ Address: "32 E. Deer Trail" Status:
Hanna-Barbaric Phone: "Shout!" ]
>> Contact/_Data
== [Name Address Status Phone]
>> Contact/Name
== "Fred Flintstone"
>> Contact/Address
== "32 E. Deer Trail"
>> Contact/Status
== "Hanna-Barbaric"
>> Contact/Phone
== "Shout!" --> Remove <--
>> Contact: Contact/_Remove 'Status >> Contact/_Data
== [Name Address Phone]
>> Contact/Status
** Script Error: Invalid path value: Status ** Near: Contact/Status Feel free to question, comment, criticize or even offer tips. :) -- Sincerely, | We're Human Beings, with the blood of a million Ed Dana | savage years on our hands! But we can stop it! We Software Developer | can admit we're killers, but we're not going to 1Ghz Athlon Amiga | kill today. That's all it takes! Knowing that we're | not going to kill... Today! -- Shatner, Star Trek.