123456789_123456789_123456789_123456789_123456789_

Class: Prism::StringQuery

Relationships & Source Files
Inherits: Object
Defined in: lib/prism/ffi.rb,
lib/prism/string_query.rb

Overview

Query methods that allow categorizing strings based on their context for where they could be valid in a Ruby syntax tree.

Class Method Summary

Instance Attribute Summary

Constructor Details

.new(string) ⇒ StringQuery

Initialize a new query with the given string.

[ GitHub ]

  
# File 'lib/prism/string_query.rb', line 11
def initialize(string)
  @string = string
end

Class Method Details

.constant?(string) ⇒ Boolean

Mirrors the C extension’s .constant? method.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 542
def constant?(string)
  query(LibRubyParser.pm_string_query_constant(string, string.bytesize, string.encoding.name))
end

.local?(string) ⇒ Boolean

Mirrors the C extension’s .local? method.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 537
def local?(string)
  query(LibRubyParser.pm_string_query_local(string, string.bytesize, string.encoding.name))
end

.method_name?(string) ⇒ Boolean

Mirrors the C extension’s .method_name? method.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 547
def method_name?(string)
  query(LibRubyParser.pm_string_query_method_name(string, string.bytesize, string.encoding.name))
end

.query(result) (private)

Parse the enum result and return an appropriate boolean.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 554
def query(result)
  case result
  when :PM_STRING_QUERY_ERROR
    raise ArgumentError, "Invalid or non ascii-compatible encoding"
  when :PM_STRING_QUERY_FALSE
    false
  when :PM_STRING_QUERY_TRUE
    true
  end
end

Instance Attribute Details

#constant?Boolean (readonly)

Whether or not this string is a valid constant name.

[ GitHub ]

  
# File 'lib/prism/string_query.rb', line 21
def constant?
  StringQuery.constant?(string)
end

#local?Boolean (readonly)

Whether or not this string is a valid local variable name.

[ GitHub ]

  
# File 'lib/prism/string_query.rb', line 16
def local?
  StringQuery.local?(string)
end

#method_name?Boolean (readonly)

Whether or not this string is a valid method name.

[ GitHub ]

  
# File 'lib/prism/string_query.rb', line 26
def method_name?
  StringQuery.method_name?(string)
end

#string (readonly)

The string that this query is wrapping.

[ GitHub ]

  
# File 'lib/prism/string_query.rb', line 8
attr_reader :string