Format no interpreted code

Hi,
I am trying to format some code in order to write as it is in a file.
File.use("~/info".standardizePath, "a", { |f| f.write(format("-> %\n", (1..200))); });
But as expected, the code (1..200) is interpreted and wrote as an array finishing with …etc… for the long ones. And what I want is to write the code itself, i.e. (1..200).

Hi,
This works:
{ |f| f.write(format("-> %\n", (1..200).asCompileString)); }
Docs say: .asCompileString: “Returns a String formatted for compiling.”
So… I consider it as: “prints complete array” :slight_smile:

… but I would like literally (1..200) in my file.

{ |f| f.write("-> (1..200)\n")}
Or do you want to have the (1…200) variable?
Then this may work:
(var array = (1..20); File.use("path", "a", { |f| f.write("->(" ++ array[0] ++ ".." ++ (array.size+array[0]-1) ++ ") \n")});)

Edit: although I see I’m skipping the format function now, so this might not relate to your original question anymore…

Yes, but probably this is a pitfall to write code while it is interpreted as a variable. The idea was to write any piece of code used as a parameter to some function and keep track in a file. (1..20) was an example and it can be something like Array.geom(20, 1, 2) or any kind of function generating arrays.
At least thanks for the method .asCompileString which can do the job as interpreted code, it is just a bit heavy to read for long arrays.

Ah, yes. I got it.
Hopefully someone else knows how to do it, I’m curious

you could wrap the argument in a function and take the compile string from that to avoid the expression from being evaluated first. see the difference between these two:

{ arg a; a.cs; }.value(Array.geom(20, 1, 2))
{ arg a; a.cs; }.value({ Array.geom(20, 1, 2) })
1 Like

Great! It was as simple as that.
(var val = {(1..20)}; File.use("~/info".standardizePath, "a", { |f| f.write(format("-> %\n", val.cs)); });)