0

How to document a YARD parameter that could be of any type possible?

Is there a convention for it?

This is similar to Any type in TypeScript

# @param [?] var1
def create_foo(var1)
  # var1 could be anything
end
BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91

1 Answers1

3

Since almost every object in Ruby inherits behavior from the class Object, # @param [Object] var1 seems like an appropriate choice.

A few examples from rubygems/rubygems and lostisland/faraday

Stefan
  • 109,145
  • 14
  • 143
  • 218
Yakov
  • 3,033
  • 1
  • 11
  • 22
  • And of course, YARD itself is document using YARD. An example for `[Object]` can be found in [`Options#[]=`](https://github.com/lsegal/yard/blob/e21c9323bba88c841a917d0531281b90f27f2378/lib/yard/options.rb#L98) – Stefan Jun 03 '22 at 13:51
  • `Kernel` doesn't inherit behavior from the `Object` class. Having `nil` as an option `# @param [Object, nil] var1` might be important for RubyMine users, it distinguishes nilable and not-nilable method arguments, but couldn't recognize that `nil` is `NilClass` which is inherited from `Object`. – Yakov Jun 03 '22 at 13:51
  • I've reverted the removal of "almost" as instances of [`BasicObject`](https://ruby-doc.org/core-3.1.2/BasicObject.html) do _not_ inherit from `Object`. – Stefan Jun 03 '22 at 14:40
  • 2
    @Stefan While the mentioned objects `Kernel` (Module) and `BasicObject` (Class) do not directly "inherit" from `Object` they are `Object`s because everything is an `Object`. e.g. `BasicObject.is_a?(Object) #=> true`, `Kernel.is_a?(Object) #=> true`. Note the anonymous eigenclass (metaclass, singleton_class) of `BasicObject` does include `Object` in its ancestry `BasicObject.singleton_class.ancestors #=> [#, Class, Module, Object, PP::ObjectMixin, Kernel, BasicObject]`. Possibly for this reason documenting as `[Object]` is widely used and accepted. – engineersmnky Jun 03 '22 at 15:05
  • @Yakov all arguments are "nilable" because arguments do not have a type. Not all methods handle `nil` as an argument but most methods do not handle a lot of objects as arguments. – engineersmnky Jun 03 '22 at 15:18
  • 1
    @engineersmnky that's why I said _"**instances** of BasicObject"_, i.e. `Object === BasicObject.new #=> false` – Stefan Jun 03 '22 at 17:14