[前][次][番号順一覧][スレッド一覧]

ruby-changes:65602

From: Takashi <ko1@a...>
Date: Sun, 21 Mar 2021 13:00:06 +0900 (JST)
Subject: [ruby-changes:65602] 5f72962a09 (master): [ruby/irb] Implement ls command

https://git.ruby-lang.org/ruby.git/commit/?id=5f72962a09

From 5f72962a0955d62dfbac2f7553b725b9d1e9e60d Mon Sep 17 00:00:00 2001
From: Takashi Kokubun <takashikkbn@g...>
Date: Thu, 18 Mar 2021 00:28:04 -0700
Subject: [ruby/irb] Implement ls command

https://github.com/ruby/irb/commit/19b6c20604
---
 lib/irb/cmd/ls.rb         | 48 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/irb/cmd/nop.rb        | 14 ++++++++++----
 lib/irb/extend-command.rb | 12 +++++++++---
 test/irb/test_cmd.rb      | 17 +++++++++++++++++
 4 files changed, 84 insertions(+), 7 deletions(-)
 create mode 100644 lib/irb/cmd/ls.rb

diff --git a/lib/irb/cmd/ls.rb b/lib/irb/cmd/ls.rb
new file mode 100644
index 0000000..c39937d
--- /dev/null
+++ b/lib/irb/cmd/ls.rb
@@ -0,0 +1,48 @@ https://github.com/ruby/ruby/blob/trunk/lib/irb/cmd/ls.rb#L1
+# frozen_string_literal: true
+
+require_relative "nop"
+require_relative "../color"
+
+# :stopdoc:
+module IRB
+  module ExtendCommand
+    class Ls < Nop
+      def execute(*arg, grep: nil)
+        o = Output.new(grep: grep)
+
+        obj    = arg.empty? ? irb_context.workspace.main : arg.first
+        locals = arg.empty? ? irb_context.workspace.binding.local_variables : []
+        klass  = (obj.class == Class || obj.class == Module ? obj : obj.class)
+
+        o.dump("constants", obj.constants) if obj.respond_to?(:constants)
+        o.dump("#{klass}.methods", obj.singleton_methods(false))
+        o.dump("#{klass}#methods", klass.public_instance_methods(false))
+        o.dump("instance variables", obj.instance_variables)
+        o.dump("class variables", klass.class_variables)
+        o.dump("locals", locals)
+      end
+
+      class Output
+        def initialize(grep: nil)
+          @grep = grep
+        end
+
+        def dump(name, strs)
+          strs = strs.grep(@grep) if @grep
+          strs = strs.sort
+          return if strs.empty?
+
+          print "#{Color.colorize(name, [:BOLD, :BLUE])}: "
+          if strs.size > 7
+            len = [strs.map(&:length).max, 16].min
+            puts; strs.each_slice(7) { |ss| puts "  #{ss.map { |s| "%-#{len}s" % s }.join("  ")}" }
+          else
+            puts strs.join("  ")
+          end
+        end
+      end
+      private_constant :Output
+    end
+  end
+end
+# :startdoc:
diff --git a/lib/irb/cmd/nop.rb b/lib/irb/cmd/nop.rb
index fa3c011..d6f7a61 100644
--- a/lib/irb/cmd/nop.rb
+++ b/lib/irb/cmd/nop.rb
@@ -14,10 +14,16 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/cmd/nop.rb#L14
   module ExtendCommand
     class Nop
 
-
-      def self.execute(conf, *opts, &block)
-        command = new(conf)
-        command.execute(*opts, &block)
+      if RUBY_ENGINE == "ruby" && RUBY_VERSION >= "2.7.0"
+        def self.execute(conf, *opts, **kwargs, &block)
+          command = new(conf)
+          command.execute(*opts, **kwargs, &block)
+        end
+      else
+        def self.execute(conf, *opts, &block)
+          command = new(conf)
+          command.execute(*opts, &block)
+        end
       end
 
       def initialize(conf)
diff --git a/lib/irb/extend-command.rb b/lib/irb/extend-command.rb
index af076a6..e7b046a 100644
--- a/lib/irb/extend-command.rb
+++ b/lib/irb/extend-command.rb
@@ -126,6 +126,11 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/extend-command.rb#L126
       ],
 
       [
+        :irb_ls, :Ls, "irb/cmd/ls",
+        [:ls, NO_OVERRIDE],
+      ],
+
+      [
         :irb_measure, :Measure, "irb/cmd/measure",
         [:measure, NO_OVERRIDE],
       ],
@@ -169,12 +174,13 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/extend-command.rb#L174
       end
 
       if load_file
+        kwargs = ", **kwargs" if RUBY_ENGINE == "ruby" && RUBY_VERSION >= "2.7.0"
         line = __LINE__; eval %[
-          def #{cmd_name}(*opts, &b)
+          def #{cmd_name}(*opts#{kwargs}, &b)
             require "#{load_file}"
             arity = ExtendCommand::#{cmd_class}.instance_method(:execute).arity
             args = (1..(arity < 0 ? ~arity : arity)).map {|i| "arg" + i.to_s }
-            args << "*opts" if arity < 0
+            args << "*opts#{kwargs}" if arity < 0
             args << "&block"
             args = args.join(", ")
             line = __LINE__; eval %[
@@ -185,7 +191,7 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/extend-command.rb#L191
                 end
               end
             ], nil, __FILE__, line
-            __send__ :#{cmd_name}_, *opts, &b
+            __send__ :#{cmd_name}_, *opts#{kwargs}, &b
           end
         ], nil, __FILE__, line
       else
diff --git a/test/irb/test_cmd.rb b/test/irb/test_cmd.rb
index 16f5940..4a512ed 100644
--- a/test/irb/test_cmd.rb
+++ b/test/irb/test_cmd.rb
@@ -374,5 +374,22 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_cmd.rb#L374
           /=> "bug17564"\n/,
         ], out)
     end
+
+    def test_ls
+      IRB.init_config(nil)
+      workspace = IRB::WorkSpace.new(self)
+      irb = IRB::Irb.new(workspace)
+      IRB.conf[:MAIN_CONTEXT] = irb.context
+      input = TestInputMethod.new([
+        "ls Object.new.tap { |o| o.instance_variable_set(:@a, 1) }\n",
+      ])
+      irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input)
+      irb.context.return_format = "=> %s\n"
+      out, err = capture_output do
+        irb.eval_input
+      end
+      assert_empty err
+      assert_match(/^instance variables: @a\n/, out)
+    end
   end
 end
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]