<FromGitter>
<bew> @asterite ahahah well I thought about it, but I didn't do any class about programming languages and the few people I talked to who did it, weren't interested in the subject.. Too bad i guess..
<FromGitter>
<bew> but `gets` could also return `nil`, and `to_i` doesn't exist for nil (and doesn't make sense either)
<FromGitter>
<bew> that's the exact error, if you read it: `undefined method 'to_i' for Nil`
<FromGitter>
<bew> so you have to handle the case where it's nil, either by interrupting the program, or giving a default value, or somthing else
<FromGitter>
<bew> for exemple you could do `some_var = gets.try(&.to_i) || 0`
<FromGitter>
<bew> so here, if `gets` returns nil, it'll just be 0. And if `gets` returns a String, it'll call `to_i` on it, and return that result
<FromGitter>
<drinkmorewaters> Ok, so that's way over my head and way more complex than Ruby, thus i can't translate its use
<FromGitter>
<bew> try to experiment around that
<FromGitter>
<drinkmorewaters> That works for getting input, fails if a string is input, but i got no idea how it works compared to gets.chomp, or gets.to_i
<FromGitter>
<bew> yeah, you have to deal with types here! But I promise it'll help you a lot (in any languages) to consider the real types of things that you're manipulating, and what could happen in this or this case
<FromGitter>
<drinkmorewaters> I know it will help, but it's definitely a much harder learning curve. I don't know why but i'm finding working with types in go much simpler. But that's maybe because i have no idea what i'm doing.
<FromGitter>
<bew> I don't understand what you mean by "got no idea how it works compared to gets.chomp, or gets.to_i"
<FromGitter>
<drinkmorewaters> Compared to ruby's version of things. I don't understand this gets.try(&.to_i) || 0
<FromGitter>
<bew> yeah the type system in Crystal is quite different than go
<FromGitter>
<drinkmorewaters> gets method? .try another method? (&.to_i) "no idea || or 0
<FromGitter>
<bew> ok, basically `gets` can return 2 kind of things: `nil` if there is no input (e.g: the user did Ctrl-D in the terminal). Or it can return a `String` object with the input data
<FromGitter>
<bew> so we say that the return type of `gets` is `String | Nil` (meaning: the String OR Nil type)
<FromGitter>
<bew> side note: `Nil` is a type, `nil` is a value. And `nil` is the only possible value that has a `Nil` type (ok maybe I lost you here...)
<FromGitter>
<drinkmorewaters> Oh god
<FromGitter>
<drinkmorewaters> That last part is long lost on me for now
<FromGitter>
<bew> in Crystal you have to handle the case where a value can be `nil`, so if `gets` returns `nil` you'll probably want to do something different than if the user entered actual real data
<FromGitter>
<bew> yeah don't worry, it's not important for now ^^
<FromGitter>
<drinkmorewaters> I think i get the first part. Gets can return a 2 things, value of nil which is like 0 or nothing? and an object of String. But because we want an integer, we get the string and change it to an integer.
<FromGitter>
<bew> `nil` is like "nothing"
<FromGitter>
<drinkmorewaters> &. is what?
<FromGitter>
<drinkmorewaters> .try is a method?
<FromGitter>
<bew> yes, `try` is a method
<FromGitter>
<bew> do you know blocks (like in ruby) ?
<FromGitter>
<drinkmorewaters> Negative, know nothing. I will look up blocks in ruby. I am learning both in parallel, with Go just learn one concept and try to do it in another language to understand it's core principle. Might be the wrong way to do things.
<FromGitter>
<bew> which language(s) do you know (well or mid-well) ?
<FromGitter>
<drinkmorewaters> I started 4 days ago haha
<FromGitter>
<drinkmorewaters> Goes like this, Go i can read it and seems to be focus on simplicity, ruby is simpler than Crystal only because i can google anything, Crystal i am most keen on as it's a logical speed + expression.
<FromGitter>
<drinkmorewaters> @bew Ok i'll take a look, thanks.
jokke1 has quit [Ping timeout: 260 seconds]
<FromGitter>
<bew> so `try` is a method that exists for every types, so you can always call it, its reason of existence is that it'll execute the given block, only if the type it's called on is not `Nil`.
<FromGitter>
<bew> note: writing `something.try(&.to_i)` is the same as writing `something.try { |x| x.to_i }`, just shorter
<FromGitter>
<drinkmorewaters> The docs for me are written for professional programmers, so they're nearly useless to me at this point in time.
<FromGitter>
<bew> hmm yes we have some improvements to do about that, maybe start with Ruby's block then, and come back later?
jokke1 has joined #crystal-lang
<FromGitter>
<drinkmorewaters> Yeah, think that's becoming very clear to me at this point, that i'll need to focus on go or ruby or a similar other language with a lot more beginners material. Which i understand takes uptake and time.
alex`` has joined #crystal-lang
<FromGitter>
<sam0x17> @drinkmorewaters you will get a lot of mileage out of first googling "how to do x in ruby", and then use the ruby-specific x terminology to find how to do x in crystal. For every difference between ruby and crystal there is some issue in github where someone is asking about it
alex`` has quit [Ping timeout: 264 seconds]
renzhi has quit [Quit: WeeChat 2.1]
<FromGitter>
<drinkmorewaters> @sam0x17 I am currently doing that, trying to digest and translate as much as i can, but then things like user input there are no stackoverflow posts, blogs or tutorials so the help from @bew just isn't documented anywhere for inputs. Without his help i would not have ever figured out the solution. ⏎ ⏎ ```B = gets.try(&.to_i) || 0``` [https://gitter.im/crystal
<FromGitter>
<drinkmorewaters> I will post more of my questions in stack overflow as there needs to be more of these questions asked and answered to help lots of programmers. ⏎ ⏎ I have made a question for user input or gets.chomp in Crystal
<FromGitter>
<drinkmorewaters> @icyleaf if you're on stackoverflow please post, i am sure there are heaps of beginners trying to learn the same things but don't ask or give up.
renzhi has joined #crystal-lang
<FromGitter>
<drinkmorewaters> Lol who downvoted it? Very inclusive community
flaviodesousa has joined #crystal-lang
davic has quit [Excess Flood]
davic has joined #crystal-lang
alex`` has joined #crystal-lang
<FromGitter>
<sam0x17> @drinkmorewaters if you truly only want an int, a common pattern is to do a loop where you ask for the int, read whatever the user enters, try converting it to an int, and if you get a valid int break out of the loop, otherwise keep looping until you get a valid int. That will probably give you the behavior you want
<FromGitter>
<sam0x17> that approach is used in practically every programming language that deals with user input
<FromGitter>
<sam0x17> I also added that as an answer on the stackoverflow
<FromGitter>
<sam0x17> note: the `next` is not necessary it was due to some edits i made so you can remove that line
<FromGitter>
<sam0x17> looks like you are all set though because the non-rough approach the other guy did is basically this
<FromGitter>
<sam0x17> I canceled out the downvotes as well
alex`` has quit [Quit: WeeChat 2.1]
alex`` has joined #crystal-lang
literal has quit [Ping timeout: 256 seconds]
<FromGitter>
<drinkmorewaters> @sam0x17 Thanks for that, the downvotes pissed me off for a good hour there. Down voters on all answers doesn't help. As both your answer and @icyleaf answer were both super helpful in seperate ways!
literal has joined #crystal-lang
<FromGitter>
<drinkmorewaters> i also created https://gitter.im/crystal-beginners/Lobby if anyone cares to moderate or join it. As the main Crystal thread seems to annoy the 'senior' devs.
Ven`` has joined #crystal-lang
<FromGitter>
<straight-shoota> @drinkmorewaters I'm not a member of the core team but I'd advise against opening a new place for Crystal talk. This chat room is hardly over flooded and I don't think beginner questions annoy anyone here. And it's better to have people come here where there are many people around to answer questions.
<FromGitter>
<drinkmorewaters> @straight-shoota I've definitely noticed a sense of hostility in many places around Crystal (not massively), so my thought would be to take some of the focus off the main channel. Comparing this to the Elixir community it's strikingly different. I can close the chacnnel if it's better though. But i put a question on stack-overflow and the first thing is, no answers, down votes, then i get an answer and
<FromGitter>
... his answer, downvotes. I've seen frameworks around Crystal on Github see hostile answers in their issues threads. So it's bothering me.
<FromGitter>
<straight-shoota> Well, SO and Github issues isn't Gitter =)
<FromGitter>
<straight-shoota> I don't see such hostility, but if it is there, then that's a bigger problem then could be solved by opening a new beginners channel.
<FromGitter>
<drinkmorewaters> It's not there for you because you're not a beginner.
jokke1 has quit [Ping timeout: 256 seconds]
jokke1 has joined #crystal-lang
return0e_ has joined #crystal-lang
return0e has quit [Read error: Connection reset by peer]
Ven`` has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
Raimondii has joined #crystal-lang
Raimondi has quit [Ping timeout: 244 seconds]
Raimondii is now known as Raimondi
<FromGitter>
<icyleaf> Anyone was the beginner who learn Crystal at first, include me :)
<FromGitter>
<drinkmorewaters> @icyleaf 谢谢。Your answer was very helpful before.
<FromGitter>
<asterite> That last snippet you sent compiles file for me. But anyway, I don't have time for this, sorry
<FromGitter>
<j8r> BTW thank you 😄
<FromGitter>
<rishavs> @drinkmorewaters hang in there. I am an absolute beginner as well. But the community has been super helpful so far. There has never been the case that i was scared of asking something just because it seemed too basic. I just make sure that I have tried something by myself before I ask the question, so I am better able to understand the answers.
<FromGitter>
<bew> @jokke you can do `crystal build <your_spec_file.cr>`
rohitpaulk has joined #crystal-lang
<FromGitter>
<yorci> how to set time zone globally?
duane has joined #crystal-lang
Ven`` has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
<FromGitter>
<straight-shoota> jokke, `crystal spec` doesn't do anything special. It's the same as `crystal run` with globbing for spec files
<FromGitter>
<straight-shoota> all the specific runtime behaviour happens by `require "spec"`
<FromGitter>
<straight-shoota> @yorci programmatically, you can do `Time::Location.local = Time::Location.load("Europe/Berlin")`
<FromGitter>
<straight-shoota> otherwise `Time::Location.local` can also read `TZ` environment variable
Ven`` has joined #crystal-lang
Ven`` has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
<FromGitter>
<jwoertink> I love invidious. It's been my go-to site for a while now.
<FromGitter>
<jwoertink> What does the percent do in front of variables when in a macro? Like in `%objs = Array(self).new` or `def self.from_rs(%rs : ::DB::ResultSet)`
<FromGitter>
<kazzkiq> Just out of curiosity: If you have a collection of files with reusable functions across your app. Do you put them in a folder called "utils" or "helpers"?
rohitpaulk has quit [Ping timeout: 248 seconds]
<z64>
@kazzkiq i've seen "helpers" more often in the wild, as a semantic in specs ("spec helpers") or in MVC web frameworks
return0e has quit [Read error: Connection reset by peer]
return0e has joined #crystal-lang
Ven`` has joined #crystal-lang
duane has quit [Quit: leaving]
Ven`` has quit [Ping timeout: 248 seconds]
<FromGitter>
<bararchy> @straight-shoota thanks for putting us one step closer to multi-threading
<FromGitter>
<straight-shoota> Cancelling a fiber doesn't have much to do with multi-threading
<FromGitter>
<straight-shoota> And it might needs some tweaks for cancelling a fiber that is currently running
<FromGitter>
<talbergs> yeah, that's clear on scoping. Thou, why compiler expects the `s` might change after `if` in the `spawn` block? ⏎ ⏎ I guess, because it's reference. So any obj may become nil.
<FromGitter>
<talbergs> this clearly illustates the implied type is not passed into the declaration of that new fiber (spawn) instead original type is "passed in"
<FromGitter>
<talbergs> the reason why so, is still not clear... could really an object (at same address) become nil (with pointer?) and then at some moment of concurrency become object again?
<FromGitter>
<talbergs> Im sure not! But that reasoning seems to be implied by this behaviour.
<FromGitter>
<bew> well that example exactly shows you that it's possible
<FromGitter>
<bew> in the spawns block, the variable is nil
<FromGitter>
<talbergs> oh, ok then, moving on. ^^ thanx!
<FromGitter>
<bew> when you use a variable in a block, it is the same variable as outside of it, and as the compiler doesn't know when that block will be executed, the variable can be anything (its type is not impacted by the `if`)
<FromGitter>
<talbergs> Ok, I finally got it.
<FromGitter>
<talbergs> No, rly :)
<FromGitter>
<talbergs> Im not a computer scientist, so it's a common thing that `nil` is a value stored at address (reference/variable) instead of just representing a "no value and no address to look at"?
<FromGitter>
<bew> this has nothing to do with `nil`, I could have use a `String` and a `Int32`
<FromGitter>
<talbergs> So the address remains taken with a nil.
<FromGitter>
<talbergs> Ah yeah .. im changing subject.
<FromGitter>
<bew> ah it's another question?
<FromGitter>
<talbergs> ^^ just sayin
duane has joined #crystal-lang
duane has quit [Ping timeout: 268 seconds]
duane has joined #crystal-lang
Raimondii has joined #crystal-lang
Raimondi has quit [Ping timeout: 264 seconds]
Raimondii is now known as Raimondi
return0e has joined #crystal-lang
johndescs_ has joined #crystal-lang
johndescs has quit [Ping timeout: 260 seconds]
johndescs_ is now known as johndescs
duane has quit [Ping timeout: 240 seconds]
alex`` has quit [Ping timeout: 260 seconds]
wontruefree has quit [Quit: bye]
<FromGitter>
<Blacksmoke16> how would you define a custom `to_json` for an array of objects
<FromGitter>
<Blacksmoke16> vs just one obj
<crystal-gh>
[crystal] asterite opened pull request #6452: Compiler: missing `request_value` in `not` call (master...bug/6451-not-missing-request-value) https://git.io/fNuub