Skip to content

Commit

Permalink
Improve Ruby style
Browse files Browse the repository at this point in the history
  • Loading branch information
arturoherrero committed Mar 10, 2020
1 parent 1d88fcb commit b44c3c2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion bin/yarr
Original file line number Diff line number Diff line change
@@ -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
38 changes: 19 additions & 19 deletions lib/interpreter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/yarr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions spec/lib/interpreter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 <main>: syntax error, unexpected &
|1 + &
| ^
Expand Down

0 comments on commit b44c3c2

Please sign in to comment.