Module: Ryo::Keywords

Included in:
Ryo
Defined in:
lib/ryo/keywords.rb

Overview

The Ryo::Keywords module implements Ryo equivalents for some of JavaScript’s keywords (eg: the in and delete operators). The instance methods of this module are available as singleton methods on the Ryo module.

Instance Method Summary collapse

Instance Method Details

#fn(&b) ⇒ Ryo::Function Also known as: function

Returns a Ryo function.

Examples:

point = Ryo(x: 0, y: 0, print: Ryo.fn { print x, y, "\n" })
point.print.()

Parameters:

  • b (Proc)

    The function’s body.

Returns:

See Also:



21
22
23
# File 'lib/ryo/keywords.rb', line 21

def fn(&b)
  Ryo::Function.new(&b)
end

#in?(ryo, property) ⇒ Boolean

Equivalent to JavaScript’s in operator

Parameters:

Returns:

  • (Boolean)

    Returns true when property is a member of ryo, or its prototype chain



34
35
36
37
# File 'lib/ryo/keywords.rb', line 34

def in?(ryo, property)
  return false unless ryo
  property?(ryo, property) || in?(prototype_of(ryo), property)
end

#delete(ryo, property, ancestors: nil) ⇒ void

This method returns an undefined value.

The #delete method deletes a property from a Ryo object

Parameters:

  • ryo (<Ryo::Object, Ryo::BasicObject>)

    A Ryo object

  • property (<String, #to_s>)

    A property name

  • ancestors (Integer) (defaults to: nil)

    The number of prototypes to traverse. Defaults to the entire prototype chain.

See Also:

  • Reflect#delete!


50
51
52
53
54
# File 'lib/ryo/keywords.rb', line 50

def delete(ryo, property, ancestors: nil)
  each_ryo(ryo, ancestors:) do
    Ryo.property?(_1, property) ? table_of(_1).delete(property) : nil
  end
end