There is an "accept" example in the documentation. But i can't figure out what is the use of the "reject" method? Can somebody explain the sense and give some usage example?
I tried to read the source but no luck.
It simply undoes accept.
For example, using the snippet in documentation demonstrating accept:
op.accept(User) do |user_id|
find_user user_id.to_i
end
If after that we issue op.reject(User), User will no longer be accepted — as if op.accept(User) ... was never specified. If we try, the code would generate unsupported argument type: User (ArgumentError).
Related
I'm trying to get the call stacks of Ruby interpreter when it gets executed. For example, I have some ruby code in foo.rb and when I execute it with some options (if any to get the call stack) by $ruby foo.rb, then I would like to get which functions of interpreter get executed.
I found this (https://programmer.help/blogs/ruby-2.x-source-code-learning-an-overview-of-interpreters.html) nice article related to my questions and provides some idea of how I should approach, but I'm not quite sure how I should do it.
Basically, the article is saying that "Open OPT_CALL_THREADED_CODE switch in vm_opts.h header file when compiling Ruby
ruby_run_node call stack" to get the call stack, but I don't really get what does it mean by "Open OPT_CALL_THREADED_CODE".
I did look at the vm_opts.h, but it does not tell much.
This issue is a very specific topic, but if anyone has any idea how I can get the call stack in anyways or with the method the article is suggesting, please please help me out here.
Thank you for all your help!
Recently I came across a method, which looks like: add(1).(2).
It was on Code Wars. I just should take this 2 argumenst and make them equated 3. It's easy part, I think, but I have never seen any arguments like this(I am newbie).
Do you have a source where I could read about it? Or could you explain it?
If you need more information, I take this example from here: https://www.codewars.com/kata/539a0e4d85e3425cb0000a88/train/ruby
This is a shorthand notation to call a proc or lambda expression
proc.call(arg)
proc[arg]
proc.(arg)
proc::(arg)
Are all equivalent.
Actually this works with any object that responds to call since Ruby is duck-typed like that.
So I was looking through the Ruby standard library docs on how to read a file when I found File.exists?, with this fascinating and illuminating description:
exists?(p1)
(A Google search turns up a similar amount of information)
It's a real method, as verified by attempting to call it and not getting an error. Based on a couple of quick tests, it seems to do the same thing as File.exist?.
Is this correct, or does it have some other behavior that I missed?
Also, as a side-question, why isn't it documented at all? Is it deprecated?
According to the Ruby 2.2.0 doc, this method is deprecated:
exists?(file_name) → true or false
Deprecated method. Don’t use.
I have a simple test that it seems to have a different behavior depending on whether I use an explicit subject or not.
context "successful validation" do
subject(:invitation) {invitations(:emmet_invite)}
after do
invitation.send_voucher
end
it "calls hotel_booked?" do
invitation.should_receive(:hotel_booked?).and_return(true) #works
end
it {should_receive(:hotel_booked?).and_return(true)} #fails
end
What's wrong here?
See this github issue in rspec-mocks: https://github.com/rspec/rspec-mocks/issues/148
Quote:
Implicit subject was added to support one line expectations, but I don't believe it was ever intended for use with mock expectations. For a mock expectation to make any sense, there has to be some code in the example after it was set, so it doesn't really make sense to use for one liners.
So basically, you can't use should_receive with an implicit subject.
You can use subject to get at the implicit subject:
it { subject.should_receive(:hotel_booked?).and_return(true) }
though this is not as easy to read as your explicit example.
Similar to __callee__, is there something which returns the calling method? I realize there is the caller which I amble to strip the name of the caller method from but I am curious is there is a standard method for returning the name of the calling method without any other information along with it.
There is no such feature in MRI. But there are some alternatives.
In case you happen to use Rubinius, you can do this instead of parsing caller:
Rubinius::VM.backtrace(1, false).first.name
#=> :calling_method_name
You can also use a gem to parse the result of caller for you. It should work for any Ruby > 1.9.
The answer to this SO question describes how you can do some simple parsing yourself.
And finally, there appears to be work in progress on getting a feature like this into Ruby 2.0, although the relevant ticket has not been updated for a while.