123456789_123456789_123456789_123456789_123456789_

Class: Prism::StringQuery

Relationships & Source Files
Inherits: Object
Defined in: lib/prism/ffi.rb,
ext/prism/extension.c,
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.

: (String string) -> void

[ GitHub ]

  
# File 'lib/prism/string_query.rb', line 22

def initialize(string)
  @string = string
end

Class Method Details

.constant?(string) ⇒ Boolean

This method is for internal use only.

Mirrors the C extension's .constant? method.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 717

def constant?(string)
  query(LibRubyParser.pm_string_query_constant(string, string.bytesize, string.encoding.name))
end

.keyword?(string) ⇒ Boolean

This method is for internal use only.

Mirrors the C extension's .keyword? method.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 727

def keyword?(string)
  query(LibRubyParser.pm_string_query_keyword(string, string.bytesize, string.encoding.name))
end

.local?(string) ⇒ Boolean

This method is for internal use only.

Mirrors the C extension's .local? method.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 712

def local?(string)
  query(LibRubyParser.pm_string_query_local(string, string.bytesize, string.encoding.name))
end

.method_name?(string) ⇒ Boolean

This method is for internal use only.

Mirrors the C extension's .method_name? method.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 722

def method_name?(string)
  query(LibRubyParser.pm_string_query_method_name(string, string.bytesize, string.encoding.name))
end

.query(result) (private)

This method is for internal use only.

Parse the enum result and return an appropriate boolean.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 734

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.

: () -> bool

[ GitHub ]

  
# File 'lib/prism/string_query.rb', line 36

def constant?
  StringQuery.constant?(string)
end

#keyword? ⇒ Boolean (readonly)

Whether or not this string is a valid keyword.

: () -> bool

[ GitHub ]

  
# File 'lib/prism/string_query.rb', line 50

def keyword?
  StringQuery.keyword?(string)
end

#local? ⇒ Boolean (readonly)

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

: () -> bool

[ GitHub ]

  
# File 'lib/prism/string_query.rb', line 29

def local?
  StringQuery.local?(string)
end

#method_name? ⇒ Boolean (readonly)

Whether or not this string is a valid method name.

: () -> bool

[ GitHub ]

  
# File 'lib/prism/string_query.rb', line 43

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 17

attr_reader :string #: String