summaryrefslogtreecommitdiff
path: root/lib/irb/context.rb
diff options
context:
space:
mode:
authorStan Lo <[email protected]>2024-08-27 13:49:17 +0100
committergit <[email protected]>2024-08-27 12:49:23 +0000
commit87fa75c5db2e6f89a892c0ed75f1701398ce5f19 (patch)
tree9febe70576ab194fed7c0a6064b25879b497b254 /lib/irb/context.rb
parenta431f4ecc474d51990a0cb263fdf3e07964150e4 (diff)
[ruby/irb] Move parse_command method to Context
(https://github.com/ruby/irb/pull/993) Since Context dictates whether a line is a command or an expression, moving the parse_command method to Context makes the relationship more explicit. https://github.com/ruby/irb/commit/9a4487af61
Diffstat (limited to 'lib/irb/context.rb')
-rw-r--r--lib/irb/context.rb26
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/irb/context.rb b/lib/irb/context.rb
index 74a08ca694..aa0c60b0e3 100644
--- a/lib/irb/context.rb
+++ b/lib/irb/context.rb
@@ -13,6 +13,7 @@ module IRB
# A class that wraps the current state of the irb session, including the
# configuration of IRB.conf.
class Context
+ ASSIGN_OPERATORS_REGEXP = Regexp.union(%w[= += -= *= /= %= **= &= |= &&= ||= ^= <<= >>=])
# Creates a new IRB context.
#
# The optional +input_method+ argument:
@@ -635,6 +636,31 @@ module IRB
result
end
+ def parse_command(code)
+ command_name, arg = code.strip.split(/\s+/, 2)
+ return unless code.lines.size == 1 && command_name
+
+ arg ||= ''
+ command = command_name.to_sym
+ # Command aliases are always command. example: $, @
+ if (alias_name = command_aliases[command])
+ return [alias_name, arg]
+ end
+
+ # Assignment-like expression is not a command
+ return if arg.start_with?(ASSIGN_OPERATORS_REGEXP) && !arg.start_with?(/==|=~/)
+
+ # Local variable have precedence over command
+ return if local_variables.include?(command)
+
+ # Check visibility
+ public_method = !!Kernel.instance_method(:public_method).bind_call(main, command) rescue false
+ private_method = !public_method && !!Kernel.instance_method(:method).bind_call(main, command) rescue false
+ if Command.execute_as_command?(command, public_method: public_method, private_method: private_method)
+ [command, arg]
+ end
+ end
+
def inspect_last_value # :nodoc:
@inspect_method.inspect_value(@last_value)
end