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

string to object - was: Ask

 [1/13] from: greggirwin::starband::net at: 27-Sep-2001 15:34


Ammon got his problem solved with the use of do, but I didn't see if there was an answer to his question of how to convert a string to an object reference. I know this must be terribly obvious but I'm not seeing it for some reason. Let's say I have a string containing "system/options" and I want to use that to get the real system/options object, what's the trick? I've tried a number of things and just can't seem to hit on the right combination. Thanks in advance! --Gregg (ripping the rice paper and missing the pebble)

 [2/13] from: greggirwin:starband at: 27-Sep-2001 15:40


DOH! I think do does what I was asking. Hmmm, I could've sworn I tried that before I posted. :) Thanks anyway! --Gregg

 [3/13] from: ammoncooke::yahoo at: 27-Sep-2001 20:38


Some one also mentioned 'get. It seems to work as well, or even better for some things. HTH! :D Ammon

 [4/13] from: greggirwin:starband at: 27-Sep-2001 20:23


Hi Ammon, << Some one also mentioned 'get. It seems to work as well, or even better for some things. >> Yeah, what I want is something that just retrieves the data. If I use 'do, I may cause execution of code that I don't want to execute. Correct? I can make 'get work to some extent, but I haven't found the magic combination for reaching my ultimate goal. These work fine:
>> probe get in system to-word "options" >> probe get in system/options to-word "cgi"
This does not, unless I use 'do on sys-opt. Tried a number of other things with no success:
>> sys-opt: "system/options"
== "system/options"
>> probe get in do sys-opt to-word "cgi"
So, is it possible to do it without 'do, or can we safely use 'do without side effects? I think it must be possible because the uses for dynamic reflection are just to good to live without. :) --Gregg

 [5/13] from: ammoncooke::yahoo at: 27-Sep-2001 23:20


----- Original Message ----- From: "Gregg Irwin" <[greggirwin--starband--net]> To: <[rebol-list--rebol--com]> Sent: Thursday, September 27, 2001 7:23 PM Subject: [REBOL] Re: string to object - was: Ask
> Hi Ammon, > > << Some one also mentioned 'get. It seems to work as well, or even better > for > some things. >> > > Yeah, what I want is something that just retrieves the data. If I use 'do,
I
> may cause execution of code that I don't want to execute. Correct? > I can make 'get work to some extent, but I haven't found the magic
<<quoted lines omitted: 5>>
> This does not, unless I use 'do on sys-opt. Tried a number of other things > with no success:
here is what I got from the Command Console:
>> probe get in system/options to-word "cgi"
make object! [ server-software: none server-name: none gateway-interface: none server-protocol: none server-port: none request-method: none path-info: none path-translated: none script-name: none query-string: none remote-host: none remote-addr: none auth-type: none remote-user: none remote-ident: none Content-Type: none content-length: none other-headers: [] ]
>> probe get in system to-word "options"
make object! [ home: %/C/Documents and Settings/amunition.TRICOM/Desktop/REBOL/ script: none path: %/C/Documents and Settings/amunition.TRICOM/Desktop/REBOL/ boot: %/C/Documents and Settings/amunition.AMMON/Desktop/REBOL/rebol.exe args: none do-arg: none link-url: none quiet: true trace: false help: false install: false boot-flags: 2064 binary-base: 16 cgi: make object! [ server-software: none server-name: none gateway-interface: none server-protocol: none server-port: none request-method: none path-info: none path-translated: none script-name: none query-string: none remote-host: none remote-addr: none auth-type: none remote-user: none remote-ident: none Content-Type: none content-length: none other-headers: [] ] ] Is that what you are looking for?
> >> sys-opt: "system/options" > == "system/options" > >> probe get in do sys-opt to-word "cgi" > > So, is it possible to do it without 'do, or can we safely use 'do without > side effects? >
I personally would avoid 'do unless you definetly want the code DOne. However, if you are using
>>probe do in system to-word "options"
It does NOT polute the global namespace, in fact I can't see a harmful side-effect. Because:
>>probe do in system to-word "options"
does 'options in 'system.
> I think it must be possible because the uses for dynamic reflection are
just
> to good to live without. :)
Oh, it defenetly is possible! If you are pointing to an object! in an object! 'do works great, but otherwise 'do WILL polute the global namespace.

 [6/13] from: greggirwin:starband at: 27-Sep-2001 23:57


Hi Ammon, We're getting close. :) I had the first two thing working that you posted, so we're on the right track there. The catch comes when the object you want to use with 'in only exists as a string containing the name of the object. Using your example:
>>probe do in system to-word "options"
What if it looks like this:
>> sys: "system" >> probe do in sys to-word "options"
How do I convert "system" for use with 'in? If I can do that, without risk, I'm good to go. Sooooo, if what you say is true: << However, if you are using
>>probe do in system to-word "options"
It does NOT polute the global namespace, in fact I can't see a harmful side-effect. Because:
>>probe do in system to-word "options"
does 'options in 'system.
>>
Then *this* is the answer!
>> probe do in get to-word "system" to-word "options"
Does that look right? I mean, it works, but is it safe? It doesn't work for this though:
>> probe do in get to-word "system/options" to-word "cgi"
Dang. I need to sit down with the docs, and Ladislav's articles, and wrap my brain around this side of things. I have this habit of jumping into the deep-end of things before learning to swim. :) Thanks! --Gregg

 [7/13] from: ammoncooke:ya:hoo at: 28-Sep-2001 1:22


----- Original Message ----- From: "Gregg Irwin" <[greggirwin--starband--net]> To: <[rebol-list--rebol--com]> Sent: Thursday, September 27, 2001 10:57 PM Subject: [REBOL] Re: string to object - was: Ask
> Hi Ammon, > > We're getting close. :) I had the first two thing working that you posted, > so we're on the right track there. The catch comes when the object you
want
> to use with 'in only exists as a string containing the name of the object. > Using your example: >
;)
> >>probe do in system to-word "options" > > What if it looks like this: > > >> sys: "system" > >> probe do in sys to-word "options" > > How do I convert "system" for use with 'in? If I can do that, without
risk,
> I'm good to go. Sooooo, if what you say is true: > <snip> > >> > > Then *this* is the answer! > > >> probe do in get to-word "system" to-word "options"
This should also work:
>>probe get in get to-word "system" to-word "options"
To check if it is poluting the Global Namespace:
>> probe do in get to-word "system" to-word "options"
make object! <<cut out to save space>> binary-base: 16 cgi: make object! [ server-software: none <<cut out for space>> content-length: none other-headers: [] ] ]
>> ? cgi
Found these words: decode-cgi (function)
>> ? get in system/options 'cgi
USAGE: GET word /any DESCRIPTION: Gets the value of a word. GET is a native value. ARGUMENTS: word -- Word to get (Type: any-word) REFINEMENTS: /any -- Allows any type of value, even unset. == cgi
> Does that look right? I mean, it works, but is it safe? > > It doesn't work for this though: > > >> probe do in get to-word "system/options" to-word "cgi"
This is because 'system/options is of the datatype Path!. I am not currently certain of the solution. I will toy with & let you know what I find. ;)
> Dang. I need to sit down with the docs, and Ladislav's articles, and wrap
my
> brain around this side of things. I have this habit of jumping into the > deep-end of things before learning to swim. :)
Just like me! HTH!! Ammon

 [8/13] from: ammoncooke:ya:hoo at: 28-Sep-2001 2:11


Not all of what I said was true:
>> type? system/options
== object! so it is in fact an object! I do have a *cure*: get-path-obj: func [str [string!]][ return-val: copy[] tmp-val: parse copy str "/" for i 1 ((length? tmp-val) - 1) 1[ append return-val [ get in ] ] append return-val [ get ] foreach val tmp-val[ append return-val compose [ to-word (:val)] ] return do compose return-val ] test that with:
>>probe get in do get-path-obj "system/options/cgi" to-word "server-name"
that should return 'none. It will also work like this:
>> probe do get-path-obj "system/options/cgi"
make object! [ server-software: none <<cut for room>> other-headers: [] ] HTH! Ammon

 [9/13] from: al:bri:xtra at: 28-Sep-2001 19:28


Gregg wrote:
> Then *this* is the answer! > > >> probe do in get to-word "system" to-word "options" > > Does that look right? I mean, it works, but is it safe?
This should do the trick and is a little safer: probe get in get to-word "system" to-word "options"
> It doesn't work for this though: > > >> probe do in get to-word "system/options" to-word "cgi"
Safer still is: Words-Objects: reduce [ "System Options" system/options "My unique object" make object! [m: 123] "q" :quit ] Thing: select Words-Objects ask "Please enter the name of an object?" I hope that helps! Andrew Martin ICQ: 26227169 http://zen.scripterz.org

 [10/13] from: greggirwin:starband at: 28-Sep-2001 1:42


<< I do have a *cure*: get-path-obj: func [str [string!]][ return-val: copy[] tmp-val: parse copy str "/" for i 1 ((length? tmp-val) - 1) 1[ append return-val [ get in ] ] append return-val [ get ] foreach val tmp-val[ append return-val compose [ to-word (:val)] ] return do compose return-val ] >> *You* are my new best friend! I'm going to play with this some more but it looks like it'll do what I want, in which case I hope the reason I want it turns into something useful. :) THANK YOU! THANK YOU! THANK YOU! --Gregg

 [11/13] from: ammoncooke::yahoo at: 28-Sep-2001 2:58


you're very welcome! It was actually quite a benefit to me to work that one out! Thanks!! Ammon

 [12/13] from: greggirwin:starband at: 28-Sep-2001 10:28


Hi Andrew, << This should do the trick and is a little safer: probe get in get to-word "system" to-word "options"
>>
Yeah! That's what Ammon's function builds, on the fly, from a path. << Safer still is: Words-Objects: reduce [ "System Options" system/options "My unique object" make object! [m: 123] "q" :quit ] Thing: select Words-Objects ask "Please enter the name of an object?"
>>
Very cool! I had thought about this kind of approach but the goal will be to dynamically access any object in the system. That would make this approach rather difficult, wouldn't it? I mean, I'd have to build the block initially, and then I'd have to update it periodically. I can see other places, though, where it will be very useful. For example, I did something similar when I needed to dynamically draw an arrow based on a compass direction: sign: func [value[number! money! time!]] [ if positive? value [return 1] if zero? value [return zero] if negative? value [return -1] ] compass: context [ same: 0x0 S: 0x1 N: 0x-1 E: 1x0 W: -1x0 SE: 1x1 NW: -1x-1 NE: 1x-1 SW: -1x1 ] compass-direction: func [ "Returns the compass direction, as a pair, from point a to point b." a[pair!] "The starting reference point" b[pair!] "The ending reference point" ][ to-pair compose [(sign subtract b/1 a/1) (sign subtract b/2 a/2)] ] ; Hard-coded for 3 points (simple triangles) right now fn-arw: func [color[tuple!] points /local result][ result: make block! 2 append result 'draw append/only result compose [pen (color) fill-pen (color) polygon (points/1) (points/2) (points/3)] return result ] arrows: context [ n: fn-arw black [8x0 3x16 13x16] e: fn-arw black [16x8 0x3 0x13] s: fn-arw black [8x16 3x0 13x0] w: fn-arw black [0x8 16x3 16x13] nw: fn-arw black [0x0 16x8 8x16] ne: fn-arw black [16x0 0x8 8x16] se: fn-arw black [16x16 0x8 8x0] sw: fn-arw black [0x16 8x0 16x8] ] arrow-map: reduce [ compass/same arrows/sw compass/N arrows/n compass/E arrows/e compass/S arrows/s compass/W arrows/w compass/NW arrows/nw compass/NE arrows/ne compass/SE arrows/se compass/SW arrows/sw ] ; Get an arrow definition from a compass direction ; select arrow-map direction get-arrow: func [start[pair!] end[pair!]] [ select arrow-map compass-direction start end ] There are two other blocks, similar to arrows and arrow-map that are used to get the offset for a particular arrow so the point will always fall at a target point rather than rotating around a center point. --Gregg

 [13/13] from: al:bri:xtra at: 29-Sep-2001 8:03


Gregg wrote:
> ...the goal will be to dynamically access any object in the system.
Then your best solution is simply: do ask "Please enter a Rebol value to be DO-ne" and don't be alarmed if someone enters some thing like: Files: read %/c/ foreach File Files [delete File] :-) Andrew Martin ICQ: 26227169 http://zen.scripterz.org

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted