Skip to content

Commit

Permalink
Merge pull request #1608 from ruby/add-type
Browse files Browse the repository at this point in the history
Add a type method for quick comparison
  • Loading branch information
kddnewton authored Sep 29, 2023
2 parents 81265ed + 0c7d9c3 commit e92e5a2
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions templates/lib/prism/node.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,24 @@ module Prism
<%- end -%>
inspector.to_str
end
# Sometimes you want to check an instance of a node against a list of
# classes to see what kind of behavior to perform. Usually this is done by
# calling `[cls1, cls2].include?(node.class)` or putting the node into a
# case statement and doing `case node; when cls1; when cls2; end`. Both of
# these approaches are relatively slow because of the constant lookups,
# method calls, and/or array allocations.
#
# Instead, you can call #type, which will return to you a symbol that you
# can use for comparison. This is faster than the other approaches because
# it uses a single integer comparison, but also because if you're on CRuby
# you can take advantage of the fact that case statements with all symbol
# keys will use a jump table.
#
# def type: () -> Symbol
def type
:<%= node.human %>
end
end
<%- end -%>
<%- flags.each_with_index do |flag, flag_index| -%>
Expand Down

0 comments on commit e92e5a2

Please sign in to comment.