Class
Here we are going to patch StringQuery
to put in the class-level methods so that it can maintain a consistent interface
Query methods that allow categorizing strings based on their context for where they could be valid in a Ruby syntax tree.
Attributes
Read
The string that this query is wrapping.
Class Methods
lib/prism/ffi.rb
View on GitHub
# File tmp/rubies/ruby-3.4.1/lib/prism/ffi.rb, line 518
def constant?(string)
query(LibRubyParser.pm_string_query_constant(string, string.bytesize, string.encoding.name))
end
Mirrors the C extension’s StringQuery::constant?
method.
lib/prism/ffi.rb
View on GitHub
# File tmp/rubies/ruby-3.4.1/lib/prism/ffi.rb, line 513
def local?(string)
query(LibRubyParser.pm_string_query_local(string, string.bytesize, string.encoding.name))
end
Mirrors the C extension’s StringQuery::local?
method.
lib/prism/ffi.rb
View on GitHub
# File tmp/rubies/ruby-3.4.1/lib/prism/ffi.rb, line 523
def method_name?(string)
query(LibRubyParser.pm_string_query_method_name(string, string.bytesize, string.encoding.name))
end
Mirrors the C extension’s StringQuery::method_name?
method.
::
lib/prism/string_query.rb
View on GitHub
# File tmp/rubies/ruby-3.4.1/lib/prism/string_query.rb, line 11
def initialize(string)
@string = string
end
Initialize a new query with the given string.
lib/prism/ffi.rb
View on GitHub
# File tmp/rubies/ruby-3.4.1/lib/prism/ffi.rb, line 530
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
Parse the enum result and return an appropriate boolean.
Instance Methods
lib/prism/string_query.rb
View on GitHub
# File tmp/rubies/ruby-3.4.1/lib/prism/string_query.rb, line 21
def constant?
StringQuery.constant?(string)
end
Whether or not this string is a valid constant name.
#
lib/prism/string_query.rb
View on GitHub
# File tmp/rubies/ruby-3.4.1/lib/prism/string_query.rb, line 16
def local?
StringQuery.local?(string)
end
Whether or not this string is a valid local variable name.
lib/prism/string_query.rb
View on GitHub
# File tmp/rubies/ruby-3.4.1/lib/prism/string_query.rb, line 26
def method_name?
StringQuery.method_name?(string)
end
Whether or not this string is a valid method name.