Sha256: 51a12cf08330949a7c5449a4e3d0e43fccc6535bcd181370fecb99f330de1629

Contents?: true

Size: 1.74 KB

Versions: 10

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

##
# The {Ryo::Keywords Ryo::Keywords} module implements Ryo equivalents
# to some of JavaScript's keywords - for example: the **in** and **delete**
# operators. The instance methods of this module are available as singleton
# methods on the {Ryo} module.
module Ryo::Keywords
  ##
  # @example
  #   point = Ryo(x: 0, y: 0, print: Ryo.fn { print x, y, "\n" })
  #   point.print.()
  #
  # @param [Proc] b
  #  The function's body.
  #
  # @return [Ryo::Function]
  #  Returns a Ryo function.
  #
  # @see Ryo::Function Ryo::Function
  def fn(&b)
    Ryo::Function.new(&b)
  end
  alias_method :function, :fn

  ##
  # Equivalent to JavaScript's **in** operator.
  #
  # @param [<Ryo::Object, Ryo::BasicObject>] ryo
  #  A Ryo object.
  #
  # @param [<String, #to_s>] property
  #  A property name.
  #
  # @return [Boolean]
  #  Returns true when **property** is a member of **ryo**, or its prototype chain.
  def in?(ryo, property)
    return false unless ryo
    property?(ryo, property) || in?(prototype_of(ryo), property)
  end

  ##
  # The {#delete} method deletes a property from a Ryo object.
  # More or less equivalent to JavaScript's "delete" operator.
  #
  # @note
  #  This method does not delete properties from the prototype(s)
  #  of a Ryo object. <br>
  #  For that - see {Ryo::Reflect#delete! Ryo::Reflect#delete!}.
  #
  # @param [<Ryo::Object, Ryo::BasicObject>] ryo
  #  A Ryo object.
  #
  # @param [<String, #to_s>] property
  #  A property name.
  #
  # @return [void]
  def delete(ryo, property)
    property = property.to_s
    if property?(ryo, property)
      table_of(ryo).delete(property)
    else
      return if getter_defined?(ryo, property)
      define_method!(ryo, property) { ryo[property] }
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
ryo.rb-0.5.6 lib/ryo/keywords.rb
ryo.rb-0.5.5 lib/ryo/keywords.rb
ryo.rb-0.5.3 lib/ryo/keywords.rb
ryo.rb-0.5.2 lib/ryo/keywords.rb
ryo.rb-0.5.1 lib/ryo/keywords.rb
ryo.rb-0.5.0 lib/ryo/keywords.rb
ryo.rb-0.4.7 lib/ryo/keywords.rb
ryo.rb-0.4.6 lib/ryo/keywords.rb
ryo.rb-0.4.5 lib/ryo/keywords.rb
ryo.rb-0.4.4 lib/ryo/keywords.rb