<hightower2>
Is it possible to have a "case var; when some_list", and have the 'when' match if var's value is found in some_list?
<hightower2>
I tried both with an array and tuple and specifying "when *list" and "when list", and it didn't work (no error, but didn't match)
<hightower2>
(I mean I get error with '*list', and no error but also no match with 'list')
<FromGitter>
<PlayLights_twitter> Include?
<hightower2>
yes but I can't write that in a case/when syntax
<FromGitter>
<PlayLights_twitter> well maybe you can save the result before the case when by the way thats dirty
rohitpaulk has quit [Ping timeout: 245 seconds]
rohitpaulk has joined #crystal-lang
faustinoaq has quit [Quit: IRC client terminated!]
<hightower2>
I am calling var1, var2, var3 = string.split 'delimiter', and am getting Index out of bounds because there are no 3 fields in string. Is there a way to default the missing ones to nil rather than raising?
<FromGitter>
<bew> Don't think so
rohitpaulk has quit [Ping timeout: 240 seconds]
return0e has quit [Remote host closed the connection]
<FromGitter>
<aisrael> @hightower2 This isn’t pretty but does the job: ⏎ `var1, var2, var3 = string.split(delimiter) + [nil, nil, nil]`
alex`` has joined #crystal-lang
rohitpaulk has joined #crystal-lang
jetpack_joe has joined #crystal-lang
<FromGitter>
<bew> this is very very bad memory wise, as I think it'll create 4 arrays: one for the split, one for the `[nil]`, one for the `[nil] * 3` and one for the concatenation of the split's array, and the `[nil] * 3`'s array...
rohitpaulk has quit [Ping timeout: 256 seconds]
rohitpaulk has joined #crystal-lang
rohitpaulk has quit [Ping timeout: 265 seconds]
rohitpaulk has joined #crystal-lang
<FromGitter>
<aisrael> That’s true. Well, maybe in the future the compiler can optimize the `[nil] * 3` and make it like, `Array.new(3, nil)` so less one array creation :)
<FromGitter>
<aisrael> Is there a way to resize or add capacity to an array, filling up the new elements with some default value (or `nil`)?
rohitpaulk has quit [Ping timeout: 240 seconds]
<FromGitter>
<Qwerp-Derp> How do I get the maximum value an `IntXX` class can be from an instance of that class? So for example, if I have `x` which is an `Int16`, I want to get `Int16.MAX`, but if it's an `Int64` I want `Int64.max` instead.
<FromGitter>
<ezrast> @Qwerp-Derp I'm not sure if there's a general way but there's this: https://carc.in/#/r/3nv6
<FromGitter>
<Qwerp-Derp> `Math.round` just rounds to whatever precision is the maximum for `base.max`, so in this case it only rounds to 9 decimal places
<FromGitter>
<Qwerp-Derp> This is probably not the best solution but I'm open to ideas before I make a PR
bonemind has joined #crystal-lang
rohitpaulk has joined #crystal-lang
alex`` has joined #crystal-lang
bonemind has quit [Ping timeout: 256 seconds]
duane has joined #crystal-lang
yaewa has joined #crystal-lang
moei has quit [Ping timeout: 245 seconds]
duane has quit [Ping timeout: 240 seconds]
gewo has quit [Quit: WeeChat 1.6]
yaewa has quit [Quit: Leaving...]
moei has joined #crystal-lang
bonemind has joined #crystal-lang
wojnar has joined #crystal-lang
return0e has quit [Read error: No route to host]
return0e has joined #crystal-lang
gewo has joined #crystal-lang
Ven`` has joined #crystal-lang
Ven`` has quit [Read error: Connection reset by peer]
Ven` has joined #crystal-lang
wuehlmaus has joined #crystal-lang
Papierkorb has joined #crystal-lang
bonemind has quit [Ping timeout: 256 seconds]
wojnar has quit [Remote host closed the connection]
duane has joined #crystal-lang
duane_ has joined #crystal-lang
duane has quit [Remote host closed the connection]
wojnar has joined #crystal-lang
<jokke>
are there any "helpers" for porting ruby code to crystal?
<jokke>
like somekind of half-automatic programs
<jokke>
that crawl through the code and at least do the obvious stuff
maattdd_ has joined #crystal-lang
maattdd_ has quit [Ping timeout: 255 seconds]
bonemind has joined #crystal-lang
duane_ has quit [Quit: leaving]
Papierkorb has left #crystal-lang ["Konversation terminated!"]
<FromGitter>
<spencerwi> Is there a roadmap or "crystal-devel" mailing list of some sort for Crystal? I always find it interesting to track the development of interesting languages, and Crystal is a pretty slick one.
gewo has quit [Ping timeout: 245 seconds]
shalmezad has joined #crystal-lang
<RX14>
@spencerwi your best bet is watching the crystal github repo
<FromGitter>
<drosehn> jokke : You'll start out thinking that translating from ruby to crystal should be trivial, but the more you that you try that, the more you'll realize there are many issues to deal with. Any one issue might be trivial, but it's not trivial to solve all of them. Especially since the crystal language and stdlib are still changing.
Ven` has quit [Ping timeout: 240 seconds]
Ven`` has joined #crystal-lang
DTZUZU has quit [Ping timeout: 268 seconds]
maattdd_ has joined #crystal-lang
Ven`` has quit [Ping timeout: 256 seconds]
Ven`` has joined #crystal-lang
<jokke>
DeBot: i know it's not trivial
<jokke>
äh
<jokke>
drosehn
DTZUZU has joined #crystal-lang
<FromGitter>
<cserb> guys, I'm trying to access a value from a parsed json (`peer["ident"].as_s`) but I'm getting a weird error (which I can't reproduce in any other example)
<FromGitter>
<cserb> Unhandled exception in spawn: ⏎ Expected Hash for # (key : String), not String (Exception) ⏎ from /usr/lib/crystal/json/any.cr:0:7 in '[]' ⏎ from src/avram.cr:108:15 in 'handle_new_peers' ⏎ from src/avram.cr:94:7 in 'handle_peer_response' ... [https://gitter.im/crystal-lang/crystal?at=5a99667c35dd17022e12034c]
Ven`` has quit [Ping timeout: 276 seconds]
<RX14>
@cserb it means that `peer` is a JSON string
<RX14>
since obviously "foo"["ident"] doesn't make sense you get an exception
Ven`` has joined #crystal-lang
<FromGitter>
<cserb> @RX14 why would that be the case: `puts "+++++ #{typeof(peer["ident"].as_s)}"`
<FromGitter>
<cserb> this works
<FromGitter>
<cserb> if I remove `typeof()` I get the error
<RX14>
because typeof doesn't actually do anything
<RX14>
typeof asks the compiler "what is the type of this expression"
<RX14>
the expression is never compiled
<RX14>
or run
<RX14>
or done anything with
Ven`` has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
<RX14>
typeof can never raise an exception because it's basically a constant
<FromGitter>
<cserb> I'm super confused :) So you are saying `peer` is not a `JSON::any`
<RX14>
no
<RX14>
this is a runtime error
<RX14>
typeof operates at compile-time
<RX14>
the compiler has no idea what's in a JSON::Any
<FromGitter>
<cserb> yeah sure. Let's forget about typeof. I just tried different things to find out what the problem is
<RX14>
it could be a hash, a string, an array, the compiler has no idea
<RX14>
when you do typeof(peer["ident"].as_s) you're asking the compiler what's the type of that expression
<RX14>
the expression always compiles so the compiler replies "it's a string of course"
<RX14>
but at runtime, if peer is not an object, then `peer["ident"]` will raise an exception
<RX14>
and if `peer["ident"]` is not a string, `peer["ident"].as_s` will raise an exception
<RX14>
the compiler has no idea what data is in the `peer` variable so it has no clue that those exceptions will be raised
<RX14>
that's why it works
<RX14>
the root cause is that `peer` is a JSON::Any containing a string
<RX14>
but you're using `peer["ident"]` as if `peer` is a JSON::Any containing a Hash
<RX14>
and it's not
<RX14>
which is probably because you made a mistake further up your code
<RX14>
or you read the structure of your JSON wrong
<RX14>
or something
<FromGitter>
<cserb> so assuming what you this should work: foo = JSON.parse(peer.as_s); foo["ident"].as_s
<RX14>
I doubt it
<RX14>
that would imply there's JSON inside a string inside json
<RX14>
which I doubt there is
DTZUZU has quit [Ping timeout: 276 seconds]
<FromGitter>
<cserb> but it did work
<RX14>
then there's JSON in your JSON
<RX14>
which is super weird
<RX14>
can you show me what you're trying to parse?
<FromGitter>
<cserb> ```code paste, see link```
<FromGitter>
<cserb> This string I take and do a JSON.parse
<RX14>
where are you getting this JSON from?
<RX14>
because that JSON is super weird
<RX14>
that's JSON encoded inside a string inside a json array
avdi has quit [Ping timeout: 240 seconds]
manveru has quit [Read error: Connection reset by peer]
<FromGitter>
<cserb> Yes I realised this now as well...
<FromGitter>
<cserb> Oh now I think I found my mistake
manveru has joined #crystal-lang
<FromGitter>
<cserb> I'm creating it via to_json from a Hash
avdi has joined #crystal-lang
<RX14>
so you must be calling to_json twice
<RX14>
on the hash and on the array of strings
maattdd_ has quit [Ping timeout: 260 seconds]
<FromGitter>
<cserb> nope
<RX14>
well somehwere there's JSON encoding going on twice
<RX14>
or you wouldn't get that output
<FromGitter>
<cserb> ok so this is the line: ` a = "response|#{@avram_peer_repo.map {|peer| {ident: peer.ident, port: peer.port}}}"`
<FromGitter>
<cserb> and I had ` a = "response|#{request}|#{@avram_peer_repo.map {|peer| {ident: peer.ident, port: peer.port}}.to_json}"`
<FromGitter>
<cserb> it turns out I don't need the to_json...
maattdd_ has joined #crystal-lang
wojnar has quit [Remote host closed the connection]
wojnar has joined #crystal-lang
DTZUZU has joined #crystal-lang
<FromGitter>
<cserb> hm ok so the correct way to do it is: `a = "response|#{@avram_peer_repo.map {|peer| {"ident" => peer.ident, "port" => peer.port}}.to_json}"`
<FromGitter>
<cserb> this works now
<FromGitter>
<cserb> many thanks @RX14
<RX14>
you do need the to_json
<RX14>
and the first and the last ones should be equivalent
<FromGitter>
<cserb> with hash rocket I get `"[{\"ident\":\"2b781707-0d0f-428d-8867-17b2b0a5c740\",\"port\":12346}]"`
<FromGitter>
<jwaldrip> It looks like its happening in: Psykube::V2::Manifest::new<YAML::ParseContext, YAML::Nodes::Node+>
<FromGitter>
<jwaldrip> But hard to find where
<FromGitter>
<bew> which crystal ersion are you using?
<FromGitter>
<bew> with 0.24.1 you should have a better trace iirc
<FromGitter>
<watzon> Yeah feel like `Iterator#first` is kinda counterintuitive
<FromGitter>
<watzon> I'd expect that to just return the first value
<FromGitter>
<jwaldrip> I am using 0.24.1
<FromGitter>
<bew> @watzon it is documented that it returns an iterator that would return the first or first N elements
<FromGitter>
<bew> @jwaldrip hmm you're on macos maybe?
<FromGitter>
<watzon> Yeah I see it now. I just feel like take makes more sense, especially in conjunction with skip. Or it could be both since skip and last seem to do the same thing.
<FromGitter>
<bew> you can open an issue about that if you think there is room for discussion
<FromGitter>
<jwaldrip> I am on macos
<FromGitter>
<bew> I don't know it well, but iirc there is a way to have file & line number in traces, do you see what I'm taking about?
greengriminal has quit [Quit: This computer has gone to sleep]
greengriminal has joined #crystal-lang
hightower has joined #crystal-lang
hightower has quit [Read error: Connection reset by peer]
greengriminal has quit [Quit: This computer has gone to sleep]
illy_ has quit [Read error: Connection reset by peer]
illy_ has joined #crystal-lang
duane has quit [Ping timeout: 268 seconds]
<FromGitter>
<watzon> If you look at my github profile I think you can tell that Crystal is my favorite language
<FromGitter>
<bew> Ahahah me too I think
<FromGitter>
<watzon> @bew I think it's funny that you have Astral pinned and haven't done anything with it
<FromGitter>
<watzon> Are you still planning on writing a PEG parser generator?
rohitpaulk has joined #crystal-lang
rohitpaulk has quit [Ping timeout: 256 seconds]
<FromGitter>
<bew> Yeah I know, it's not even written, just ideas & wish ˆˆ I don't know I have other projects in the work that have higher priority than astral for now
greengriminal has quit [Quit: This computer has gone to sleep]