<asterite>
Yes, I saw it! In fact at about 21hs here I said "ok, joke's over" and put a message on the blog, but then saw the reddit post and thought "well, let's continue the joke for some hours more" :)
<asterite>
One coworker said to me: "But if you cache the generated method, then it might be that another guy checks out the project, runs the compiler and gets a totally different implementation! That's not very good"
<a5i>
lmao
<a5i>
gotta go now, night !
<asterite>
night!
panga has joined #crystal-lang
ponga has quit [Ping timeout: 248 seconds]
panga has quit [Remote host closed the connection]
ponga has joined #crystal-lang
weskinner_work has quit [Ping timeout: 265 seconds]
asterite has quit [Quit: Page closed]
ponga has quit [Remote host closed the connection]
e_dub has quit [Quit: ZZZzzz…]
e_dub has joined #crystal-lang
e_dub has quit [Client Quit]
e_dub has joined #crystal-lang
e_dub has left #crystal-lang ["It's a hard knock life"]
havenwood has quit []
ponga has joined #crystal-lang
sandelius has joined #crystal-lang
<sandelius>
can a class extends itself in crystal? e.g class << self
weskinner_work has joined #crystal-lang
<Cassyblanca>
Er
<Cassyblanca>
Why?
weskinner_work has quit [Ping timeout: 265 seconds]
<sandelius>
Cassyblanca useful when creating macros
<sandelius>
like in Ruby
canhtak has joined #crystal-lang
ponga has quit [Quit: Leaving...]
ponga has joined #crystal-lang
sandelius has quit [Quit: My Mac has gone to sleep. ZZZzzz…]
sandelius has joined #crystal-lang
sandelius has quit [Client Quit]
sandelius has joined #crystal-lang
canhtak has quit [Quit: canhtak]
bcardiff has joined #crystal-lang
leafybasil has joined #crystal-lang
ponga has quit [Quit: Leaving...]
ponga has joined #crystal-lang
sandelius has quit [Quit: My Mac has gone to sleep. ZZZzzz…]
sandelius has joined #crystal-lang
bcardiff has quit [Quit: Leaving.]
sandelius has quit [Quit: My Mac has gone to sleep. ZZZzzz…]
<jhass>
it works in Ruby because Ruby is more forgiving about giving the correct size
<jhass>
for example a = []; a[1000] = "1" works in Ruby
<jhass>
in Crystal it doesn't
<colorisco>
ok, understand
<jhass>
so that's also true for access
<jhass>
a = []; a[1] will give you nil in Ruby
<jhass>
in crystal you get the error you're seeing
<colorisco>
yes
sandelius has quit [Ping timeout: 250 seconds]
Exilor has joined #crystal-lang
canhtak has joined #crystal-lang
canhtak has quit [Client Quit]
Exilor has quit [Quit: Page closed]
weskinner_work has joined #crystal-lang
havenwood has joined #crystal-lang
canhtak has joined #crystal-lang
canhtak has quit [Quit: canhtak]
canhtak has joined #crystal-lang
odaeus has joined #crystal-lang
<odaeus>
Hi everyone, I was wondering if there is a nice way to access an adhoc hash from JSON data, e.g.: `a = JSON.parse(body); a[“query”][“results”][“rate”][0]` This seems to fail because at any point the key/value can be nil or some random type?
<ytti>
you could write your own MagicJsonHash
<ytti>
which returns NoObject object for key miss
<ytti>
which has method_missing which always returns NoObject
<ytti>
terrible hack, if you ask me
<ytti>
but then your above result would always either return the expected result, or NoObject
<ytti>
don't shoot me, please
<odaeus>
ytti: heh, thanks, the proper way is to use the JSON mapping function?
<ytti>
the proper way is to verify if the data is there, what you expect
<ytti>
you could also be a baller and do just
<ytti>
begin
sillesta has quit [Ping timeout: 248 seconds]
<ytti>
rate = a[“query”][“results”][“rate”][0]
<ytti>
rescue
<ytti>
rate = nil
<ytti>
end
<odaeus>
ytti: that’s funny, I didn’t consider that compiler checks could be rescued!
<a5i>
uhh, do we have a logo?
<ytti>
hohum, disclaimer, i've not tried this in crystal
<ytti>
but this isn't compile time at any rate
<odaeus>
ytti: I think it is, it’s the type checker saying that it doesn’t know what type the hash is..
<Cassyblanca>
a5i: The floating 3D crystal thing, I think
<ytti>
odaeus, oh right, i think your'e right
<ytti>
odaeus, yes, the type of rate wouldn't be known compiletime, and that is a problem
<a5i>
hu
<odaeus>
ytti: the error is: undefined method for ‘[]’ for Nil: rate = result[“query”][“results”… with it pointing out that result could be nil.
<ytti>
odaeus, yeah problem is that if result[“query”] returns nil
<ytti>
odaeus, you're calling nil['foo'] and nill does not have method []
<ytti>
odaeus, my suggestion seems to work just fine
<ytti>
k = {
<ytti>
"bar" => { "rate" => 42 },
<ytti>
}
<ytti>
rate = begin
<ytti>
k["foo"]["rate"]
<ytti>
rescue
<ytti>
nil
<ytti>
end
<ytti>
p rate
<ytti>
this prints 'nil'
<ytti>
if i change foo to bar, it prints '42'
<jhass>
meh, exceptions for control flow
<jhass>
you could .try
<jhass>
but that doesn't combine well with JSON::Any either I guess
<jhass>
er, JSON::Type it is
asterite has joined #crystal-lang
<asterite>
>> require "json"; json = JSON.parse(%<{"query": {"results": {"rate": 10}}}>); (((json as Hash)["query"] as Hash)["results"] as Hash)["rate"]
<DeBot>
asterite: 10
<asterite>
I'd use casts, like `as Hash`
<asterite>
You can see it's not that much typing, if it's just a casual json you need to parse. But, as others said, you can use json_mapping. It'll also be more efficient, and you'll probably get better errors
shama has joined #crystal-lang
<asterite>
By the way, you can use json_mapping and map only what you need, you don't need to map the whole object
<asterite>
ytti: unfortunately your suggestion doesn't work. It works in your example because it's a Hash of Hash, so you can index both, but when you parse JSON it's a union of many types