Skip to content

Commit

Permalink
add the ability to store last eval in variable.
Browse files Browse the repository at this point in the history
  • Loading branch information
lorrydriveloper committed Nov 27, 2023
1 parent a3b7c2a commit 86f4903
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/web_console/evaluator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,40 @@ module WebConsole
# difference of a regular +Binding+ eval is that +Evaluator+ will always
# return a string and will format exception output.
class Evaluator
# stores last evalutation in this variable for futher use.
cattr_accessor :last_evaluation_variable, default: '_'
# Cleanses exceptions raised inside #eval.
cattr_reader :cleaner, default: begin
cleaner = ActiveSupport::BacktraceCleaner.new
cleaner.add_silencer { |line| line.start_with?(File.expand_path("..", __FILE__)) }
cleaner
end


def initialize(binding = TOPLEVEL_BINDING)
@binding = binding
end

def eval(input)
# Binding#source_location is available since Ruby 2.6.
if @binding.respond_to? :source_location
"=> #{@binding.eval(input, *@binding.source_location).inspect}\n"
output = "=> #{@binding.eval(input, *@binding.source_location).inspect}\n"
else
"=> #{@binding.eval(input).inspect}\n"
output = "=> #{@binding.eval(input).inspect}\n"
end
set_last_eval(input)
output
rescue Exception => exc
format_exception(exc)
end

private

def set_last_eval(input)
return if input.blank?
@binding.eval("#{last_evaluation_variable} = #{input}")
end

def format_exception(exc)
backtrace = cleaner.clean(Array(exc.backtrace) - caller)

Expand Down
6 changes: 6 additions & 0 deletions lib/web_console/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,11 @@ def web_console_permissions
initializer "i18n.load_path" do
config.i18n.load_path.concat(Dir[File.expand_path("../locales/*.yml", __FILE__)])
end

initializer 'web_console.last_evaluation_variable' do
if last_evaluation_variable = config.web_console.last_evaluation_variable
Evaluator.last_evaluation_variable = last_evaluation_variable
end
end
end
end
16 changes: 16 additions & 0 deletions test/web_console/evaluator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ def backtrace
END
end

test "Evaluator preserve the last value in the _ variable" do
@repl.class.last_evaluation_variable = "_"
assert_equal "=> 42\n", @repl.eval("foo = 42")
assert_equal "=> 42\n", @repl.eval("_")
assert_equal "=> 50\n", @repl.eval("_ + 8")
assert_equal "=> 50\n", @repl.eval("_")
end

test "last evaluation variable can be configured" do
assert_equal "=> 42\n", @repl.eval("foo = 42")
assert_equal "=> 42\n", @repl.eval("_")
@repl.class.last_evaluation_variable = "$_last_value"
assert_equal "=> 42\n", @repl.eval("foo = 42")
assert_equal "=> 42\n", @repl.eval("$_last_value")
end

private

def current_trace
Expand Down

0 comments on commit 86f4903

Please sign in to comment.