<nowhereFast>
assuming a class that includes JSON::Serializable which has a property defined that is of type JSON::Any, when running a to_json on an instance if this class, should this property be affected?
<FromGitter>
<naqvis> `@type.class.methods` what will that return?
<FromGitter>
<naqvis> what do you mean by "property be affected?" ?
<nowhereFast>
rephrasing the question, should calling to_json on a type of JSON::Any add any escaping?
<Evtimiy>
@naqvis - `@type.class.methods` will return exactly what I want :) Thank you!
<FromGitter>
<naqvis> are you experiencing some problems with generated json?
<nowhereFast>
`JSON::Any.new("{\"some\":\"val\"}").to_json` results in `"\"{\\\"some\\\":\\\"val\\\"}\""`
<Evtimiy>
nowhereFast, are you trying to assign already generated JSON string to the property ?
<nowhereFast>
since JSON::Any already is already json, I'd expect to_json to not have had an effect on it. expected return: `"{\"some\":\"val\"}"`
<Evtimiy>
I think that JSON::Any is a wrapper that boxes the actual value and encodes it when is requested
<FromGitter>
<naqvis> First of JSON::Any is not what you are treating it like
<FromGitter>
<naqvis> it is a Union of JSON Object types
<nowhereFast>
Evtimiy, I'd like to have a property of type JSON::Any, as a transient blob of json, which I an pass around as part of a bigger entity, and ultimately call to_json on the whole
<FromGitter>
<naqvis> question is how are you getting that `JSON::Any` ?
<FromGitter>
<naqvis> if you are getting that via `JSON.parse` then it contained parsed JSON
<FromGitter>
<naqvis> if you are creating that manually, then it will wrap the value what you provide to it
<nowhereFast>
I think it's as Evtimiy pointed out earlier, the json blob that comes through already has escaped chars
<jhass>
note it's easy to get confused here since JSON looks similar to a namedtuple or hash inspection and inspecting a string puts quotes around and escapes any inner ones
<jhass>
but yeah, if you pass a JSON string to JSON::Any that's most likely not what you want
<jhass>
note how I sometimes used p and other times puts im my example above
<nowhereFast>
yup, it'd have to go through JSON.parse in this case to get the right kind of JSON::Any
<nowhereFast>
I was hoping to be able to get away with not having to do any work on the blob at all
<Evtimiy>
I'm not sure if there is a way for disabling converting/escaping to JSON for certain fields
<Evtimiy>
But if there isn't such way, you can try something else, but I'm not sure that it will be perfect performance-wise
<Evtimiy>
You can set your json blob field to a constant and then replace that in the string returned from to_json
<nowhereFast>
yup, keep out of the entity and tack it on after to_json is called on it
<nowhereFast>
probably the approach for best performance
<FromGitter>
<naqvis> as a workaround, just remove namespace related data `<gpx version="1.1">` and then it should work
<repo>
cool thanks!
<repo>
jhass: hm i don't know how that applies to the //trkpt xpath
<jhass>
exactly the same
<repo>
ahh
<repo>
yeah
<repo>
got it
<repo>
/gpx:trkpt
<repo>
/
<repo>
//
<repo>
:)
<jhass>
LLVM still amazes me sometimes. I wanted to check if it optimizes a tail call friendly recursive definition into actually doing tail calls. Instead it just rewrites it as a loop
<jhass>
yep and does tail calls for more complex ones, nice
<FromGitter>
<watzon> @Blacksmoke16 are all fields on a Granite model nillable by default?
<FromGitter>
<Blacksmoke16> yes
<FromGitter>
<xmonader> is there something special about forward slash when i want to replace with backslash? ⏎ ⏎ ```path = "Dir1\Dir2\File.txt" ⏎ ⏎ puts path.gsub({"\\": "/"})``` ⏎ ⏎ because I get Dir1Dir2File.txt [https://gitter.im/crystal-lang/crystal?at=5ecff5d0778fad0b13314a92]
<FromGitter>
<simonhf> newbie question: In Perl I can do @array = `my_command` to put the output lines of my_command into an array... Is there an equivalent with crystal?
renich_ has joined #crystal-lang
<FromGitter>
<simonhf> sorry, this above lines should have had backticks around the my_command part...
renich has quit [Ping timeout: 256 seconds]
renich_ is now known as renich
<FromGitter>
<Blacksmoke16> `array = my_command`?
<FromGitter>
<Blacksmoke16> `@array` would denote an instance variable named array. local variables dont need it
<FromGitter>
<simonhf> thanks! how does `array = \`my_command\`` know that array should be an array and not a string?
<FromGitter>
<simonhf> sorry... not sure how to escape backticks in markdown :-|
<FromGitter>
<Blacksmoke16> Crystal has some duck typing
<FromGitter>
<Blacksmoke16> the compiler knows `my_command` returns an array and assigns that type to `array`
<FromGitter>
<Blacksmoke16> idk, first thought that came to mind
<oprypin>
```my_command`.each_line``; ```my_command`.split.each`` are some options
<FromGitter>
<simonhf> > @simonhf you can read the API docs here: https://crystal-lang.org/api/master/. ⏎ ⏎ I always try to read the docs before posting here :-) The backticks section is short and didn't help me...
<oprypin>
sigh
<oprypin>
`` `my_command`.each_line``; `` `my_command`.split.each`` are some options
<FromGitter>
<simonhf> 1) prepends the flush thing. 2. creates temporary file with sha256 of one-liner. 3. compiles if not already compiled. 4. deletes older one-liners if too many. 5. runs the compiled one-liner.
<FromGitter>
<Blacksmoke16> prob could just do something like
<FromGitter>
<Blacksmoke16> is all the caching needed?
<FromGitter>
<simonhf> seemed like a nice touch
<robacarp>
what are we doing here with all the sha-ing
<robacarp>
seems off base with the point of the issue
<FromGitter>
<Blacksmoke16> `~/.cache/crystal/` on mac at least
<FromGitter>
<Blacksmoke16> for me, seems it depends on which one of those works ofc
<robacarp>
awesome, interesting. So that 4s was probably so long because it was the first time I've ever run crystal eval
<robacarp>
completely cold cache
<oprypin>
there's even OS-level and CPU-level cache to consider
<robacarp>
yeah, wiping ~/.cache/crystal/eval didn't reset the time to 4s, but it went up to 2s
<robacarp>
sample size small enough to just be variance in cpu load at time of invocation
<robacarp>
my assumption would be that most of that cache is filesystem cache of the crystal compiler
<FromGitter>
<Blacksmoke16> could also add `--stats --progress`
<FromGitter>
<Blacksmoke16> iirc it says if .o files were reused and stuff
<oprypin>
nice
<FromGitter>
<simonhf> > https://crystal-lang.org/api/master/File.html#exists?(path:Path%7CString):Bool-class-method is a thing as well btw ⏎ ⏎ Thanks! I saw that and wondered if there is any advantage of `.exists?` over `.file?`? The docs referred to a path instead of file... so I choose `.file?` to be on the safe side :-)
<FromGitter>
<simonhf> > *<robacarp>* is this because crystal compiling is slow? ⏎ ⏎ Partly, but also because 1. eval does not have all the options that build has for some reason, and 2. writing crystal one liners on the command line, I'd like to be able to type less and maybe have some generic code auto required and/or prepended...
<FromGitter>
<simonhf> > i.e. so it would just be built once, otherwise just run the already existing binary ⏎ ⏎ that's right...
<FromGitter>
<simonhf> > `~/.cache/crystal/` on mac at least ⏎ ⏎ Seems to be same place on Ubuntu too...
<FromGitter>
<simonhf> And when you run crystal eval ... then it creates the folder `~/.cache/crystal/eval/` ... is that a bug? or just a not finished off feature? because it doesn't actually cache anything for eval, or?
<FromGitter>
<simonhf> It always takes the same amount of time to build the same eval source code...
<FromGitter>
<simonhf> But it's the same for cre.cr too: ⏎ ⏎ ```code paste, see link``` ⏎ ⏎ It always takes the same time to build even though `~/.cache/crystal/home-simon-20200526-crystal-cre.cr/` exists... [https://gitter.im/crystal-lang/crystal?at=5ed0287d7da67d06fada45df]
<FromGitter>
<simonhf> So it it really 'caching' anything? Or is .cache really a .build folder...
<FromGitter>
<Blacksmoke16> what if you clear that folder then try again
<FromGitter>
<simonhf> > what if you clear that folder then try again ⏎ ⏎ ```code paste, see link``` ⏎ ⏎ Interesting... you're right... it saves about 1/3rd of the time... so it cache 'a bit'... is that expected? [https://gitter.im/crystal-lang/crystal?at=5ed0292b4412600ccd74648d]
<FromGitter>
<Blacksmoke16> prob yea
<FromGitter>
<Blacksmoke16> some of the `.o` files get cached
<FromGitter>
<Blacksmoke16> if you run with `--progress` you can see
<FromGitter>
<Blacksmoke16> can reuse some `.o` files, im not super familiar with what those are/process around that tho
<FromGitter>
<simonhf> > could also add `--stats --progress` ⏎ ⏎ Nice. I updated cre.cr so that it also caches the build log, and has its cache in ~/.cache/cre which seems like a better location...
<jhass>
what's your obsession with sprintf/printf btw?
<FromGitter>
<simonhf> > *<jhass>* @simonhf: I never even ran this, but some ideas: https://p.jhass.eu/8j.cr ⏎ ⏎ Thanks so much for posting that... gives me a lot of inspiration... I'm going to make some changes! ;-)
<jhass>
ah, line 10 needs to be a range for example, 0..-CACHE_SIZE
<jhass>
always get confused between the two overloads
<FromGitter>
<simonhf> > *<jhass>* what's your obsession with sprintf/printf btw? ⏎ ⏎ just the bad habits of a C programmer :-) but open to alternatives! I noticed you did `Log.debug { "Writing #{body} to #{source_path}" }` ... how would you pad stuff if body was numeric, e.g. `%05d`? And also, does crystal have comma padding, e.g. `%'05d`?
<jhass>
things like that, or actually go to formatting for these rare cases, and not invoke sprintf on a single string liteal assignment that doesn't even have any format specifiers in it...
<jhass>
and I tend to prefer String#% over sprintf
<FromGitter>
<simonhf> > https://crystal-lang.org/api/master/Number.html#format(separator='.',delimiter=',',decimal_places:Int?=nil,*,group:Int=3,only_significant:Bool=false):String-instance-method ⏎ ⏎ So that would be something like e.g. `Log.debug { "bytes=#{bytes.format(...)}" }` ?
<FromGitter>
<Blacksmoke16> well if you want bytes
<FromGitter>
<simonhf> Yeah, I'm all over that page... one of the frustrating things about the docs is that they never seem to include any example snippets...
<FromGitter>
<Blacksmoke16> Log stuff is changing quite a bit in next version
<FromGitter>
<simonhf> like I tried `CRYSTAL_LOG_SOURCES=STDOUT CRYSTAL_LOG_LEVEL=INFO` but nothing happens :-(
<jhass>
for basic usecases like above actually not that much
<jhass>
forget about LOG_SOURCES, you don't need it here
<jhass>
and of course info level does not show debug logs
<FromGitter>
<simonhf> > Of s ⏎ ⏎ How would that work?
<FromGitter>
<simonhf> I tried `["body", body, "path", path].each {|s,v| Log.debug { "s=#{v}" }}` but each only provides one argument...
<jhass>
j8r: the nice thing about seeing_is_believing is that it's designed such that it can run in the editor and replace the buffer contents. Watch how he uses it in the video, that's what I'm talking about, it basically turns your editor into an interactive repl
<jhass>
maybe we should have Log.p! and Log.pp!
<FromGitter>
<j8r> Yeah... would not be easy to do in Crystal
<jhass>
crystal playground does it basically
<jhass>
crystal play I mean
<FromGitter>
<Blacksmoke16> > *<jhass>* maybe we should have Log.p! and Log.pp! ⏎ ⏎ eh, a better case would be to use these as metadata
<FromGitter>
<j8r> With compiler knowledge, may be it is not hard - don't know
<FromGitter>
<simonhf> given a variable, is there a way to introspect at run-time and get its text name?
<oprypin>
no..?
<jhass>
no, that's why we have macros
<oprypin>
i mean, if you're referring to a variable, you have its text name right there. just instead of `a` write `"a"`
<FromGitter>
<simonhf> aaah... kk
<FromGitter>
<simonhf> how would I add the macro to this line? `[body, path].each {|s| Log.debug { "s=#{s}" }}`
<FromGitter>
<Blacksmoke16> :thinking:
<FromGitter>
<Blacksmoke16> prob be a better design to do (0.35.0) `Log.debug &.emit(body: body, path: path)`
<FromGitter>
<Blacksmoke16> or `Log.debug &.emit("Running command", body: body, path: path)`
<oprypin>
`{% for v in ["body", "path"] %}; Log.debug { "#{{{v}}}={{v.id}} }; {% end %}`
<FromGitter>
<j8r> btw don't abuse macros
<oprypin>
the idiomatic thing here would indeed be to just write 2 lines
<FromGitter>
<j8r> harder to debug, longer compile times
<FromGitter>
<simonhf> yeah, but I want to have a longer list of stuff...
<FromGitter>
<simonhf> unfortunately the macro line gives errors... trying to debug it... :-)
<oprypin>
simonhf, fyi for trying things out this functionality is built-in (just not using `Log`): `p! body, path`
<jhass>
or pp! for pretty printing
<FromGitter>
<simonhf> thanks
<oprypin>
>> body = "a"; path = 6; p! body, path
<FromGitter>
<Blacksmoke16> if all you need is to print to STDOUT the you'd be better off doing something like
<oprypin>
`STDIN.raw` might be the key to success there?
<FromGitter>
<simonhf> interestingly the eval code compiles but nothing comes out: `CRYSTAL_LOG_LEVEL=DEBUG crystal eval 'require "log"; body = "a"; path = 6; {% for v in ["body", "path"] %} Log.debug { "#{{{v}}}={{v.id}}" }; {% end %}'`
<oprypin>
presumably cuz Log.debug is silent by default?
<FromGitter>
<simonhf> I guess somehow the env var is not getting passed down?
<oprypin>
oh nvm you set the level. maybe something wrong there, yea
<oprypin>
simonhf, `LOG_LEVEL=DEBUG cr eval 'require "log"; Log.setup_from_env; body = "a"; path = 6; {% for v in ["body", "path"] %} Log.debug { "#{{{v}}}={{v.id}}" }; {% end %}'`
<oprypin>
woops, that `cr` is my alias for crystal@master
<oprypin>
current one is `CRYSTAL_LOG_LEVEL=DEBUG crystal eval 'require "log"; Log.setup_from_env; body = "a"; path = 6; {% for v in ["body", "path"] %} Log.debug { "#{{{v}}}={{v.id}}" }; {% end %}'`
kradnoel has quit [Read error: Connection reset by peer]
kradnoel has joined #crystal-lang
<FromGitter>
<j8r> Using crystal eval for code snippets is not really readable you know...
<FromGitter>
<simonhf> I guess it's what you get used to? :-
<FromGitter>
<simonhf> :-)
<FromGitter>
<watzon> Ok there is definitely a std bug somewhere. I keep getting this: ⏎ ⏎ ```code paste, see link``` ⏎ ⏎ they happen with the same code at different times, and only when trying to make an HTTP request from a fiber [https://gitter.im/crystal-lang/crystal?at=5ed03faf4412600ccd7498c7]
<FromGitter>
<watzon> Fml this is making life difficult
<FromGitter>
<simonhf> are those ??? stack corruption or something else?
<FromGitter>
<watzon> Probably unreadable symbols in the stack. I know there's a PR that got merged that's supposed to help with that.
<FromGitter>
<j8r> @simonhf everybody properly humans reads better with a formatted code which doesn't have lines of +200 chars
<FromGitter>
<simonhf> 'properly human' lol :-)
<FromGitter>
<j8r> you might be not haha? Only computers like minified code put on a single line to parse faster
<straight-shoota>
@watzon, seems like this is erroring inside libssl
<FromGitter>
<watzon> Yeah, but it only happens in one specific case. When creating a HTTP::Client request from within a running fiber. Idk what's going on, but it's driving me crazy.
<FromGitter>
<simonhf> Running out of stack space?
<FromGitter>
<j8r> you are not using mt @watzon ?
<FromGitter>
<watzon> Not currently. I could try and see if it helps.
<FromGitter>
<j8r> Not really
<FromGitter>
<j8r> It you were using mt, it could be something related to thread safety