From 7de77868b4931beaf9fee66a583cbc007cc5fd78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mon=20ou=C3=AFe?= Date: Mon, 25 Apr 2011 18:09:04 +0200 Subject: [PATCH] Made command sets respect :keep_retval --- lib/pry/command_set.rb | 5 +++-- test/test.rb | 14 ++++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/pry/command_set.rb b/lib/pry/command_set.rb index c0a1b790..eb663354 100644 --- a/lib/pry/command_set.rb +++ b/lib/pry/command_set.rb @@ -10,7 +10,8 @@ class Pry class CommandSet class Command < Struct.new(:name, :description, :options, :block) def call(context, *args) - context.instance_exec(*args, &block) + ret = context.instance_exec(*args, &block) + ret if options[:keep_retval] end end @@ -89,7 +90,7 @@ class Pry def alias_command(new_name, old_name, desc = nil) commands[new_name] = commands[old_name].dup commands[new_name].name = new_name - commands[new_name].description = desc + commands[new_name].description = desc if desc end # Runs a command. diff --git a/test/test.rb b/test/test.rb index 0f036b0a..cfd9ba55 100644 --- a/test/test.rb +++ b/test/test.rb @@ -801,10 +801,6 @@ describe Pry::CommandSet do @set.name.should == :some_name end - it 'should have no commands' do - @set.commands.should.be.empty - end - it 'should call the block used for the command when it is called' do run = false @set.command 'foo' do @@ -911,4 +907,14 @@ describe Pry::CommandSet do @set.commands['foo'].description.should == 'baz' end + + it 'should return nil for commands by default' do + @set.command('foo') { 3 } + @set.run_command(nil, 'foo').should == nil + end + + it 'should be able to keep return values' do + @set.command('foo', '', :keep_retval => true) { 3 } + @set.run_command(nil, 'foo').should == 3 + end end