Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the ability to store last eval in variable. #335

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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