brokkoli_origin has quit [Ping timeout: 248 seconds]
brokkoli_origin has joined #crystal-lang
SamantazFox has quit [Killed (NickServ (GHOST command used by SamantazFox_))]
SamantazFox_ has joined #crystal-lang
manveru2 has joined #crystal-lang
manveru has quit [Write error: Connection reset by peer]
manveru2 is now known as manveru
dannyAAM has quit [Quit: Will be back soon!]
dannyAAM has joined #crystal-lang
casionaut has joined #crystal-lang
casionaut has quit [Remote host closed the connection]
ih8u_ has joined #crystal-lang
alexherbo2 has joined #crystal-lang
alexherbo2 has quit [Remote host closed the connection]
dawids has joined #crystal-lang
<ih8u_>
how do i use variables in a def?
<ih8u_>
like `@a = 1 def change @a = 2 end`
<ih8u_>
`can’t use class variables at the top level`
jfontan has quit [Remote host closed the connection]
tsujp has quit [Write error: Connection reset by peer]
tsujp has joined #crystal-lang
jfontan has joined #crystal-lang
<ih8u_>
wtf
<ih8u_>
why is it telling me `Missing hash key`?
<ih8u_>
i’m trying to access a hash value
<ih8u_>
i know for a fact that the hash value is there
<ih8u_>
i can pull the hash value at the same level the hash is defined in
<ih8u_>
but i can’t use the hash from a def
<ih8u_>
and there are no globals?
<ih8u_>
and instance variables are not allowed at top level?
<ih8u_>
so i put the hash and method into a class, and marked the hash with @
<ih8u_>
but now in the def it’s telling me the key i want doesn’t exist
<ih8u_>
what on earth is going on?
<ih8u_>
okay, characters won’t match strings with size of 1
<ih8u_>
interesting choice
<ih8u_>
still curious about variable scope
<ih8u_>
docs aren’t very exhaustive on the topic
dawids has quit [Quit: Leaving]
alexherbo2 has joined #crystal-lang
<SamantazFox_>
ih8u_: @var is a class variable
<ih8u_>
right, an instance variable
<SamantazFox_>
so you can only use that inside a "class Foo; end" or "struct Bar ;end"
<SamantazFox_>
instance variable, yes, sorry*
<ih8u_>
wait, there are structs in crystal?
<SamantazFox_>
class variables are @@var
<SamantazFox_>
yes there are ^^
<ih8u_>
weird
<ih8u_>
so no “everything is an object” philosophy?
<ih8u_>
i still don’t understand how to use a variable inside a def at top level
<SamantazFox_>
var=1
<SamantazFox_>
def foo(new_value); var=new_value; end
<ih8u_>
so there is no way to reference a variable from within a def without passing one in as an argument?
<SamantazFox_>
ih8u_: you can! In my example 'var' is global and referenced inside of foo. foo has a local variable, the argument 'new_value' that is used to update the value of 'var'
<SamantazFox_>
var="old"
<SamantazFox_>
def foo; var="new"; end
<SamantazFox_>
that works too ^
<SamantazFox_>
when you call foo, 'var' is updated
<ih8u_>
this would be the relevant documentation, but as you can see, it is rather sparse
<ih8u_>
it confirms that “Local variables in the top-level scope are file-local and not visible inside method bodies”, but does not say how to create top-level variables that are visible in top-level method bodies
<ih8u_>
so am i to conclude that it is simply not possible?
<SamantazFox_>
it makes a global constant VAR_STORE that always points to a single and unique instance of MyVarStore
<SamantazFox_>
that class' instance variables can be accessed from anywhere, as they are declared using 'property', which automatically creates getter/setter methods for you
<ih8u_>
neat
<ih8u_>
but so, so weird
<ih8u_>
doesn’t that defeat the purpose of a constant?
<SamantazFox_>
the constant is still constant
<SamantazFox_>
it's just a reference (pointer) to an instance of that class
<SamantazFox_>
so the pointer never changes, but the referenced data does
<SamantazFox_>
also, in general, you'll rarely need globally scoped variables
alexherbo2 has quit [Remote host closed the connection]
ih8u_ has quit [Quit: ih8u_]
alexherbo2 has joined #crystal-lang
alexherbo2 has quit [Remote host closed the connection]