A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://ruby-syntax-tree.github.io/syntax_tree/SyntaxTree/HshPtn.html below:

class SyntaxTree::HshPtn - RDoc Documentation

  1. SyntaxTree::
  2. HshPtn
class SyntaxTree::HshPtn

HshPtn represents matching against a hash pattern using the Ruby 2.7+ pattern matching syntax.

case value
in { key: }
end
Attributes
nil | VarField

an optional parameter to gather up all remaining keywords

Array[ [DynaSymbol | Label, nil | Node

]] the set of tuples

representing the keywords that should be matched against in the pattern

Public Class Methods

Source

def initialize(constant:, keywords:, keyword_rest:, location:)
  @constant = constant
  @keywords = keywords
  @keyword_rest = keyword_rest
  @location = location
  @comments = []
end
Public Instance Methods

Source

def ===(other)
  other.is_a?(HshPtn) && constant === other.constant &&
    keywords.length == other.keywords.length &&
    keywords
      .zip(other.keywords)
      .all? { |left, right| ArrayMatch.call(left, right) } &&
    keyword_rest === other.keyword_rest
end

Source

def accept(visitor)
  visitor.visit_hshptn(self)
end

Source

def child_nodes
  [constant, *keywords.flatten(1), keyword_rest]
end

Source

def copy(constant: nil, keywords: nil, keyword_rest: nil, location: nil)
  node =
    HshPtn.new(
      constant: constant || self.constant,
      keywords: keywords || self.keywords,
      keyword_rest: keyword_rest || self.keyword_rest,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

Source

def deconstruct_keys(_keys)
  {
    constant: constant,
    keywords: keywords,
    keyword_rest: keyword_rest,
    location: location,
    comments: comments
  }
end

Source

def format(q)
  parts = keywords.map { |(key, value)| KeywordFormatter.new(key, value) }
  parts << KeywordRestFormatter.new(keyword_rest) if keyword_rest
  nested = PATTERNS.include?(q.parent.class)

  
  
  if constant
    q.group do
      q.format(constant)
      q.text("[")
      q.indent do
        q.breakable_empty
        format_contents(q, parts, nested)
      end
      q.breakable_empty
      q.text("]")
    end
    return
  end

  
  if parts.empty?
    q.text("{}")
    return
  end

  
  
  if !nested && parts.size == 1
    format_contents(q, parts, nested)
    return
  end

  
  
  q.group do
    q.text("{")
    q.indent do
      q.breakable_space
      format_contents(q, parts, nested)
    end

    if q.target_ruby_version < Formatter::SemanticVersion.new("2.7.3")
      q.text(" }")
    else
      q.breakable_space
      q.text("}")
    end
  end
end

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4