Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Piotr Solnica
@solnic
Reform should handle it then somehow
Michał Pietrus
@blelump
yup
Tim Riley
@timriley
Interesting Ruby behaviour I encountered today:
Thing = Struct.new(:thing)
# => Thing

Thing.new("hi")
# => #<struct Thing thing="hi">

Array(Thing.new("hi"))
# => ["hi"]
# ಠ_ಠ - my struct object got totally stripped away
I see that this is probably due to Struct#to_a
A(nother) reason it’s probably better to define your own classes for holding small values etc. when you need them.
Piotr Solnica
@solnic
@timriley Struct is a pretty bad API tbf
it’s a) slow!!! b) comes with gotchas like you just mentioned c) splat args in constructor are meh
I initially implemented Message in dry-v using Struct then it turned out to be 2 x slower to instantiate than a custom class with a constructor and attr readers….
Tim Riley
@timriley
wow!
Piotr Solnica
@solnic
I was surprised too, I used to think Struct is a very cheap object
Piotr Solnica
@solnic
@timriley dry-stdlib? O_o
Piotr Solnica
@solnic
@backus do you think we could wrap up dry-struct?
I have a growing need for it in my current project at work…I start having usecases similar to yours, so I’m motivated to help with that
furthermore, we’re gonna have important improvements in dry-types so would be nice to release it, and in master Struct is no more…:)
Nikita Shilnikov
@flash-gordon
@solnic dry-rb/dry-struct#8
Piotr Solnica
@solnic
can somebody remind me why we’re using gemnasium? O_o
Piotr Solnica
@solnic
dry-rb/dry-validation#241 #help-wanted
Igor
@svilenkov
Does anyone can see why am I getting a ArgumentError: wrong number of arguments (1 for 0) for initialize the error points to the line def initialize
class IsobusExportAdapter
  extend Dry::Initializer::Mixin

  attr_reader :gis_api_params

  option :application_map_id
  option :field_id
  option :strategy_bundle_id
  option :worker

  def initialize
  end
end
IsobusExportAdapter.new(application_map_id: 43, field_id: field.id, strategy_bundle_id: bundle.id, worker: user)
Piotr Solnica
@solnic
@keeperhood initialize from Mixin expects arguments
you can’t override it w/o args
Igor
@svilenkov
Can I use *args ?
Piotr Solnica
@solnic
yes
def initialize(*args); super; # your-stuff; end
Igor
@svilenkov
would it work without ; ?
def initialize(*args)
  super
end
gives me
     Failure/Error: super

     ArgumentError:
       wrong number of arguments (1 for 0)
but without super but with just *args doesn't fail, but it doesn't call the mixin initializer
Can you call super with .send or __send__ somehow?
Ralf Schmitz Bongiolo
@mrbongiolo
@keeperhood do you really need to override initialize? are you setting other variables there or something like that?
Igor
@svilenkov
Yeah setting more complex variables, based on some db ids
internal objects
Ralf Schmitz Bongiolo
@mrbongiolo
since you're using options, try this:
def initialize(**args)
  super
  # your stuff
end

# or just define it with the correct options
def initialize(application_map_id:, field_id:, strategy_bundle_id:, worker:)
end
Igor
@svilenkov
I think this works normally without the mixin, the mixin somehow disallows the same ruby behavior
same error with **args
superis not the same in that scope somehow
hence the error wrong number of arguments (1 for 0) for the line the super is being called
Ralf Schmitz Bongiolo
@mrbongiolo
maybe, try to call super with the parameters defined to see what happens
Igor
@svilenkov
wrong number of arguments (4 for 0)
I don't know how the mixin redefines the initialiser, but looks like super is not picking up that
having a after_initialize or some kind of callback would be nice to have
or on_initialize
Ralf Schmitz Bongiolo
@mrbongiolo
it has something like an __after_initialize__ but I guess that's used internally, but maybe you could use super with it
tbh I haven't used initializer yet, but the ability to use super is something that should work
or then as you suggested some kind of hooks into it
Igor
@svilenkov
looks like defining a private method __after_initialize__ works
Ralf Schmitz Bongiolo
@mrbongiolo
try to get a failling spec and add it as an issue there, then we can take a better look at it
Igor
@svilenkov
This works definitely
private

  def __after_initialize__
    # custom initializer code
    super
  end
Piotr Solnica
@solnic
I wouldn’t recommend hiding complex object building inside a constructor
I suspect you’d like to hit the db in that overridden constructor?