greengriminal has quit [Ping timeout: 272 seconds]
greengriminal has joined #crystal-lang
greengriminal has quit [Quit: This computer has gone to sleep]
<FromGitter>
<proyb6> Does this code allocate any new string or stack?
<FromGitter>
<proyb6> p "hello"[1..-1]
rohitpaulk has quit [Ping timeout: 244 seconds]
<FromGitter>
<aboeglin> @fenicks I don't see any coming, where do you see that ?
<FromGitter>
<aboeglin> using HTTP::Server::Response: ⏎ ⏎ ```code paste, see link``` ⏎ ⏎ If anyone can explain me why and if using a NamedTuple is the prefered way when returning json from an http server. [https://gitter.im/crystal-lang/crystal?at=5bc57c3ff659e67772c2c9bc]
rohitpaulk has joined #crystal-lang
ua_ has quit [Ping timeout: 250 seconds]
<FromGitter>
<Timbus> Looks like the compiler thinks you are passing a block to 'print', in the second example.
ua has joined #crystal-lang
<FromGitter>
<fusillicode_twitter> Guys just to be sure, right now there is only one viable solution to query a mongo db instance from Crystal right?
<FromGitter>
<proyb6> if I wanted to return only the URL after /user path as in /user/proyb6, only proyb6 and the rest will pass to the variable without allocating any resource? Not sure if that’s possible
<FromGitter>
<fenicks> @bew a member of the staff answer my question in discussions.
<FromGitter>
<fenicks> It's not planned for now but it's coming
<FromGitter>
<bew> @proyb6 how are you going to use the partial string?
<FromGitter>
<bew> @fenicks good to know! I'm living in paris, so yeah i'm exited :p
<FromGitter>
<bew> +c
return0e has joined #crystal-lang
<FromGitter>
<fenicks> @bew OK, me too
<FromGitter>
<fenicks> I'm from Paris too
<FromGitter>
<fenicks> 👍
moei has quit [Read error: Connection reset by peer]
moei has joined #crystal-lang
<FromGitter>
<iSarCasm> Hello guys, is there any info about when new crystal release is coming?
<FromGitter>
<bew> @fenicks cool! let's meet one day!
<FromGitter>
<proyb6> @bew the string was actually sent to context.response to return string to the web browser
<FromGitter>
<bew> @proyb6 you can try using `Slice`, you can get a slice from a String with `String#to_slice`, but note that it'll not play nicely with UTF-8
<FromGitter>
<bew> I have a shard idea to make a StringView type, that will play well with UTF-8 and avoid allocating new Strings when all you want is to get a part of a string
<FromGitter>
<bew> but there's no builtin way as far as i know
<FromGitter>
<proyb6> I see, I look forward to try your shard if it will handle http routing like /user/:id that could be done uncomplicated
<FromGitter>
<proyb6> I came across HN which I thought could be useful as a shard for C in crystal
<FromGitter>
<aboeglin> @bew I found my solution, in that case I just needed to check if the object had 1 property equal, and includes would test reference equality I suppose as my object doesn't implement == operator
<FromGitter>
<bew> ok perfect then
rohitpaulk has joined #crystal-lang
<jokke>
is there something like a code style _checker_ for crystal (not crystal tool format)?
<jokke>
i noticed crystal tool format can be run with --check but that doesn't give any output
<jokke>
only exit code
<jokke>
preferably something configurable
rohitpaulk has quit [Ping timeout: 272 seconds]
ashirase has quit [Read error: Connection reset by peer]
<jokke>
z64: yeah it doesn't do any format checks though
<jokke>
another question: how can i display all compile flags the running program was built with?
<z64>
i see. it said on the tin it "enforces consistent crystal code style", i guess i don't know what that means then lol :p
<jokke>
hm ok
<z64>
i doubt there is anything like it though, the formatter itself by design is not configurable, one would have to completely write one from scratch i would imagine
<jokke>
mh :/
<z64>
if you're interested in formatter changes, i will usually stage all my files before running it. then the formatters changes will dirty the working copy diff so i can review them
<z64>
usually with `git add -p` etc
<jokke>
yeah
ashirase has joined #crystal-lang
marmotini_ has joined #crystal-lang
<FromGitter>
<aboeglin> I have a question regarding classes. I see a lot of code with classes inside classes like this : ⏎ ⏎ ```class A ⏎ class B ⏎ end ⏎ end``` ⏎ ⏎ Is this syntax described somewhere ? Is it a way to namespace ? Is class A expected to be "used", meaning instantiated ? What is the use-case for this ? [https://gitter.im/crystal-lang/crystal?at=5bc5e44e64cfc273f9d6ad4f]
<FromGitter>
<j8r> Yes this is a way to namespace.
<FromGitter>
<aboeglin> how would one use it then ?
<FromGitter>
<aboeglin> A::B.new ?
<z64>
yes
<FromGitter>
<j8r> Yes if you are outside, but inside `A` you can `B.new`
<FromGitter>
<aboeglin> and what about just declaring the class as class A::B ?
<FromGitter>
<j8r> like any defined class defined upper in the hierarchy in fact
<FromGitter>
<aboeglin> and why not using a module for that ?
<FromGitter>
<j8r> you can also, but here you may need instance variables
<FromGitter>
<aboeglin> you mean B may use instance variables from A ? :o
marmotini_ has quit [Ping timeout: 250 seconds]
<FromGitter>
<aboeglin> so you'd do instance_of_a::B.new ?
<FromGitter>
<j8r> a purpose of this is when you want to use a class inside this class, you can `private class`
<FromGitter>
<aboeglin> yes, obviously
<FromGitter>
<aboeglin> you could also have two classes in a single file and one would be private right ?
<FromGitter>
<aboeglin> and is it possible to define two sub classes in two different files ?
<jokke>
so what i'm trying to do: instead of invoking a method on the block argument i want to invoke a method on another object and pass it the block argument(s)
<FromGitter>
<aboeglin> wahh
<FromGitter>
<aboeglin> it opens a whole new world ^^
<FromGitter>
<j8r> haha, but better not to open this Pandora's box 😏
<FromGitter>
<j8r> only when you really have to
<jokke>
anyone?
<jokke>
this is a pretty common use case.
<jokke>
especially with things like `try` or `map`
<z64>
jokke: i dont understand what you're trying to do, could you provide a full example
<FromGitter>
<fusillicode_twitter> @asterite it seems that `skip` does exactly what I need, thanks really a lot for the support with such a stupid question 😓
<FromGitter>
<asterite> I think we had `skip` and `drop` at one point, but because we don't want aliases... just note that right now `array[1..-1]` will probably be faster than `skip`, I'm writing a PR to fix that
<FromGitter>
<j8r> or something like `array[1..-1]?`
DTZUZU has quit [Quit: WeeChat 2.2]
DTZUZU has joined #crystal-lang
<f1refly>
Can someone here give me a hint how to create a File::Stat ? When i require "file" it doesn't find either "Stat" nor "File::Stat"
<f1refly>
Never mind, i just found out that it was deprecated and replaced by File::Info
<FromGitter>
<fusillicode_twitter> thanks once again for the broad explanation @asterite 🙇
<FromGitter>
<michelson> Hoy can I initialize a NamedTuple from a Hash(k,v) ?
<FromGitter>
<michelson> with JSON.parse I've receive a mixed type like `Hash{a: b , NamedTuple{c: b}}`
<FromGitter>
<straight-shoota> @michelson `JSON.parse` can't possibly return a `NamedTuple` type
<FromGitter>
<michelson> it that NamedTuple needs to know the key values it will receive ?
<FromGitter>
<michelson> how can I initialize it with a variable k , v ?
<FromGitter>
<asterite> The answer is that you don't need NamedTuple. There's no use case for NamedTuple other than representing named arguments
rohitpaulk has quit [Ping timeout: 272 seconds]
<Yxhuvud>
.. unless some API made by someone else requires them.
<FromGitter>
<Blacksmoke16> i with you could splat annotations
<FromGitter>
<michelson> I'm implementing a class with receives a hash option and I initializing some instance variables, here is the code ⏎ ⏎ ```code paste, see link``` ⏎ ⏎ error is in line 11:`instance variable '@options' of Command must be Hash(Symbol, String), not NamedTuple(message: String)` ... [https://gitter.im/crystal-lang/crystal?at=5bc60b511c100a4f2996da81]
<FromGitter>
<michelson> now I'm just parsing JSON and .to_json to tackle this
<FromGitter>
<michelson> but it does not feel right
<FromGitter>
<Blacksmoke16> thats not a hash
<FromGitter>
<Blacksmoke16> hash would be like `{:command => "one"}`
<FromGitter>
<asterite> Right, exactly. Now sure what "options" is used for, though
<FromGitter>
<michelson> it is supposed to be a hash with variable key values
<FromGitter>
<asterite> all of the same type? Say, string?
<FromGitter>
<asterite> It should probably be "Hash(String, String)" then, if you plan to read that hash from a configuration or something (you can't create a Symbol dynamically)
wontruef_ has quit [Read error: Connection reset by peer]
wontruef_ has joined #crystal-lang
wontruefree has quit [Read error: Connection reset by peer]
marmotini_ has joined #crystal-lang
DTZUZU has quit [Quit: WeeChat 2.2]
DTZUZU has joined #crystal-lang
rohitpaulk has quit [Ping timeout: 246 seconds]
<FromGitter>
<j8r> @asterite you have changed back your image from the Asterite to the Apple
moei has joined #crystal-lang
<jokke>
i'm still experiencing this very weird bug where something with the socket of a http connection is going terribly wrong using traefik as a proxy
<jokke>
the problem is: i can't really debug it since this is what i see on stdout: https://p.jokke.space/Iqur/
<jokke>
even though i'm not compiling with --release
<jokke>
might also be something related to docker swarm and the overlay network docker uses
<jokke>
this reliably happens after the app has been running for a while
<jokke>
i'm thinking that maybe traefik tries to keep the socket open indefinetely and at some point it just timeouts
<FromGitter>
<Blacksmoke16> i was getting something similar iirc
<jokke>
really?
<jokke>
did you solve it somehow?
<FromGitter>
<asterite> @j8r Yeah, I missed my original avatar...
<FromGitter>
<Blacksmoke16> yea, is coming back to me
<jokke>
you would make my day!
<FromGitter>
<Blacksmoke16> issue was on a period task i had running using mosquito
<FromGitter>
<Blacksmoke16> if it was long than x minutes between each it would throw an error like that
<FromGitter>
<Blacksmoke16> solved it by calling `.close` on the HTTP::Client instance i had
<jokke>
hmmmm
<FromGitter>
<Blacksmoke16> as i think it was trying to keep it open for too long and it was timing out
<jokke>
i have HTTP::Server though
<FromGitter>
<Blacksmoke16> hm, could be realted?
<FromGitter>
<aboeglin> Error writing to socket: Broken pipe
<FromGitter>
<aboeglin> Do you get something like that ?
<jokke>
yeah
<jokke>
exactly that
<FromGitter>
<aboeglin> It just seems I have it too
<FromGitter>
<aboeglin> on swarm
<jokke>
aha!
<FromGitter>
<aboeglin> doesn't happen with docker-compose locally though
<jokke>
yeah
<FromGitter>
<aboeglin> but I use nginx as reverse proxy
<jokke>
seems to be a swarm issue then
<jokke>
i'm also not 100% sure _which_ socket this is but several things point to it not being http but rather postgres
<FromGitter>
<aboeglin> well, I don't get that with other languages
<jokke>
hm true
<FromGitter>
<aboeglin> oh, you use postgres as well on that service ?
<jokke>
yeah
<FromGitter>
<aboeglin> haha
<FromGitter>
<aboeglin> I do too ..
<jokke>
we're getting closer
<jokke>
because i have some routes that don't hit the db and those work fine
<jokke>
also the db connection would be a long running connection
<FromGitter>
<Blacksmoke16> is the container that hits the db on a diff service?
<jokke>
not sure what you mean
<jokke>
i have my crystal webapp as one service and postgres as another
<jokke>
maybe it's just an issue of making the db shard more resistant against closed sockets
<FromGitter>
<Blacksmoke16> yea could the issue be the swam timing out the connection between services
<jokke>
yeah could be. but that'd be super annoying
<FromGitter>
<Blacksmoke16> im not *super* familiar with swarm to know how to fix it, but why dont you have everything in the same service just diff containers?
<FromGitter>
<Blacksmoke16> swarm has a notion of creating a service, and compose has its own notion of a service
<jokke>
aboeglin: i'll report back in an hour if it's still working :)
<jokke>
Blacksmoke16: thanks for helping with the debugging!
<FromGitter>
<Blacksmoke16> np, hope you get it fixed
<jokke>
yeah me too :)
<FromGitter>
<aboeglin> Thanks, it would be great !
<FromGitter>
<kingsleyh> how do I make an any type? would it be something like this? `alias Any = String|Nil|MyType` or like this `alias Any = Union(String,Nil,MyType)`
<FromGitter>
<j8r> Yes @kingsleyh
<FromGitter>
<kingsleyh> which one?
<FromGitter>
<kingsleyh> this seems to work: ` alias Any = Union(RethinkDB::Cursor | RethinkDB::QueryResult | Array(RethinkDB::QueryResult) | Nil)`
<FromGitter>
<Blacksmoke16> dont need the union, just `|` separated
DTZUZU has quit [Read error: Connection reset by peer]
<FromGitter>
<aboeglin> Is your service still up ?
<FromGitter>
<valamorgon> I've an array of hashes like this [{id=>1},{id=>2}] and I want to find a specific hash by using one of its attribute, like finding hash with id 1 in my example. Is there any way to achieve this without manually looping or changing structure of my array?
<FromGitter>
<valamorgon> like find property of array in javascript
<FromGitter>
<valamorgon> method*
<FromGitter>
<aboeglin> Enumerable.find
<FromGitter>
<aboeglin> It works exactly like in js with a block
<FromGitter>
<valamorgon> @aboeglin I guess this is it, I will work on it, thanks
marmotini_ has quit [Ping timeout: 260 seconds]
marmotini_ has joined #crystal-lang
<FromGitter>
<kingsleyh> how can I turn JSON::Any into a Hash of Symbol => valid Json type?
<FromGitter>
<kingsleyh> it's easy to turn `{name: "kings"}.to_json` but I want to go the other way
<FromGitter>
<kingsleyh> and start with `{"name "=> "kings"}.to_h` and get back the original hash map
<FromGitter>
<kingsleyh> is there a library that does this or some built in thing that does this - or do I have to write my own Macro for this?
<FromGitter>
<straight-shoota> A Hash of symbol keys is nothing you should ever do in Crystal.
<FromGitter>
<straight-shoota> Just use string keys
<FromGitter>
<straight-shoota> There is no benefit in using symbols, it's just more complicated
<FromGitter>
<kingsleyh> ok - but how to get the JSON into the hash?
<FromGitter>
<kingsleyh> do I have to write my own Macro? or is there something that will do it already?
<FromGitter>
<straight-shoota> `any.as_h.transform_keys &.as_s` should to it.
<FromGitter>
<kingsleyh> the keys are already strings
<FromGitter>
<kingsleyh> any.as_h give me Hash(String, JSON::Any)
<FromGitter>
<kingsleyh> so is there a transform_values
<FromGitter>
<kingsleyh> any.as_h.transform_values(&.as_s) - but then all the values become strings
<FromGitter>
<kingsleyh> so also not what I wanted - I would like the value to become the original type - e.g. Int32|String|anyother valid json type
DTZUZU has joined #crystal-lang
<FromGitter>
<kingsleyh> I guess it’s impossible to know the type from the json
<FromGitter>
<kingsleyh> As it’s JSON any
<FromGitter>
<kingsleyh> I would have to keep a type mapping inside the json as well
<FromGitter>
<kingsleyh> To know how to create the hash with the correct type
<FromGitter>
<girng> you can specifically set the types if you know the json in advance
<FromGitter>
<straight-shoota> Oh, yes you don't need to transform the keys for JSON. I was thinking about YAML::Any
<FromGitter>
<straight-shoota> you can do `any.as_h.transform_values(&.raw)` to get the raw type (`JSON::Any::Type`). But it is generally better to keep it as `JSON::Any` for better accessibility
Heaven31415 has joined #crystal-lang
<Heaven31415>
Hi
ashirase has joined #crystal-lang
marmotini_ has quit [Ping timeout: 244 seconds]
<FromGitter>
<girng> Hi Heaven
<FromGitter>
<ljuti> @straight-shoota You implemented a custom type `Value` for Crinja, right? Is the current implementation you have “complete”, in other words a working one?
<FromGitter>
<ljuti> I tried to implement a similar custom type by using that as an example and it’s mostly working, but iterating over hash-like structures doesn’t work
<FromGitter>
<ljuti> Maybe I missed something, and it’s best to rewrite it tomorrow with better spec coverage :)
<FromGitter>
<straight-shoota> yep, it's currently pretty much complete
<FromGitter>
<straight-shoota> at least considering what Crystal allows to do. I'd like to wrap any `Object`, really.
<FromGitter>
<straight-shoota> It's pretty much the same concept as `JSON::Any` and `YAML::Any` btw