class Prism::StringQuery

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

string[R]

The string that this query is wrapping.

Public Class Methods

Prism::StringQuery::constant?(string) → bool click to toggle source

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.

static VALUE
string_query_constant_p(VALUE self, VALUE string) {
    const uint8_t *source = (const uint8_t *) check_string(string);
    return string_query(pm_string_query_constant(source, RSTRING_LEN(string), rb_enc_get(string)->name));
}
Prism::StringQuery::local?(string) → bool click to toggle source

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.

static VALUE
string_query_local_p(VALUE self, VALUE string) {
    const uint8_t *source = (const uint8_t *) check_string(string);
    return string_query(pm_string_query_local(source, RSTRING_LEN(string), rb_enc_get(string)->name));
}
Prism::StringQuery::method_name?(string) → bool click to toggle source

Returns true if the string constitutes a valid method name.

static VALUE
string_query_method_name_p(VALUE self, VALUE string) {
    const uint8_t *source = (const uint8_t *) check_string(string);
    return string_query(pm_string_query_method_name(source, RSTRING_LEN(string), rb_enc_get(string)->name));
}
new(string) click to toggle source

Initialize a new query with the given string.

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

Private Class Methods

query(result) click to toggle source

Parse the enum result and return an appropriate boolean.

# File lib/prism/ffi.rb, line 566
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

Public Instance Methods

constant?() click to toggle source

Whether or not this string is a valid constant name.

# File lib/prism/string_query.rb, line 22
def constant?
  StringQuery.constant?(string)
end
local?() click to toggle source

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

# File lib/prism/string_query.rb, line 17
def local?
  StringQuery.local?(string)
end
method_name?() click to toggle source

Whether or not this string is a valid method name.

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