I meant to type test and receive nil. However I receive this instead:
ArgumentError: wrong number of arguments (0 for 2..3)
from (irb):15:in `test'
from (irb):15
from /usr/bin/irb:12:in `<main>'
Does anyone know if I need to fix something?
I think this may be what you're after:
2.3.0 :003 > puts 'test'
test
=> nil
Related
I'm trying this in irb, Ruby version 3:
3.0.0 :001 > num = 42
=> 42
3.0.0 :002 > num.is_a?(Integer) && num > 10
=> true
3.0.0 :003 > num.is_a? Integer && num > 10
Traceback (most recent call last):
5: from /Users/kai/.rvm/rubies/ruby-3.0.0/bin/irb:23:in `<main>'
4: from /Users/kai/.rvm/rubies/ruby-3.0.0/bin/irb:23:in `load'
3: from /Users/kai/.rvm/rubies/ruby-3.0.0/lib/ruby/gems/3.0.0/gems/irb-1.3.0/exe/irb:11:in `<top (required)>'
2: from (irb):3:in `<main>'
1: from (irb):3:in `is_a?'
TypeError (class or module required)
Why do I need the parentheses here:
v.is_a?(Integer)
?
How does the operator precedence work?
And why does Ruby generate such an arcane error message in this scenario?
With some digging I find the question answered previously here:
Ruby 'is_a?' class or module required (TypeError)
and the fundamental issue addressed here:
What's the precedence of method calls with and without parentheses?
which reference the official Ruby documentation:
https://ruby-doc.org/core-3.0.0/doc/syntax/precedence_rdoc.html
But I don't feel all my questions are answered. Anyone care to wrap all of this up cleanly?
Why do I need the parentheses here:
v.is_a?(Integer)
You need the parenthesis to specify what you are passing to the is_a? method. Without them, Ruby evaluates the entire Integer && num > 10 expression and passes that to the method.
How does the operator precedence work?
is_a? isn't an operator, so there's no precedence for it to follow. Expressions are evaluated before being passed to methods, that's all that's happening.
And why does Ruby generate such an arcane error message in this scenario?
Ruby is evaluating the expression Integer && num > 10 which gives true. This value is then being passed to num.is_a?. is_a? then gives a TypeError as it expects a class or module, not a boolean.
If I follow the following part of this article:
Figure out where a method was defined
object = Object.new
puts object.method(:blank?).source_location
=> ["/gems/activesupport-5.0.0.beta1/lib/active_support/core_ext/object/blank.rb", 14]
I should be able to find the definition of the blank? method, however when I try this code within irb with ruby 2.0.0 I get this error message:
➜ ~ irb
irb(main):001:0> object = Object.new
=> #<Object:0x007fc84882f088>
irb(main):002:0> puts object.method(:blank?).source_location
NameError: undefined method `blank?' for class `Object'
from (irb):2:in `method'
from (irb):2
from /usr/bin/irb:12:in `<main>'
Did I miss anything?
Thank you.
.blank? method does not exist for a Object type. I know for sure it exists for a String method if I include the active_support lib
irb(main):001:0> String.new.method(:blank?).source_location
=> ["/home/xeon/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/activesupport-4.2.8/lib/active_support/core_ext/object/blank.rb", 116]
If you include activesupport-5.0.0.beta1 then it will work for you. (Looking at the source path of the article you have posted)
I have a MiniTest like this:
describe Message do
describe "#is_getting_unavailable" do
let( :message ) { Message.new() }
it "should be false when user does not exist in the database" do
message.handle
assert_equal(false, message.is_getting_unavailable)
end
end
end
Running this gives me complaint from assert_equal:
Message::#is_getting_unavailable#test_0001_should be false when user does not exist in the database
ArgumentError: wrong number of arguments (2 for 0)
test/unit/message_test.rb:148:in `(root)'
org/jruby/RubyBasicObject.java:1703:in `__send__'
org/jruby/RubyKernel.java:2209:in `send'
org/jruby/RubyArray.java:1617:in `each'
org/jruby/RubyArray.java:1617:in `each'
I did not understand this, so I included the test (just before the call of assert_equal):
puts method(:assert_equal).inspect
puts method(:assert_equal).arity
puts method(:assert_equal).source_location.inspect
The output is:
#<Method: #<Class:0x1d1e394d>(Minitest::Assertions)#assert_equal>
-3
["/home/rjung/.rvm/gems/jruby-1.7.4/gems/minitest-5.0.6/lib/minitest/assertions.rb", 155]
So the method is correct, and the arity is correct. What's the issue here?
We also use rr, timecop. Any other questions, that could help me find a solution?
It took me a while, but I could narrow the problem down to this failing test:
require 'minitest/autorun'
describe 'Message' do
let( :message ) { Hash.new }
it "should not fail awkwardly" do
assert_equal false, message.nil?
end
end
The output of this test is
1) Error:
Message#test_0001_should not fail awkwardly:
ArgumentError: wrong number of arguments (2 for 0)
test/unit/message_test.rb:7:in `(root)'
org/jruby/RubyBasicObject.java:1703:in `__send__'
org/jruby/RubyKernel.java:2209:in `send'
org/jruby/RubyArray.java:1617:in `each'
org/jruby/RubyArray.java:1617:in `each'
So I filed a Bug for https://github.com/seattlerb/minitest/issues/343.
Minitest does have a method message that was overwritten, so don't use message as a variable.
What I still wonder is, why the stacktrace says the wrong number of arguments happen in message_test.rb:7, because the method that takes no arguments (message) is definitive called from somewhere else.
I want to know to what exception name I should refer to. I am getting invalid date. I checked the docs and I couldn't find it.
Begin
Date.new(day,month,year)
Rescue exceptionname
statements
I think you're looking for ArgumentError. Using irb:
> Date.new(2,-200, 3)
ArgumentError: invalid date
from (irb):11:in `new'
from (irb):11
so
begin
Date.new(2,-200, 3)
rescue ArgumentError
#your logic
end
I want to find out how to reproduce the following error in Ruby 1.9:
NoMethodError (undefined method `[]' for nil:NilClass):
It's my own interest. The following doesn't work for me:
a = nil
a[:key]
It produce the following error:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]
Your code works on my machine (submitting an answer since I can't format the comments):
⇨ irb
1.9.3p194 :001 > a = nil
=> nil
1.9.3p194 :002 > a[:key]
NoMethodError: undefined method `[]' for nil:NilClass
from (irb):2
from /Users/bjc/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'