From: Ned Konz Date: 2001-08-15T03:50:44+09:00 Subject: [ruby-talk:19720] Re: and in postconditions On Tuesday 14 August 2001 11:13 am, you wrote: > Hi everyone, > I've found something very strange. "and" doesn't > seem to do what I expect it to do when used with postconditions. > In Perl, however, it does. > > $ cat test.pl > sub test($) { > my $i = shift; > print "TRUE!\n" and return true if ($i > 10); > print "FALSE!\n"; > return false; > } > print test(20), "\n"; > print test(2), "\n"; Note that true and false aren't keywords in Perl (you are doing the equivalent of return 'true'), and that the ($) prototype on test doesn't do anything useful (you should avoid prototypes in Perl unless you know for sure when you shouldn't). I'd recommend 'use strict' and the -w flag when doing Perl, just to catch problems like the barewords true and false above ('true' and 'false' are both true in Perl). In Perl, the print() returns 1 when it succeeds, so "print "TRUE" and return true" executes the right hand side of the and. In Ruby, print returns nil, so the right hand side of the and doesn't get executed. -- Ned Konz currently: Stanwood, WA email: ned@bike-nomad.com homepage: http://bike-nomad.com