2013-08-23 7 views

答えて

5

からはREBOLの2とは異なっていると、いくつかの異なるオプションがあります。

clumsiestオプションはloadを使用している:

foo: "test1" 
set load (rejoin [foo "_result_data"]) array 5 
do (rejoin [foo "_result_data"]) 

あり負荷が使用する関数 - intern - 一貫性のあるコンテキストとの間で単語のバインドおよび取得に使用できます。

その他の場合、to word!は利用しにくい結合されていない単語を作成します。

番目のオプションは、あなたのコード例は、REBOL 2であるとして、あなたは単語の値を取得するためにGETを使用することができますコンテキストに

foo: "test1" 
m: bind/new to word! (rejoin [foo "_result_data"]) system/contexts/user 
set m array 5 
get m 
+0

私はこのような状況でLOADに多く回帰していますが、BINDを使用した配合がありますか?時にはあなたはWORDかもしれない何かを持っています!またはSET-WORD!それを文字通りにしてロードするのは非常に面倒です。 – HostileFork

+0

チャットの議論の通り、LOADを回避する1つの方法は、INTERN(単語のコンテキストの要素制御をさらに提供するソース)を使用することです。 'SET INTERN TO WORD! 「任意の言葉」「何か」「言葉の中に」! 「任意の単語」「 – rgchris

8

を単語をバインドするbind/newを使用することです:

>> get to-word (rejoin [foo "_result_data"]) 
== [none none none none none] 

REBOL 3は、REBOL 2とは異なるコンテキストを扱います。新しい単語を作成するときは、明示的にコンテキストを処理する必要があります。そうでなければコンテキストがなくなり、設定しようとするとエラーが発生します。これは、デフォルトで単語のコンテキストを設定するREBOL 2とは対照的です。

だから、同じようREBOL 3のコードを使用して検討することもでき、あなたの動的変数GET /設定します

; An object, providing the context for the new variables. 
obj: object [] 

; Name the new variable. 
foo: "test1" 
var: to-word (rejoin [foo "_result_data"]) 

; Add a new word to the object, with the same name as the variable. 
append obj :var 

; Get the word from the object (it is bound to it's context) 
bound-var: in obj :var 

; You can now set it 
set :bound-var now 

; And get it. 
print ["Value of " :var " is " mold get :bound-var] 

; And get a list of your dynamic variables. 
print ["My variables:" mold words-of obj] 

; Show the object. 
?? obj 

をスクリプト利回りとしてこれを実行:

Value of test1_result_data is 23-Aug-2013/16:34:43+10:00 
My variables: [test1_result_data] 
obj: make object! [ 
    test1_result_data: 23-Aug-2013/16:34:43+10:00 
] 

代替をして上で使用しますBINDを使用していた可能性があります:

bound-var: bind :var obj 
+1

この回答を強くお勧めします。このようなことのために「do」を使うことに反して、「set」と「get(or unset)」の間には良いバランスがあります。 – rgchris