From b44c3c2cd127eef60ff54b77182404e6d0ef8d24 Mon Sep 17 00:00:00 2001 From: Arturo Herrero Date: Tue, 10 Mar 2020 22:59:44 +0000 Subject: [PATCH] Improve Ruby style --- bin/yarr | 2 +- lib/interpreter.rb | 38 ++++++++++++++++++------------------ lib/yarr.rb | 2 +- spec/lib/interpreter_spec.rb | 9 ++++++--- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/bin/yarr b/bin/yarr index d2f08d8..bf46229 100755 --- a/bin/yarr +++ b/bin/yarr @@ -1,5 +1,5 @@ #!/usr/bin/env ruby -require File.expand_path("../../lib/yarr", __FILE__) +require File.expand_path("../lib/yarr", __dir__) YARR.new.call diff --git a/lib/interpreter.rb b/lib/interpreter.rb index 4086e30..e8773e9 100644 --- a/lib/interpreter.rb +++ b/lib/interpreter.rb @@ -34,7 +34,7 @@ def call private def prompt(separator) - "#{bold("ruby")}:#{"%03d" % (Readline::HISTORY.size + 1)}#{bold(separator)} " + "#{bold('ruby')}:#{format('%03d', (Readline::HISTORY.size + 1))}#{bold(separator)} " end def process(expression) @@ -47,34 +47,34 @@ def process(expression) def command(expression) command, arguments = expression.split(" ", 2) - self.send(command[1..-1], arguments) - rescue - "#{red("ERROR")} #{command} command is not available" + send(command[1..-1], arguments) + rescue StandardError + "#{red('ERROR')} #{command} command is not available" end - def exit(args) + def exit(_args) exit! end alias_method :quit, :exit - def help(args) - <<-END.gsub(/^\s+\|/, '') + def help(_args) + <<-END.gsub(/^\s+\|/, "") | |Available commands: - | #{bold(":exit")} Exit the shell - | #{bold(":help")} Display this help message - | #{bold(":hist")} Display edit-line history - | #{bold(":quit")} Alias to #{bold(":exit")} - | #{bold(":")}number Execute a specific expression from history - | #{bold(":!")} cmd Execute a shell command à la Vim + | #{bold(':exit')} Exit the shell + | #{bold(':help')} Display this help message + | #{bold(':hist')} Display edit-line history + | #{bold(':quit')} Alias to #{bold(':exit')} + | #{bold(':')}number Execute a specific expression from history + | #{bold(':!')} cmd Execute a shell command à la Vim | END end - def hist(args) + def hist(_args) "".tap do |history| Readline::HISTORY.to_a.each.with_index(1) do |command, index| - history << " #{bold("%03d" % index)} #{command}\n" + history << " #{bold('%03d' % index)} #{command}\n" end end end @@ -83,7 +83,7 @@ def hist(args) `export CLICOLOR=1; export CLICOLOR_FORCE=1; #{args}` end - def method_missing(name, *args) + def method_missing(name, *_args) index = (name.to_s[1..-1].to_i - 1).tap { |index| raise StandardError if index == -1 } expression = Readline::HISTORY[index] Readline::HISTORY.pop @@ -92,13 +92,13 @@ def method_missing(name, *args) end def evaluate(expression) - "#{bold("===>")} #{eval(expression, TOPLEVEL_BINDING)}" + "#{bold('===>')} #{eval(expression, TOPLEVEL_BINDING)}" rescue Exception => e - "#{red("ERROR")} #{e.message}" + "#{red('ERROR')} #{e.message}" end def commands - %w(:exit :help :hist :quit :!) + %w[:exit :help :hist :quit :!] end def capture_stdout(&block) diff --git a/lib/yarr.rb b/lib/yarr.rb index 9b1900b..c62d1a6 100644 --- a/lib/yarr.rb +++ b/lib/yarr.rb @@ -23,7 +23,7 @@ def call def banner puts bold("YARR | Ruby #{RUBY_VERSION}") - puts "Type '#{bold(":help")}' for help." + puts "Type '#{bold(':help')}' for help." puts bold("-" * IO.console.winsize.last) end end diff --git a/spec/lib/interpreter_spec.rb b/spec/lib/interpreter_spec.rb index 0be5ac1..ff2d739 100644 --- a/spec/lib/interpreter_spec.rb +++ b/spec/lib/interpreter_spec.rb @@ -31,7 +31,8 @@ setup("def foo", "1", "end") setup("foo") given(":hist") - expect(interpreter.call).to eq(<<-END.gsub(/^\s+\||$/, '') + expect(interpreter.call).to eq( + <<-END.gsub(/^\s+\||$/, ""), | \e[1m001\e[0m 5 | \e[1m002\e[0m def foo | 1 @@ -56,7 +57,8 @@ setup("var = 0") setup(":001") given(":hist") - expect(interpreter.call).to eq(<<-END.gsub(/^\s+\||$/, '') + expect(interpreter.call).to eq( + <<-END.gsub(/^\s+\||$/, ""), | \e[1m001\e[0m var = 5 | \e[1m002\e[0m var = 0 | \e[1m003\e[0m var = 5 @@ -81,7 +83,8 @@ context "invalid expression" do it "returns a syntax error" do given("1 + &") - expect(interpreter.call).to eq(<<-END.gsub(/^\s+\||\n$/, '') + expect(interpreter.call).to eq( + <<-END.gsub(/^\s+\||\n$/, ""), |\e[1;31mERROR\e[0m
: syntax error, unexpected & |1 + & | ^