Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 08:23
    caspiano edited #10531
  • Dec 08 23:04
    HertzDevil edited #12623
  • Dec 08 22:57
    Blacksmoke16 edited #9745
  • Dec 08 22:57
    Blacksmoke16 labeled #9745
  • Dec 08 22:57
    Blacksmoke16 labeled #9745
  • Dec 08 22:54
    HertzDevil labeled #12828
  • Dec 08 22:54
    HertzDevil labeled #12828
  • Dec 08 22:54
    HertzDevil opened #12828
  • Dec 08 22:34
    straight-shoota edited #12827
  • Dec 08 22:32
    straight-shoota edited #12827
  • Dec 08 22:29
    straight-shoota labeled #12827
  • Dec 08 22:29
    straight-shoota labeled #12827
  • Dec 08 22:29
    straight-shoota opened #12827
  • Dec 08 22:15
    straight-shoota closed #12814
  • Dec 08 17:12
    straight-shoota assigned #12814
  • Dec 08 17:12
    straight-shoota assigned #12813
  • Dec 08 17:12
    straight-shoota assigned #12809
  • Dec 08 17:12
    straight-shoota assigned #12802
  • Dec 08 17:12
    straight-shoota assigned #12797
  • Dec 08 17:12
    straight-shoota assigned #12664
George Dietrich
@Blacksmoke16
Inserts and updates can just use prepared statements so really np
mfiano
@mjfiano:matrix.org
[m]
Perhaps a dumb question, but is there any best practice as to when to use x, @x, or self.x to refer to an instance variable in an instance method? I seem to be able to use all 3 with the same effect.
mfiano
@mjfiano:matrix.org
[m]
My guess is that @x is the only "safe" thing that will always resolve to an ivar, even if there is another local var or method named x...so is there no reason to use the other 2, other than to make things less explicit and confusing? :)
erdnaxeli
@erdnaxeli:cervoi.se
[m]
"x" resolves to "self.x", unless you have a local var named "x"
"self.x" is a method (you are probably using the macro "getter") that returns "@x"
and "@x" is the actual instance var
so the best practice if you want to access to the instance var is to use the instance var "@x" :)
From IRC (bridge bot)
@FromIRC
<hightower2> if at some point later you replace access to an instance var with a getter method, then you'll need to replace @x with x or self.x
<hightower2> sometimes I wish that instead of making local vars be unprefixed, the lang designers chose to prefix local vars... so then the scope/lookup of all unprefixed vars like 'x' or 'var1' would be very uniform regardless of whether it was a property or method
<hightower2> well, I guess if one really wanted to do so, one could stick to a rule of always prefixing local vars with something, manually...
George Dietrich
@Blacksmoke16
i always use self. when wanting to invoke a method, and use @x within the type
this way its always clear what is a method call and what isnt
however, a good usecase for using self.x is if you have customer logic in the getter and/or are using a lazily initialized ivar
From IRC (bridge bot)
@FromIRC
<hightower2> yes, that works, but what I find silly then is that we use self.x for vars and x for methods...
George Dietrich
@Blacksmoke16
can also use x and x()
From IRC (bridge bot)
@FromIRC
<hightower2> sure, but this just continues the practice of non-uniform invocation. I would actually prefer the invocation to always be 'x', regardless of whether it is an instance var accessor or instance method
Daniel Worrall
@Daniel-Worrall
If you have a lazy getter, I'd use x, but you could use self.x to be verbose too
Oh, glossed over. lazy was already mentioned
mfiano
@mjfiano:matrix.org
[m]
Makes sense, thanks all
Does crystal have anything like...I'm not sure what it's called, but in Nim, you can assign a slice to a slice of a variable...like:
var x = [1, 2, 3, 4, 5]
var y = [6, 7, 8, 9, 10]

x[1..2] = y[1..2] # x => [1, 7, 8, 4, 5]
Ary Borenszweig
@asterite
If you remove var from that code, it already works in Crystal. It's nothing special in the language, just a method call
mfiano
@mjfiano:matrix.org
[m]
Oh nice. It seems you can even grow shrink it with unsymmetrical ranges
Thanks @asterite
mfiano
@mjfiano:matrix.org
[m]
Are there functions for extracting the sign and fractional components of floating-point numbers? In some languages you have something like sign and fract:
sign(42.0) # 1.0
sign (-42.0) # -1.0
sign (0.0) # 0.0
fract(42.123) # 0.123
I suppose line 3 can be implemented differently if there is a distinction between IEEE-754 positive and negative zero
last one could just be like value - (value.floor) probably?
mfiano
@mjfiano:matrix.org
[m]
Of course I only went down the type tower API from Float64 to Float
George Dietrich
@Blacksmoke16
inherited methods are also listed towards the bottom
image.png
mfiano
@mjfiano:matrix.org
[m]
I suppose I was searching for the wrong half, fract 😆
mfiano
@mjfiano:matrix.org
[m]
If I have a type with value types (floats) for ivars, is it conventional to call a method which copies it clone or dup? In this case there is nothing to traverse for a deep copy, but I see some projects use clone in this case, while others use dup, so I'm not sure if there is a consensus.
George Dietrich
@Blacksmoke16
i imagine dup would be ok if the only thing you need is a new type instance
Ary Borenszweig
@asterite
dup for shallow copy, clone for deep copy
mfiano
@mjfiano:matrix.org
[m]
Yes, in this case a deep copy is a shallow copy
Maybe I worded it poorly. I was stating that either would be semantically correct for this particular example, and asking if there was a general agreement of which takes preference.
Ary Borenszweig
@asterite
I think there's no need to implement clone in this case given that dup already covers that use case
mfiano
@mjfiano:matrix.org
[m]
Thanks
Taupiqueur
@alexherbo2
how to convert the following yaml { a: ["1", "2"], b: true } to a MyClass with items being ItemA, ItemB?
the real use case is to parse a scope.yml having the properties roots, paths, etc. in a Scope class, I'd like to avoid flow control to check the filters as scope.roots, scope.paths, to something like scope.filters having their own class
I have Scope with property roots and property paths to pull the data from the file, but what I want when creating the instance is more something like a property filters doing when having roots keys, Root.new(value), etc.
so when filtering, I can just do scope.filters.all? { |filter| filter.evaluate_with(path) }
Taupiqueur
@alexherbo2
not sure if I actually need an intermediate class to pull the data fromscope.ymlother than Scope
like ScopeFile and using it to create the new Scope with the correct filters
George Dietrich
@Blacksmoke16
regarding your first question you can use a custom converter on that property
that could do like Item.new pull.read_string and return that array
Taupiqueur
@alexherbo2
currently I have a monolithic Scope class with hard-coded properties, it is not necessarily a problem as my scope will probably not have other props than roots, paths, extensions, but I realized there was too much logic in my Scope#evaluate method
I have to re-check if the props are nil, what they are supposed to do etc.