How to Tell a Ruby Method to Expect a Specific Parameter Type

Can I tell a Ruby method to expect a specific parameter type?

No, you can't. You can only do what you're already doing: check the type yourself.

Checking the type of a method parameter

Use the Kernel#Integer method to convert the input before using it. It will raise an ArgumentError when the input could not be converted to an integer in any reasonable fashion.

def my_method(number)
number = Integer(number)
# do something with number, which is now guaranteed to be an integer
end

I recommend Avdi Grimm's new book Confident Ruby for more insight into this.

Determining type of an object in ruby

The proper way to determine the "type" of an object, which is a wobbly term in the Ruby world, is to call object.class.

Since classes can inherit from other classes, if you want to determine if an object is "of a particular type" you might call object.is_a?(ClassName) to see if object is of type ClassName or derived from it.

Normally type checking is not done in Ruby, but instead objects are assessed based on their ability to respond to particular methods, commonly called "Duck typing". In other words, if it responds to the methods you want, there's no reason to be particular about the type.

For example, object.is_a?(String) is too rigid since another class might implement methods that convert it into a string, or make it behave identically to how String behaves. object.respond_to?(:to_s) would be a better way to test that the object in question does what you want.

How to test if parameters exist in rails

You want has_key?:

if(params.has_key?(:one) && params.has_key?(:two))

Just checking if(params[:one]) will get fooled by a "there but nil" and "there but false" value and you're asking about existence. You might need to differentiate:

  • Not there at all.
  • There but nil.
  • There but false.
  • There but an empty string.

as well. Hard to say without more details of your precise situation.

How to make Ruby methods show what types they expect

after Ruby2.1 I think the following solution is nice.

def find_by(name:)
# and you can check if type of name is what you expect as you need.
# But that is not duck-typingy.
end

How do Ruby programmers do type checking?

Ruby is, of course, dynamically typed.

Thus the method documentation determines the type contract; the type-information is moved from the formal type-system to the [informal type specification in the] method documentation. I mix generalities like "acts like an array" and specifics such as "is a string". The caller should only expect to work with the stated types.

If the caller violates this contract then anything can happen. The method need not worry: it was used incorrectly.

In light of the above, I avoid checking for a specific type and avoid trying to create overloads with such behavior.

Unit-tests can help ensure that the contract works for expected data.

Expect an instance of a specific class as a keyword argument

You can use hash_including together with instance_of matchers (instead of what is written as an_instance in your example):

hash_including(environment: instance_of(Puppet::Node::Environment))

How to test if a method call with arguments happened in RSpec

I've found the way to get this working!

Basically, I was doing everything right apart from doing the 'post'
call in the right place.

The post call have to be done after the:

  expect(controller).to receive(:track_event).with('User Action')

i.e. the code below will work without any problems:

  expect(controller).to receive(:track_event).with('User Action')
post :new_user_action_post

and not before.

I hope will help someone! ;)

D.



Related Topics



Leave a reply



Submit