class Oga::Ruby::Node

Class representing a single node in a Ruby AST.

The setup of this class is roughly based on the “ast” Gem. The “ast” Gem is not used for this class as it provides too many methods that might conflict with this class’ {#method_missing}.

ASTs can be built by creating a node and then chaining various method calls together. For example, the following could be used to build an “if” statement:

number1 = Node.new(:lit, %w{10})
number2 = Node.new(:lit, %w{20})

(number2 > number1).if_true do
  Node.new(:lit, %w{30})
end

When serialized to Ruby this would roughly lead to the following code:

if 20 > 10
  30
end

@private

Attributes

type[R]

@return [Symbol]

Public Class Methods

new(type, children = []) click to toggle source

@param [Symbol] type @param [Array] children

# File lib/oga/ruby/node.rb, line 35
def initialize(type, children = [])
  @type     = type.to_sym
  @children = children
end

Public Instance Methods

add_block(*args) { |])| ... } click to toggle source

Wraps the current node in a block.

@param [Array] args Arguments (as Node instances) to pass to the block. @return [Oga::Ruby::Node]

# File lib/oga/ruby/node.rb, line 114
def add_block(*args)
  Node.new(:block, [self, args, yield])
end
and(other) click to toggle source

Returns a boolean “and” node.

@param [Oga::Ruby::Node] other @return [Oga::Ruby::Node]

# File lib/oga/ruby/node.rb, line 81
def and(other)
  Node.new(:and, [self, other])
end
assign(other) click to toggle source

Returns an assignment node.

This method wraps assigned values in a begin/end block to ensure that multiple lines of code result in the proper value being assigned.

@param [Oga::Ruby::Node] other @return [Oga::Ruby::Node]

# File lib/oga/ruby/node.rb, line 61
def assign(other)
  if other.type == :followed_by
    other = other.wrap
  end

  Node.new(:assign, [self, other])
end
else() { |])| ... } click to toggle source

Adds an “else” statement to the current node.

This method assumes it’s being called only on “if” nodes.

@return [Oga::Ruby::Node]

# File lib/oga/ruby/node.rb, line 157
def else
  Node.new(:if, @children + [yield])
end
eq(other) click to toggle source

Returns an equality expression node.

@param [Oga::Ruby::Node] other @return [Oga::Ruby::Node]

# File lib/oga/ruby/node.rb, line 73
def eq(other)
  Node.new(:eq, [self, other])
end
followed_by(other = nil) { || ... } click to toggle source

Chains two nodes together.

@param [Oga::Ruby::Node] other @return [Oga::Ruby::Node]

# File lib/oga/ruby/node.rb, line 165
def followed_by(other = nil)
  other = yield if ::Kernel.block_given?

  Node.new(:followed_by, [self, other])
end
if_false() { || ... } click to toggle source

Wraps the current node in an ‘if !…` statement.

@see [#if_true]

# File lib/oga/ruby/node.rb, line 138
def if_false
  self.not.if_true { yield }
end
if_true() { |])| ... } click to toggle source

Wraps the current node in an if statement node.

The body of this statement is set to the return value of the supplied block.

@return [Oga::Ruby::Node]

# File lib/oga/ruby/node.rb, line 131
def if_true
  Node.new(:if, [self, yield])
end
inspect() click to toggle source

@return [String]

# File lib/oga/ruby/node.rb, line 184
def inspect
  "(#{type} #{@children.map(&:inspect).join(' ')})"
end
is_a?(klass) click to toggle source

Returns a node for Ruby’s “is_a?” method.

@param [Class] klass @return [Oga::Ruby::Node]

# File lib/oga/ruby/node.rb, line 106
def is_a?(klass)
  Node.new(:send, [self, 'is_a?', Node.new(:lit, [klass.to_s])])
end
method_missing(name, *args) click to toggle source

Returns a node for a method call.

@param [Symbol] name The name of the method to call.

@param [Array] args Any arguments (as Node instances) to pass to the

method.

@return [Oga::Ruby::Node]

# File lib/oga/ruby/node.rb, line 179
def method_missing(name, *args)
  Node.new(:send, [self, name.to_s, *args])
end
not() click to toggle source

Returns a node that evaluates to its inverse.

For example, a variable ‘foo` would be turned into `!foo`.

@return [Oga::Ruby::Node]

# File lib/oga/ruby/node.rb, line 98
def not
  !self
end
or(other) click to toggle source

Returns a boolean “or” node.

@param [Oga::Ruby::Node] other @return [Oga::Ruby::Node]

# File lib/oga/ruby/node.rb, line 89
def or(other)
  Node.new(:or, [self, other])
end
to_a() click to toggle source

@return [Array]

# File lib/oga/ruby/node.rb, line 41
def to_a
  @children
end
Also aliased as: to_ary
to_array() click to toggle source

Returns a “to_a” call node.

@return [Oga::Ruby::Node]

# File lib/oga/ruby/node.rb, line 50
def to_array
  Node.new(:send, [self, :to_a])
end
to_ary()
Alias for: to_a
while_true() { |])| ... } click to toggle source

Wraps the current node in a ‘while` statement.

The body of this statement is set to the return value of the supplied block.

@return [Oga::Ruby::Node]

# File lib/oga/ruby/node.rb, line 148
def while_true
  Node.new(:while, [self, yield])
end
wrap() click to toggle source

Wraps the current node in a ‘begin` node.

@return [Oga::Ruby::Node]

# File lib/oga/ruby/node.rb, line 121
def wrap
  Node.new(:begin, [self])
end