# Case with && not working compared to using if, works!

**URL:** <https://www.ruby-forum.com/t/case-with-not-working-compared-to-using-if-works/180162>\
**Category:** Ruby\
**Created:** [December 13, 2009, 9:16pm UTC](https://www.ruby-forum.com/t/case-with-not-working-compared-to-using-if-works/180162 "2009-12-13T21:16:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bloodniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bloodniece](https://www.ruby-forum.com/u/bloodniece)\
**Post date:** [December 13, 2009, 9:16pm UTC](https://www.ruby-forum.com/t/case-with-not-working-compared-to-using-if-works/180162/1 "2009-12-13T21:16:44Z")

</div>

Hi All,

I looked through many threads for case issues and they did not help me.  
I want to use case just because for this situation:

PIDF = “/usr/local/vr/prod/tmp/pids/mongrel.pid”  
d = Date.today  
t = Time.now  
pidhash = Hash.new  
pidhash[PIDF] = %x(ps auxwww |grep [m]ongrel|grep 80|awk ‘{print  
$2}’).chomp

File.open(PIDF).each do |line|  
hasval = pidhash.value?(line) ### store true/false  
if hasval  
log\_mtd("Running PID == PID in File  
".+(d.to\_s).+(t.hour.to\_s).+(t.min.to\_s))  
exit  
elsif (!hasval && File.size(PIDF) == 0)  
puts “PID not active yet file exists”, PIDF  
exit  
elsif (!hasval && File.size(PIDF) \> 0)  
puts "  
exit  
else  
puts “no con met”  
exit  
end  
end

I tried this syntax and it was not meeting my expected output:  
So when I substitute line with a random # I expect the 2nd condition to  
be printed which it is, but its not even evaling the File.size(PIDF). I  
know this b/c I test it with a hard-coded file size.

hasval = pidhash.value?(7) ### store true/false  
case hasval  
when true  
then  
puts("Running PID == PID in File  
".+(d.to\_s).+(t.hour.to\_s).+(t.min.to\_s))  
when (false && File.size(PIDF) == 1)  
then  
puts “2nd. PID not active yet file exists”, PIDF  
when (false && File.size(PIDF) == 0)  
then  
puts “3rd”  
else  
puts “no con met”  
exit  
end

test: 77 lines, 1826 characters.  
[root@v /usr/local/vr/test/script]# ruby test  
PID not active yet file exists  
/usr/local/vr/prod/tmp/pids/mongrel.pid

[root@v /usr/local/vr/test/script]# ls -l  
/usr/local/vr/prod/tmp/pids/mongrel.pid  
-rw-r–r-- 1 root wheel 5 Nov 16 12:17  
/usr/local/vr/prod/tmp/pids/mongrel.pid

thank you!

---

<div class="post-metadata">

**Author:** ![bloodniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bloodniece](https://www.ruby-forum.com/u/bloodniece)\
**Post date:** [December 14, 2009, 12:31am UTC](https://www.ruby-forum.com/t/case-with-not-working-compared-to-using-if-works/180162/2 "2009-12-14T00:31:05Z")

</div>

On Dec 13, 2009, at 12:22 , Derek S. wrote:

> ```
> then
> puts "3rd"
> 
> ```
> 
> else  
> puts “no con met”  
> exit  
> end

It just doesn’t work this way. It is nonsensical. Stick to your  
if/elsif/else and things will work fine.

About the nonsensical:

case x  
when a  
when b  
…

is roughly equivalent to:

if a === x  
elsif b === x  
…

So what does this mean?

case hasval  
when (false && File.size(PIDF) == 1)

it roughly translates to:

if (false && File.size(PIDF) == 1) === hasval

or by logical deduction:

if false === hasval

Which I doubt you meant.

Again, stick to simple if/elsif/else statements and you’ll be happier.

Also, your code drives me bonkers. Check it:

puts("Running PID == PID in File ". +(d.to\_s). +(t.hour.to\_s).  
+(t.min.to\_s))

is about as pedantically awkward as you can get. Dot notation for the +  
operator?? NO!

At worst it should be:

puts "Running PID == PID in File " + d.to\_s + t.hour.to\_s + t.min.to\_s

Hey look! My space bar works! Also, My pinkies aren’t so tired by typing  
so many parens!

Even better, use interpolation:

puts “Running PID == PID in File #{d}#{t.hour}#{t.min}”

(tho that looks like it’ll print out ugly, it is equivalent to your  
original code and is much more readable.)

---

<div class="post-metadata">

**Author:** ![bloodniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bloodniece](https://www.ruby-forum.com/u/bloodniece)\
**Post date:** [December 14, 2009, 1:11am UTC](https://www.ruby-forum.com/t/case-with-not-working-compared-to-using-if-works/180162/3 "2009-12-14T01:11:23Z")

</div>

> Even better, use interpolation:
> 
> puts “Running PID == PID in File #{d}#{t.hour}#{t.min}”
> 
> (tho that looks like it’ll print out ugly, it is equivalent to your  
> original code and is much more readable.)

LOL…advice taken. Thanks Ryan. 🙂

---

<div class="post-metadata">

**Author:** ![bloodniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bloodniece](https://www.ruby-forum.com/u/bloodniece)\
**Post date:** [December 14, 2009, 2:44am UTC](https://www.ruby-forum.com/t/case-with-not-working-compared-to-using-if-works/180162/4 "2009-12-14T02:44:35Z")

</div>

Hi,

Am Montag, 14. Dez 2009, 05:22:00 +0900 schrieb Derek S.:

> ```
> when (false && File.size(PIDF) == 0)
> then
> puts "3rd"
> else
> puts "no con met"
> exit
> 
> ```
> 
> end

Why use case or elsif at all? What you meant is probably:

if pidhash.value? 7 then  
puts “1st: Running PID == PID in File #{d} #{t}”  
else  
if File.size(PIDF).nonzero? then  
puts “2nd: PID not active yet file exists: #{PIDF}”  
else  
puts “3rd”  
end  
end

Bertram
