From 07b15bf6fa2a2183f73fcb9b6740c7df75c8b47b Mon Sep 17 00:00:00 2001 From: Markus Roberts Date: Fri, 9 Jul 2010 18:06:37 -0700 Subject: Code smell: Avoid unneeded blocks Replaced 45 occurances of (DEF) begin (LINES) rescue(.*) (LINES) end end with 3 Examples: The code: def find(name) begin self.const_get(name.to_s.capitalize) rescue puts "Unable to find application '#{name.to_s}'." Kernel::exit(1) end end becomes: def find(name) self.const_get(name.to_s.capitalize) rescue puts "Unable to find application '#{name.to_s}'." Kernel::exit(1) end The code: def exit_on_fail(message, code = 1) begin yield rescue RuntimeError, NotImplementedError => detail puts detail.backtrace if Puppet[:trace] $stderr.puts "Could not #{message}: #{detail}" exit(code) end end becomes: def exit_on_fail(message, code = 1) yield rescue RuntimeError, NotImplementedError => detail puts detail.backtrace if Puppet[:trace] $stderr.puts "Could not #{message}: #{detail}" exit(code) end The code: def start begin case ssl when :tls @connection = LDAP::SSLConn.new(host, port, true) when true @connection = LDAP::SSLConn.new(host, port) else @connection = LDAP::Conn.new(host, port) end @connection.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3) @connection.set_option(LDAP::LDAP_OPT_REFERRALS, LDAP::LDAP_OPT_ON) @connection.simple_bind(user, password) rescue => detail raise Puppet::Error, "Could not connect to LDAP: #{detail}" end end becomes: def start case ssl when :tls @connection = LDAP::SSLConn.new(host, port, true) when true @connection = LDAP::SSLConn.new(host, port) else @connection = LDAP::Conn.new(host, port) end @connection.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3) @connection.set_option(LDAP::LDAP_OPT_REFERRALS, LDAP::LDAP_OPT_ON) @connection.simple_bind(user, password) rescue => detail raise Puppet::Error, "Could not connect to LDAP: #{detail}" end --- install.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'install.rb') diff --git a/install.rb b/install.rb index a4abcb9ab..293a4c4b4 100755 --- a/install.rb +++ b/install.rb @@ -371,7 +371,6 @@ rescue SystemCallError end def run_tests(test_list) - begin require 'test/unit/ui/console/testrunner' $LOAD_PATH.unshift "lib" test_list.each do |test| @@ -386,9 +385,8 @@ def run_tests(test_list) tests.each { |test| Test::Unit::UI::Console::TestRunner.run(test) } $LOAD_PATH.shift - rescue LoadError +rescue LoadError puts "Missing testrunner library; skipping tests" - end end ## -- cgit