Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > case with && not working compared to using if, works!

Reply
Thread Tools

case with && not working compared to using if, works!

 
 
Derek Smith
Guest
Posts: n/a
 
      12-13-2009
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!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Ryan Davis
Guest
Posts: n/a
 
      12-13-2009

On Dec 13, 2009, at 12:22 , Derek Smith wrote:

> hasval =3D pidhash.value?(7) ### store true/false
> case hasval
> when true
> then
> puts("Running PID =3D=3D PID in File
> ".+(d.to_s).+(t.hour.to_s).+(t.min.to_s))
> when (false && File.size(PIDF) =3D=3D 1)
> then
> puts "2nd. PID not active yet file exists", PIDF
> when (false && File.size(PIDF) =3D=3D 0)
> 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 =3D=3D=3D x
elsif b =3D=3D=3D x
...

So what does this mean?

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

it roughly translates to:

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

or by logical deduction:

if false =3D=3D=3D 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 =3D=3D 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 =3D=3D 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 =3D=3D 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.)


 
Reply With Quote
 
 
 
 
Derek Smith
Guest
Posts: n/a
 
      12-14-2009
> 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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Bertram Scharpf
Guest
Posts: n/a
 
      12-14-2009
Hi,

Am Montag, 14. Dez 2009, 05:22:00 +0900 schrieb Derek Smith:
>
> 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


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


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
*
Discover String#notempty? at <http://raa.ruby-lang.org/project/step>.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Emacs, VHDL Mode - Upper Case Enum Values & Upper Case Constants (not working)? BlackHelicopter VHDL 0 01-31-2013 04:12 AM
Advantages of using the Visual Studio Development Server (VSDS)compared to IIS jeeji ASP .Net 5 03-18-2008 09:43 PM
Re: Why Linux and Windows NT series are not fairly compared roy.schestowitz@yahoo.com Java 5 06-20-2006 01:15 PM
how to case select with case-insensitive string ? Tee ASP .Net 3 06-23-2004 07:40 PM
Possible to turn on/off cookieless sessions dynamically on a case by case basis at run-time? Steve Franks ASP .Net 2 06-10-2004 02:04 PM



Advertisments