Class: Prism::StringQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/prism/ffi.rb,
lib/prism/string_query.rb,
sig/generated/prism/string_query.rbs,
ext/prism/extension.c

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ StringQuery

Initialize a new query with the given string.

: (String string) -> void

Parameters:

  • string (String)


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

def initialize(string)
  @string = string
end

Instance Attribute Details

#string ⇒ String (readonly)

The string that this query is wrapping.

Returns:

  • (String)


17
18
19
# File 'lib/prism/string_query.rb', line 17

def string
  @string
end

Class Method Details

.constant?(string) ⇒ Boolean

:markup: markdown call-seq:

constant?(string) -> bool

Returns true if the string constitutes a valid constant name. Note that this means the names that can be set through Module#const_set, not necessarily the ones that can be set through a constant assignment.

Returns:

  • (Boolean)


717
718
719
# 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

:markup: markdown call-seq:

keyword?(string) -> bool

Returns true if the string is a valid keyword, such as if, defined?, or __FILE__.

Returns:

  • (Boolean)


727
728
729
# 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

:markup: markdown call-seq:

local?(string) -> bool

Returns true if the string constitutes a valid local variable name. Note that this means the names that can be set through Binding#local_variable_set, not necessarily the ones that can be set through a local variable assignment.

Returns:

  • (Boolean)


712
713
714
# 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

:markup: markdown call-seq:

method_name?(string) -> bool

Returns true if the string constitutes a valid method name.

Returns:

  • (Boolean)


722
723
724
# 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

Instance Method Details

#constant? ⇒ Boolean

Whether or not this string is a valid constant name.

: () -> bool

Returns:

  • (Boolean)


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

def constant?
  StringQuery.constant?(string)
end

#keyword? ⇒ Boolean

Whether or not this string is a valid keyword.

: () -> bool

Returns:

  • (Boolean)


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

def keyword?
  StringQuery.keyword?(string)
end

#local? ⇒ Boolean

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

: () -> bool

Returns:

  • (Boolean)


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

def local?
  StringQuery.local?(string)
end

#method_name? ⇒ Boolean

Whether or not this string is a valid method name.

: () -> bool

Returns:

  • (Boolean)


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

def method_name?
  StringQuery.method_name?(string)
end