<havenwood>
ryouba: One nice thing about this way compared to your example is you end up creating fewer default procs (just one) in the case of multiple autovivifying Hashes.
<havenwood>
ryouba: Another nice thing is you end up only polluting Hash globally with only a single private constant rather than a public method.
<havenwood>
ryouba: (In your example, each new Hash also means a new Proc.)
cthulchu has quit [Ping timeout: 246 seconds]
<havenwood>
Another way to achieve the same is to use Hash#fetch and create a vivifying Hash as the fallback value.
Azure has joined #ruby
<havenwood>
So either have a Hash that knows its default value is a vivifying Hash or use Hash#fetch with a block for a new vivifying Hash as the default value.
<havenwood>
baweaver: Which begs the question, what's an autovivifying Array. :P
<havenwood>
?
<leftylink>
oh, hmm, I guess since the original one wasn't refined anyway then shrug. but to actually answer thee original thing... kinda sounds like this needs the Smalltalk `become:`
<baweaver>
havenwood: Now that's a fun question
<baweaver>
Not sure.
howdoi has joined #ruby
dviola has quit [Quit: WeeChat 2.6]
<havenwood>
baweaver: Challenge accepted.
<baweaver>
havenwood: a thought: Array-like methods applied to a wrapper class that intercepts and changes its render on iteration
involans has quit [Remote host closed the connection]
Fernando-Basso has quit [Remote host closed the connection]
r3m has quit [Quit: WeeChat 2.6-rc2]
ramfjord has quit [Ping timeout: 268 seconds]
greypack has quit [Ping timeout: 245 seconds]
r3m has joined #ruby
grilix has quit [Ping timeout: 276 seconds]
jrafanie has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
ap4y has joined #ruby
<teardown>
i wish set math worked on hashs
greypack has joined #ruby
henninb has joined #ruby
involans has joined #ruby
<teardown>
i suppose that im lazy
baojg has joined #ruby
johnny56 has joined #ruby
AJA4350 has quit [Quit: AJA4350]
<baweaver>
teardown: Sets exist in Ruby
<baweaver>
that, and hashes at least have some set operations
jacksoow_ has quit [Read error: Connection reset by peer]
jacksoow has joined #ruby
spacesuitdiver has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
Rudd0^ has quit [Ping timeout: 245 seconds]
weilawei has joined #ruby
<weilawei>
Dear rubyists, I have sinned xD I'm pretty sure that using included, include, class_exec, and Object.set_const to swap in test stubs is some violation of physics.
<weilawei>
It's ugly, but reasons.
<weilawei>
Out of my hands.
Exuma has joined #ruby
Rudd0 has joined #ruby
<weilawei>
Don't even bring up rspec. >< The whole business is distinctly smelly. The requirement came down to: because Reasons, the test stub class must be swapped into the scope of another class just before an instance method is called on it.. that calls a class method on the class swapped in
cycloni89 has quit [Quit: -a- IRC for Android 2.1.54]
howdoi has quit [Quit: Connection closed for inactivity]
grilix has joined #ruby
Rapture has joined #ruby
deathwishdave has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
leitz has joined #ruby
lefterisnik has joined #ruby
GodFather has joined #ruby
<lefterisnik>
hi guys, does anybody know to stub the `define_method` only when it takes a specific argument with mocha?
leitz has left #ruby [#ruby]
FrankDW has joined #ruby
deathwishdave has joined #ruby
spacesuitdiver has joined #ruby
DaRock has quit [Ping timeout: 240 seconds]
FastJack has joined #ruby
xtolid has joined #ruby
FastJack has quit [Read error: Connection reset by peer]
openCircuit__ has joined #ruby
<adam12>
lefterisnik: What have you tried? and can you share some code?
yqt has joined #ruby
<lefterisnik>
adam12: I have tried this `Module.stubs(:define_method).with(:status).returns("incomplete")`
krawchyk has joined #ruby
<adam12>
lefterisnik: It's an interesting stub. Why on Module?
<lefterisnik>
and it seems that it works but the problem is that the define_method is called many times by different gems and I am getting `Unexpected invocation: #<Class:0x5586b13d5e90>.define_method(:default_url_options)`
openCircuit__ has quit [Ping timeout: 276 seconds]
<adam12>
lefterisnik: It makes sense. And it's likely painful on purpose.
<lefterisnik>
adam12 because the `define_method` is defined the `class Module`
<adam12>
lefterisnik: What are you trying to do, at a higher level?
<adam12>
lefterisnik: define_method is setting up a method on the singleton. Do you not have access to that singleton for stubbing directly on it?
<adam12>
Actually, it's not on the singleton, but that doesn't matter I guess.
<lefterisnik>
adam12: you might be right about that but again that won't fix completely the problem. It will reduce the unexpected invocations only to the singleton calls
<adam12>
lefterisnik: Sure, but why can't you stub on the object/class it's defining? Why go all the way up to Module? Unless i'm missing something.
<lefterisnik>
adam12 I tried on the class level but it doesn't work because the class registers its methods with the define_method
<adam12>
lefterisnik: I'm not sure you'll be able to do what you want with Mocha. You could probably do it manually with some sort of monkeypatch.
<lefterisnik>
`Stripe::Subscription.any_instance.stubs(:status).returns("incomplete")` doesn't work because the class `Subscription` (if I have understood right the structure of that class) doesn't have a method with name `status`. It's defined dynamically using the `define_method`
<adam12>
lefterisnik: I think this is an indication that you're doing it at the wrong integration point tho.
<adam12>
lefterisnik: Ah yes. I think Stripe uses method_missing a bunch.
<lefterisnik>
yeap
Intelo has quit [Remote host closed the connection]
hutch has quit [Ping timeout: 264 seconds]
<adam12>
lefterisnik: do you have access to the subscription somewhere? perhaps you could just `def subscription.status; "incomplete"; end`
<lefterisnik>
adam12: You mentioned `You could probably do it manually with some sort of monkeypatch.` can you point me to guide?
<lefterisnik>
adam12: You mentioned `You could probably do it manually with some sort of monkeypatch.` can you point me to a guide?
<lefterisnik>
adam12: I don't have access to the subscription object. The subscription object is created by that function that I want to test
<adam12>
lefterisnik: Not sure I have one, but there's two steps to it. You want to modify `define_method` on Module. So at test run (presuming you're doing this in tests), you safely modify the define_method method, and then on teardown, you put the define_method back the way it was.
<adam12>
lefterisnik: Possibly using prepend or alias_method. You want to be able to revert the alias/monkey patch, which is what Mocha and/or minitest/mock does.
<adam12>
lefterisnik: Can you share the method you're testing that creates the subscription? Maybe there's an opportunity there.
<ruby[bot]>
lefterisnik: we in #ruby do not like pastebin.com, it loads slowly for most, has ads which are distracting and has terrible formatting. Please use https://gist.github.com
<adam12>
lefterisnik: How are you testing do_sign_up? Is this an integration test where you're making a web request to test?
<adam12>
lefterisnik: And where to you care about status in here?
krawchyk has quit [Quit: krawchyk]
<lefterisnik>
adam12: updated gist with the test
<rintaun>
I'm trying to understand why Kernel singleton methods are available everywhere, even though Kernel's singleton class isn't in the ancestor chain anywhere... can anyone explain that for me? I also asked about this on SO: https://stackoverflow.com/q/57872724/479561
krawchyk has joined #ruby
Rapture has joined #ruby
<adam12>
lefterisnik: I'd say that Integrations::Stripe.create_or_end_trial_on_subscription should be your best bet for stubbing something.
<adam12>
lefterisnik: Is that class/module yours or from a gem?
<lefterisnik>
my module
<lefterisnik>
adam12: Also I added another comment on my gist
<lefterisnik>
adam12: might help
<adam12>
lefterisnik: Ah I see.
yqt has quit [Ping timeout: 246 seconds]
<adam12>
lefterisnik: Does the check_sca method retrieve the sub again or use the one you passed in?
<lefterisnik>
adam12: uses the passed in subscription
<adam12>
lefterisnik: This is somewhat challenging because you're going through a web request to handle this. A lot of times this is more convenient in some sort of operation/transaction/whatever the cool kids are calling them now, with the Rails controller just being a dumb layer that receives data and returns data.
yqt has joined #ruby
<adam12>
lefterisnik: Just for clarity, can you show me the create_or_end_trail__on_subscription method? I think that's going to be where you want to end up.
poontangmessiah has quit [Remote host closed the connection]
absolutejam3 has quit [Ping timeout: 276 seconds]
<lefterisnik>
adam12: updated
MrCrackPot has joined #ruby
yqt has quit [Ping timeout: 245 seconds]
MrCrackPot has quit [Max SendQ exceeded]
MrCrackPot has joined #ruby
MrCrackPot has quit [Max SendQ exceeded]
cthulchu has joined #ruby
MrCrackPot has joined #ruby
hightower2 has joined #ruby
MrCrackPot has quit [Remote host closed the connection]
MrCrackPot has joined #ruby
banisterfiend has quit [Ping timeout: 240 seconds]
Fischmiep has quit [Ping timeout: 245 seconds]
<adam12>
lefterisnik: I'm still contemplating what I might suggest, but out of curiosity, can you just try monkeypatching that method and see what happens?
<adam12>
lefterisnik: Let me help you with the monkeypatch.
MrCrackPot has quit [Remote host closed the connection]
MrCrackPot has joined #ruby
banister_ has quit [Ping timeout: 244 seconds]
TomyWork has quit [Ping timeout: 246 seconds]
<lefterisnik>
cool thanks adam12, let me try that out
banisterfiend has quit [Ping timeout: 276 seconds]
involans has joined #ruby
Guest11997 has joined #ruby
banisterfiend has joined #ruby
<adam12>
lefterisnik: I wouldn't be surprised if other tests broke because of it. You'd need a way to clean up somehow. There's ways to do it, which might be an exercise to the reader.
hutch has joined #ruby
emptyflask has joined #ruby
<lefterisnik>
adam12: I am getting an error ` module definition in method body (SyntaxError)`
banisterfiend has quit [Ping timeout: 276 seconds]
hightower2 has joined #ruby
oetjenj has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
oetjenj has joined #ruby
oetjenj has quit [Client Quit]
rippa has quit [Quit: {#`%${%&`+'${`%&NO CARRIER]
oetjenj has joined #ruby
oetjenj has quit [Client Quit]
oetjenj has joined #ruby
oetjenj has quit [Client Quit]
oetjenj has joined #ruby
ap4y has joined #ruby
oetjenj has quit [Client Quit]
oetjenj has joined #ruby
oetjenj has quit [Client Quit]
oetjenj has joined #ruby
iNs has quit [Ping timeout: 260 seconds]
oetjenj has quit [Client Quit]
iNs has joined #ruby
oetjenj has joined #ruby
tdy has quit [Ping timeout: 244 seconds]
oetjenj has quit [Client Quit]
ellcs1 has joined #ruby
catbusters has quit [Quit: Connection closed for inactivity]
ur5us has joined #ruby
GodFather has quit [Ping timeout: 246 seconds]
banister_ has quit [Ping timeout: 268 seconds]
Guest11997 is now known as davidw
davidw has quit [Changing host]
davidw has joined #ruby
<davidw>
Given a module like Foo::Bar - is there a method that will return 'Bar' (besides hacking at the string name with regexp or split or something) ?
oetjenj has joined #ruby
grilix has quit [Ping timeout: 268 seconds]
dionysus69 has quit [Remote host closed the connection]
Ven`` has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
jrafanie has joined #ruby
poontangmessiah has joined #ruby
absolutejam4 has quit [Ping timeout: 246 seconds]
<havenwood>
davidw: Assuming you're not in Rails?
<davidw>
havenwood, I'd be curious to see both a Rails and non Rails solution. I ended up hacking the strings up... it works, but isn't very nice looking
<havenwood>
davidw: The rails solution is: Foo::Bar.name.demodulize