error = false if `whoami` != "ubuntu" puts "You aren't logged in as ubuntu" error = true end if `ps -ef |Â grep [s]elenium`.length == 0 puts "selenium isn't start" error = true end puts "environment is ready" unless error
Refactorings
No refactoring yet !
steved
March 5, 2011, March 05, 2011 06:32, permalink
(Untested)
def user_ubuntu?
if `whoami` != "ubuntu"
puts "You aren't logged in as ubuntu"
false
else
true
end
end
def selenium_started?
if `ps -ef |Â grep [s]elenium`.length == 0
puts "selenium isn't started"
false
else
true
end
end
puts "environment is ready" if user_ubuntu? && selenium_started?
Ants
March 5, 2011, March 05, 2011 09:13, permalink
@steved: I actually had the same idea as you have, but that approach changes the behavior. The original behavior would print both "You aren't logged in as ubuntu" and "selenium isn't started", but now if you aren't logged in as ubuntu, you'll never get the the other message.
steved
March 5, 2011, March 05, 2011 20:34, permalink
@ants: Ah, correct. I think avoiding the flag in this case is a questionable requirement. But, this should work
puts "environment is ready" if [user_ubuntu?, selenium_started?].all?
rkr1410.myopenid.com
March 5, 2011, March 05, 2011 20:50, permalink
Whether you use a flag or not, breaking the code into subroutines is in my opinion definitely a good refactoring in itself.
You could possibly keep the flag out of the functions and just use the returned value:
success = selenium_started? success = success && user_ubuntu? success = success && some_other_check? ... puts "environment is ready" if success
rkr1410.myopenid.com
March 5, 2011, March 05, 2011 20:52, permalink
//deleted - double posting, sorry
pahanix
March 6, 2011, March 06, 2011 06:49, permalink
http://www.ruby-doc.org/core/classes/FalseClass.html#M000419
http://stackoverflow.com/questions/487067/how-to-avoid-short-circuit-evaluation-on
use & instead of && to do no short-circuit evaluation
but you have to make sure that question mark methods return true, false or nil
because for example Fixnum#& does bitwise AND or Array#& sets intersection
puts "environment is ready" if user_ubuntu? & selenium_started?
searching for an elegant solution. i think its a basic problem.
do you have an idea. i want to avoid an error flag variable.
cheers