havenwood changed the topic of #ruby to: Rules & more: https://ruby-community.com | Ruby 2.7.1, 2.6.6, 2.5.8: https://www.ruby-lang.org | Paste 4+ lines of text to https://dpaste.de/ and select Ruby as the language | Rails questions? Ask in #RubyOnRails | Books: https://goo.gl/wpGhoQ | Logs: https://irclog.whitequark.org/ruby | Can't talk? Register/identify with Nickserv first!
SeepingN has quit [Disconnected by services]
SeepingN_ has joined #ruby
braincrash has quit [Ping timeout: 256 seconds]
ramfjord has quit [Ping timeout: 272 seconds]
jud^ has joined #ruby
cliluw has quit [Ping timeout: 265 seconds]
cliluw has joined #ruby
cuerbot has joined #ruby
jud has quit [Ping timeout: 260 seconds]
elcuervo has quit [Ping timeout: 240 seconds]
swaggboi has quit [Quit: C-x C-c]
SeepingN_ has quit [Quit: The system is going down for reboot NOW!]
swaggboi has joined #ruby
lmat has quit [Ping timeout: 272 seconds]
lmat has joined #ruby
shokohsc has quit [Ping timeout: 240 seconds]
teardown has quit [Ping timeout: 240 seconds]
jud^ has quit [Quit: Leaving]
jud has joined #ruby
teardown has joined #ruby
kent\n has quit [Remote host closed the connection]
kent\n has joined #ruby
shokohsc has joined #ruby
cliluw has quit [Ping timeout: 240 seconds]
cliluw has joined #ruby
TCZ has quit [Quit: Leaving]
chouhoulis has quit [Remote host closed the connection]
postmodern has quit [Quit: Leaving]
srgupta has joined #ruby
DTZUZU has quit [Ping timeout: 246 seconds]
srgupta has quit [Client Quit]
DTZUZU has joined #ruby
bvdw has quit [Quit: The Lounge - https://thelounge.chat]
bvdw has joined #ruby
Rudd0 has quit [Remote host closed the connection]
BSaboia has joined #ruby
mozzarella has quit [Read error: Connection reset by peer]
mozzarella has joined #ruby
cthu| has quit [Ping timeout: 272 seconds]
BSaboia has quit [Quit: This computer has gone to sleep]
BSaboia has joined #ruby
CommunistWolf has quit [Ping timeout: 240 seconds]
gix has joined #ruby
CommunistWolf has joined #ruby
gix- has quit [Ping timeout: 260 seconds]
BSaboia has quit [Quit: This computer has gone to sleep]
adu has joined #ruby
wallace_mu has quit [Remote host closed the connection]
wallace_mu has joined #ruby
wallace_mu has quit [Ping timeout: 240 seconds]
_whitelogger has joined #ruby
zacts has joined #ruby
Rudd0 has joined #ruby
heavengin7 has joined #ruby
donofrio_ has quit [Remote host closed the connection]
heavengin7 has quit [Quit: Hi. If you wanna buy selected item on Amazonian pls click here > http://amazonian2.000webhostapp.com/]
Mia has joined #ruby
Mia has quit [Changing host]
Mia has joined #ruby
howdoi has quit [Quit: Connection closed for inactivity]
ChmEarl has quit [Quit: Leaving]
bocaneri has joined #ruby
jenrzzz has quit [Ping timeout: 260 seconds]
<kevinsjoberg> What's the difference between these two examples? https://gist.github.com/KevinSjoberg/87565191b232efb0d0030d35c69cff98. The first one does not work. The server returns something about an invalid header. The second one work, not raising any problems.
<kevinsjoberg> I thought those would be pretty much identical.
<havenwood> kevinsjoberg: You don't set the Accept header in the latter.
<havenwood> kevinsjoberg: I mean in the former*
<havenwood> oh, you do
<kevinsjoberg> havenwood: Yeah, the headers are identical.
<kevinsjoberg> The only thing I've changed is what API I use.
<havenwood> kevinsjoberg: "Content-Type" => "application/json"
<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.
<kevinsjoberg> Thanks. Appreciate the help.
<havenwood> You're welcome. Any time.
<havenwood> Took my eyes a minute to adjust, too.
adu has quit [Ping timeout: 240 seconds]
adu has joined #ruby
jenrzzz has joined #ruby
akem has joined #ruby
jenrzzz has quit [Ping timeout: 240 seconds]
adu has quit [Quit: adu]
brainfunnel has joined #ruby
supercoven has joined #ruby
supercoven has quit [Max SendQ exceeded]
supercoven has joined #ruby
supercoven has quit [Max SendQ exceeded]
supercoven has joined #ruby
rafadc has quit [Quit: ZNC - https://znc.in]
rafadc has joined #ruby
akem has quit [Ping timeout: 272 seconds]
alfiemax has joined #ruby
cliluw has quit [Ping timeout: 272 seconds]
_whitelogger has joined #ruby
fercell has joined #ruby
schne1der has joined #ruby
cd has quit [Quit: cd]
akem has joined #ruby
schne1der has quit [Ping timeout: 256 seconds]
jenrzzz has joined #ruby
alfiemax has quit [Ping timeout: 240 seconds]
TCZ has joined #ruby
jenrzzz has quit [Ping timeout: 260 seconds]
BSaboia has joined #ruby
fercell has quit [Quit: WeeChat 2.9]
imode has quit [Ping timeout: 240 seconds]
bocaneri has quit [Ping timeout: 256 seconds]
alfiemax has joined #ruby
alfiemax has quit [Ping timeout: 260 seconds]
bocaneri has joined #ruby
braincrash has joined #ruby
ellcs has joined #ruby
CrazyEddy has joined #ruby
brianj has joined #ruby
chouhoulis has joined #ruby
ur5us has joined #ruby
chouhoulis has quit [Ping timeout: 256 seconds]
jenrzzz has joined #ruby
jud has quit [Quit: Leaving]
jenrzzz has quit [Ping timeout: 246 seconds]
dualfade has quit [Ping timeout: 272 seconds]
dualfade has joined #ruby
kinduff has quit [Quit: gg]
kinduff4 has joined #ruby
dionysus69 has joined #ruby
ellcs has quit [Ping timeout: 260 seconds]
AndreYuhai has joined #ruby
Axy has joined #ruby
Axy has joined #ruby
Axy has quit [Changing host]
dualfade has quit [Quit: leaving]
<AndreYuhai> Hey there, how can I achieve this: https://dpaste.org/NSXX
<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)]
<rubydoc> # => [:a, :failed] (https://carc.in/#/r/9qeh)
elcuervo has joined #ruby
<AndreYuhai> leftylink, I figured I can do that with define_singleton_method as well, is it what it does under the hood? https://stackoverflow.com/q/3026943/4796762
cuerbot has quit [Ping timeout: 272 seconds]
<leftylink> I will need to review what that does
<leftylink> okay, it looks like it is so.
<AndreYuhai> leftylink, Alright, thank you :)
ur5us has quit [Ping timeout: 240 seconds]
oneeggeach has joined #ruby
TCZ has quit [Quit: Leaving]
oneeggeach has quit [Quit: Leaving...]
axsuul has quit [Ping timeout: 260 seconds]
axsuul has joined #ruby
dualfade has quit [Ping timeout: 256 seconds]
dualfade has joined #ruby
<AndreYuhai> Do I need to use the alias keyword after I define the method?
<AndreYuhai> in a class
Rudd0 has quit [Ping timeout: 260 seconds]
jenrzzz has joined #ruby
jenrzzz has quit [Ping timeout: 272 seconds]
Twemlow has joined #ruby
TCZ has joined #ruby
brainfunnel has quit [Quit: whoops]
BSaboia has quit [Quit: This computer has gone to sleep]
Twemlow has quit [Quit: Twemlow has now left the arena!]
<AndreYuhai> how do I define an alias for a method that's generated using define_method in the initializer of a class?
<AndreYuhai> inside the initializer probably. :D
schne1der has joined #ruby
wallace_mu has joined #ruby
<AndreYuhai> ^Nope: "NoMethodError: undefined method `alias_method' "
<AndreYuhai> ^self.class.send worked.
<apotheon> err
<apotheon> If your class is called Foo, you should be able to just use this: Foo.send :alias_method, :newname, :oldname
<apotheon> Where are you running executing self.class.send?
<apotheon> Did you reopen the class?
<apotheon> s/running //
<apotheon> redundant term
ChmEarl has joined #ruby
zacts has quit [Quit: leaving]
zacts has joined #ruby
imode has joined #ruby
DTZUZU has quit [Ping timeout: 272 seconds]
DTZUZU has joined #ruby
cd has joined #ruby
dionysus69 has quit [Remote host closed the connection]
alfiemax has joined #ruby
akem has quit [Ping timeout: 272 seconds]
jenrzzz has joined #ruby
akem has joined #ruby
zacts has quit [Quit: leaving]
jenrzzz has quit [Ping timeout: 240 seconds]
chouhoulis has joined #ruby
zacts has joined #ruby
akem_ has joined #ruby
akem has quit [Ping timeout: 272 seconds]
akem_ is now known as akem
Rudd0 has joined #ruby
hiroaki has joined #ruby
roshanavand has joined #ruby
roshanavand_ has joined #ruby
roshanavand has quit [Ping timeout: 256 seconds]
roshanavand_ is now known as roshanavand
<AndreYuhai> apotheon, inside initialize method
<AndreYuhai> apotheon, https://dpaste.org/hxqZ
aandrew has joined #ruby
zacts has quit [Quit: leaving]
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, checking it right now, thank you.
<havenwood> AndreYuhai: Singleton is one of the stdlib modules that Matz himself maintains.
<havenwood> AndreYuhai: It's for when you have one, and only one, instance of state.
<AndreYuhai> havenwood, but I might have multiple instances with different information.
<havenwood> AndreYuhai: Then use instance methods, not class methods.
<havenwood> AndreYuhai: That's exactly what instance methods are for, unlike class methods which should be shared by all instances of that class.
alfiemax has joined #ruby
<AndreYuhai> havenwood, so define_singleton_method instead of define_method on class?
<havenwood> AndreYuhai: Just plain old #define_method on the instance, rather than the class.
<havenwood> AndreYuhai: Expose instance variables with instance methods.
<AndreYuhai> havenwood, but I can't call define_method on the instance
<havenwood> AndreYuhai: Why not?
<AndreYuhai> havenwood, I get "undefined method"
<AndreYuhai> havenwood, and it says did you mean define_singleton_method
<havenwood> AndreYuhai: Sec, I'll show an example.
<AndreYuhai> havenwood, Alright.
BSaboia has joined #ruby
<AndreYuhai> havenwood, oh you call it on singleton_class. what is the difference between this (https://dpaste.org/hFf1) and yours? Besides brevity.
<AndreYuhai> I don't know whether they work the same.
<havenwood> AndreYuhai: Yeah, that's another way to properly define a singleton method for the instance,\ rather than defining a class method.
<havenwood> AndreYuhai: I prefer attr_reader if it's a pure getter.
<AndreYuhai> havenwood, yes that looks better I will use that. I can use alias in the same way, right?
<havenwood> AndreYuhai: Yup
<AndreYuhai> havenwood, Alright, thank you so much.
<havenwood> AndreYuhai: you're welcome
roshanavand has quit [Ping timeout: 260 seconds]
drincruz has joined #ruby
zacts has quit [Quit: leaving]
<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: Yes, for sure.
CrazyEddy has joined #ruby
<havenwood> AndreYuhai: Consider this example: https://gist.github.com/havenwood/211682a7852b77fa33e250e17cc91907
<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
houhoulis has joined #ruby
<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: Here's an example of overriding #initialize for a model: https://github.com/havenwood/example/blob/master/app/models/customer.rb#L54-L62
<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: Example code there ^ is from this talk: https://youtu.be/6h5TbKBnLws?t=23
<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.
<havenwood> AndreYuhai: https://youtu.be/6h5TbKBnLws?t=701
<havenwood> AndreYuhai: Consider merging the two attributes then keeping the key/value pairs that the third model also has.
zacts has joined #ruby
<AndreYuhai> havenwood, Oh alright, will watch the entire talk as well. Thank you!
<havenwood> AndreYuhai: [foo, bar].map(&:serializable_hash).reduce(:merge).transform_keys(&:to_sym).except(:id, :created_at, :updated_at)
<havenwood> AndreYuhai: ^ for example
<havenwood> AndreYuhai: Then create your third type with the remaining attributes.
braj has joined #ruby
<zacts> is ruboto strictly required for non-GUI android apps?
<zacts> or can I get by with plain jruby for this?
<zacts> well, non-GUI. I mean, I would need some GUI interface. hum..
<zacts> (sorry, I'm kind of brainstorming an idea)
<havenwood> zacts: You can include JRuby code in Java code, so in that sense, "yes."
<zacts> the main reason for avoiding ruboto is that it looks like the project isn't fully maintained.
<havenwood> zacts: If you run into issues, some folk in the #jruby channel have probably done it.
<zacts> ok
<havenwood> zacts: Or mruby on Android :)
<havenwood> Oh, JRuby, J
<zacts> oops
<zacts> I thought I was on #jruby
<zacts> sorry
<havenwood> No prob. JRuby is Ruby after all!
<zacts> havenwood: I meant to ask this on #jruby
<zacts> :-)
<havenwood> zacts: Ah, gotcha :)
Axy has quit [Read error: Connection reset by peer]
Mia has quit [Read error: Connection reset by peer]
zacts has quit [Ping timeout: 240 seconds]
carbone5 has quit [Quit: carbone5]
alfiemax has joined #ruby
alfiemax has quit [Ping timeout: 256 seconds]
greypack has quit [Quit: All your IRC are belong to ZNC]
akem has quit [Quit: Leaving]
leah2 has quit [Ping timeout: 272 seconds]
houhoulis has quit [Ping timeout: 246 seconds]
exkith has quit [Ping timeout: 256 seconds]
greypack has joined #ruby
akem has joined #ruby
zacts has joined #ruby
leah2 has joined #ruby
zacts has quit [Client Quit]
factormystic0 has quit [Quit: The Lounge - https://thelounge.chat]
factormystic0 has joined #ruby
zacts has joined #ruby
carbone5 has joined #ruby
akem has quit [Ping timeout: 240 seconds]
AndreYuhai has quit [Quit: Leaving]
factormystic0 has quit [Quit: The Lounge - https://thelounge.chat]
factormystic has joined #ruby
va5c0 has joined #ruby
greypack has quit [Read error: Connection reset by peer]
braincrash has quit [Ping timeout: 260 seconds]
greypack has joined #ruby
BSaboia has quit [Quit: This computer has gone to sleep]
va5c0 has quit [Quit: WeeChat 2.9]
va5c0 has joined #ruby
zacts has quit [Quit: leaving]
carbone5 has quit [Quit: carbone5]
va5c0 has quit [Quit: WeeChat 2.9]
va5c0 has joined #ruby
adu has joined #ruby
va5c0 has quit [Client Quit]
va5c0 has joined #ruby
va5c0 has quit [Client Quit]
va5c0 has joined #ruby
va5c0 has quit [Client Quit]