<kevinsjoberg>
havenwood: Oh, didn't realize that. Nice find! It's probably the cause. Let me try it out.
<kevinsjoberg>
You become blind after staring at the code for too long, haha.
<kevinsjoberg>
havenwood: It works! Nice catch. I even checked that all headers were strings before but somehow missed the fact that it was a symbol key.
<AndreYuhai>
Can I define different methods for each instance of a class?
<AndreYuhai>
Depending on the type
<AndreYuhai>
Because this way it gets defined for all the instances of the class
<AndreYuhai>
Or what would be a better approach if you have any suggestions?
dualfade has joined #ruby
<leftylink>
we talking instance methods? that should definitely be possible. but you wouldn't want to do self.class there if you wanted it to be an instance method
<leftylink>
&>> class A; def initialize(s); if s; def self.a; :a end end end end; [A.ne
<rubydoc>
stderr: -e:5: syntax error, unexpected `end', expecting ']'... check link for more (https://carc.in/#/r/9qeg)
<leftylink>
oh no
<leftylink>
&>> class A; def initialize(s); if s; def self.a; :a end end end end; [A.new(true).a, (A.new(false).a rescue :failed)]
roshanavand has quit [Remote host closed the connection]
roshanavand has joined #ruby
TCZ has quit [Quit: Leaving]
hiroaki has quit [Remote host closed the connection]
teardown has quit [Ping timeout: 258 seconds]
BSaboia has joined #ruby
impermanence has joined #ruby
zacts has joined #ruby
teardown has joined #ruby
BSaboia has quit [Quit: This computer has gone to sleep]
<apotheon>
AndreYuhai: I don't see a define_method call in there.
alfiemax has quit [Remote host closed the connection]
<AndreYuhai>
apotheon, oh sorry, define_attr_reader does that, here I pasted https://dpaste.org/0bsr. It works fine this way if what I am doing is not wrong.
alfiemax has joined #ruby
alfiemax has quit [Ping timeout: 265 seconds]
<havenwood>
AndreYuhai: Just catching up, but I don't like setting class methods from an instance. Have you considered using Singleton module?
<AndreYuhai>
havenwood, using define_singleton_method instead? Would it do the job?
<AndreYuhai>
havenwood, if I wanted those on the class is there any short way like that instead of doing self.class.define_method?
CrazyEddy has quit [Ping timeout: 240 seconds]
<havenwood>
AndreYuhai: It seems like a real anti-pattern for an instance to define a class method like that. What's the reason to do it? Why doesn't an instance method suffice for accessing instance variable state on the instance?
<AndreYuhai>
havenwood, oh I was just wondering. But if we just use define_method inside the class then that would be okay?
<havenwood>
AndreYuhai: Class methods should be shared by all. They're global to the class. Instance methods are particular for an instance. You *can* do anything, but it's good practice to follow convention.
<havenwood>
AndreYuhai: Yeah, it's fine to define methods on each instance. I'm curious if that's really the best thing to do?
alfiemax has quit [Ping timeout: 272 seconds]
drincruz has quit [Ping timeout: 265 seconds]
<havenwood>
AndreYuhai: Dynamically defining methods at runtime has performance implications. You may want to have a single method that exposes access to the settings.
<havenwood>
AndreYuhai: (It busts the method cache.)
<AndreYuhai>
havenwood, Basically I am initializing with a hash and setting all the keys and values as instance variables.
<AndreYuhai>
havenwood, oh that's probably why https://github.com/sferik/twitter was using an attrs hash instead of declaring them all as instance variables.
<havenwood>
AndreYuhai: You still get a nice interface, but you don't bust the method cache and less back-bending code. :)
supercoven has quit [Read error: Connection reset by peer]
<havenwood>
AndreYuhai: If you know what the possible keys are up front, consider making the struct up front.
<AndreYuhai>
havenwood, Oh I will probably switch to that haha. I will just have a hash with all the attributes and they may vary.
<havenwood>
Maybe there aren't method cache concerns? I need more coffee.
<havenwood>
AndreYuhai: Turning a hash of attributes into a struct might be nice.
<havenwood>
AndreYuhai: Or leave it as a hash, depending on how you want the interface to be.
<havenwood>
AndreYuhai: Making it a Struct might have advantages if they're called frequently. It has a nice method-chaining interface. You create one less object if you leave it a Hash. There are tradeoffs.
<AndreYuhai>
havenwood, I don't think it would be of concern haha but anyway it's always nice to learn new things.
chouhoulis has quit [Quit: Leaving...]
<havenwood>
AndreYuhai: Structs are nice. I look forward to the new Struct literal in 3.0 (assuming it gets merged).
<AndreYuhai>
havenwood, never used those in Ruby. :D
<AndreYuhai>
havenwood, what's the difference between writing a Struct and Class in Ruby?
<havenwood>
AndreYuhai: Not much.
chouhoulis has joined #ruby
cnsvc has joined #ruby
brianj has quit []
<AndreYuhai>
What's wrong with this one https://dpaste.org/kHAd ? A class inheriting from ActiveRecord::Base and I've overwritten the initialize method but I still get argument errors
<AndreYuhai>
And it throws different argument errors in different order depending on whether I called ActiveRecord::Base.establish_connection
<havenwood>
AndreYuhai: In your case, tailer `super` to what you need.
<havenwood>
AndreYuhai: And make the arguments to your initialize work with the anticipated ones.
<AndreYuhai>
havenwood, oh so I can not pass two objects at the same time? What I am trying to do is that I've got two different objects with different information and this table has all the columns from both objects.
<AndreYuhai>
havenwood, so this is how I would combine them but apparently not. :D
<havenwood>
AndreYuhai: Are these fields backed by a DB?
<havenwood>
AndreYuhai: This ^ talk shows how to make a Rails model that isn't backed by a DB. They can be backed by an API, a text file, something in memory, whatever as long as it is CRUD.
<havenwood>
AndreYuhai: It's hard to know how to advise without more context.
<havenwood>
AndreYuhai: Maybe if you say a bit more about what you're doing and why someone in the channel will have a best practice to recommend.
* havenwood
goes in search of more coffee
<AndreYuhai>
havenwood, So I've got a database table with all the necessary columns and the information comes from two different objects so I would like to combine them and create a DB row.
carbone5 has joined #ruby
zapata has quit [Ping timeout: 240 seconds]
<havenwood>
AndreYuhai: Do you know how to ask the two models what they're attributes are and how to merge them?
<havenwood>
their* attributes (yikes)
* havenwood
tries hard to wake up
<havenwood>
AndreYuhai: (That ^ talk actually mentions how and why it works.)
zapata has joined #ruby
<havenwood>
ActiveModel::Serializers
<AndreYuhai>
havenwood, I tried to call attribute_names.each and then checking whether any of the two objects respond to any of those attribute names.