Fn is the class of all functions. An object of class Fn can be called with arguments. The number of arguments must match the arity of the function, i.e. the number of parameters.

Static Methods

indirect_call(fn, args)

Call function fn with the arguments in the array args. fn must be an instance of Fn and not a callable.

def hello(name) {
  return "Hello " + name
}
IO.println(Fn.indirect_call(hello, ["World"])) #> Hello World

new(function)

Create a new function thanks to function. function must be a block argument. All this function do is return its argument.

def identity = Fn.new {|a| a }
IO.println(identity(42)) #> 42

Methods

arity

Return the number of parameters of the function.

def f0() { }
IO.println(f0.arity) #> 0
def f1(a) { }
IO.println(f1.arity) #> 1
def f2(a, b) { }
IO.println(f2.arity) #> 2

to_s

Return a String: "<fn>"

def f() { }
IO.println(f.to_s) #> <fn>