f



when should one use "and" and "or"

Hi All,

just got hit w the ff

> case
> when x==1 or x==2
>   print "yeoh!"
> end
SyntaxError: compile error
(irb):17: syntax error, unexpected kOR, expecting kTHEN or ':' or '\n' or ';'
when x==1 or x==2
            ^

"or" and "and" both exhibit the behaviour in ruby1.8/1.9.

workaround
1 put parens around the condition (not again)
2 replace or/and with ||/&&

i believe there are other similar quirks when using "and/or"..
has ruby relegated the use of "and/or"....?

best regards -botp

0
botp
2/5/2010 3:55:24 AM
comp.lang.ruby 48886 articles. 0 followers. Post Follow

6 Replies
743 Views

Similar Articles

[PageSpeed] 0

On Feb 4, 9:55=A0pm, botp <botp...@gmail.com> wrote:
> Hi All,
>
> just got hit w the ff
>
> > case
> > when x=3D=3D1 or x=3D=3D2
> > =A0 print "yeoh!"
> > end

x =3D 5
    =3D=3D>5
case x
  when 2,3
    p 'no'
  when 4,5
    p 'yes'
end
"yes"
    =3D=3D>nil
0
w_a_x_man
2/5/2010 5:40:13 AM
On Fri, Feb 5, 2010 at 1:45 PM, w_a_x_man <w_a_x_man@yahoo.com> wrote:
#> x =3D 5
#> =A0 =A0=3D=3D>5
#> case x
#> =A0when 2,3
#> =A0 =A0p 'no'
#> =A0when 4,5
#> =A0 =A0p 'yes'
#> end

of course i know that ;-)
im talking about the second type of "case", eg

> case
> when x.baz and y.foo
....

best regards -botp

0
botp
2/5/2010 5:55:49 AM
botp wrote:
> i believe there are other similar quirks when using "and/or"..
> has ruby relegated the use of "and/or"....?

No, but it's essentially inherited from perl. These operators have 
extremely low precedence, so without parens your statement was being 
parsed as something like

  (when x==1) or x==2

I think the typical perl use is something like:

  open F, "foo" or die "Oops";

If you use || then you need extra params so you don't get
open(F, "foo" || die "Oops");

But if you rely on poetry mode like this it may still trip you up. IMO 
you're better off with:

  a = (some-expr) || (raise "Oops")
  a = (some-expr) || raise("Oops")
-- 
Posted via http://www.ruby-forum.com/.

0
Brian
2/5/2010 7:14:11 AM
On Fri, Feb 5, 2010 at 3:14 PM, Brian Candler <b.candler@pobox.com> wrote:
> No, but it's essentially inherited from perl. These operators have
> extremely low precedence, so without parens your statement was being
> parsed as something like
>
> =A0(when x=3D=3D1) or x=3D=3D2
>
> I think the typical perl use is something like:
....

but brian, i'm still inside the "case" block. can't ruby sense that?

best regards -botp

0
botpena (164)
2/5/2010 3:03:44 PM
botp wrote:
>> �(when x==1) or x==2
>>
>> I think the typical perl use is something like:
> ....
> 
> but brian, i'm still inside the "case" block. can't ruby sense that?

I agree, but in the absence of a formal grammar you'll have to look at 
the implementation.
-- 
Posted via http://www.ruby-forum.com/.

0
b.candler (2627)
2/5/2010 5:08:42 PM
botp wrote:
> On Fri, Feb 5, 2010 at 3:14 PM, Brian Candler <b.candler@pobox.com> wrote:
>> No, but it's essentially inherited from perl. These operators have
>> extremely low precedence, so without parens your statement was being
>> parsed as something like
>>
>>  (when x==1) or x==2
>>
>> I think the typical perl use is something like:
> ....
> 
> but brian, i'm still inside the "case" block. can't ruby sense that?

And shouldn't 'when' have the same precedence as 'if'?

At least for 'or', you can use a comma instead:

case
when x==1, x==2; puts "1 or 2"
end

but it doesn't read as well, and there's nothing that works like this 
for 'and'.

0
2/5/2010 7:45:21 PM
Reply:

Similar Artilces:

"out" and "in out"
Hi i found the following explaination: In Ada, "in" parameters are similar to C++ const parameters. They are effectively read-only within the scope of the called subprogram. Ada "in out" parameters have a reliable initial value (that passed in from the calling subprogram) and may be modified within the scope of the called procedure. Ada "out" parameters have no reliable initial value, but are expected to be assigned a value within the called procedure. What does "have no reliable initial value" mean when considering the "out" parameter? By c...

about "++" and "--"
why this program snippet display "8,7,7,8,-7,-8" the program is: main() { int i=8; printf("%d\n%d\n%d\n%d\n%d\n%d\n",++i,--i,i++,i--,-i++,-i--); } > why this program snippet display "8,7,7,8,-7,-8" Ask your compiler-vendor because this result is IMHO implementation-defined. Check this out: http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.15 http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.16 Regards, Irina Marudina fxc123@gmail.com wrote: > why this program snippet display "8,7,7,8,-7,-8&q...

why "::", not "."
Why does the method of modules use a dot, and the constants a double colon? e.g. Math::PI and Math.cos -- Posted via http://www.ruby-forum.com/. On Oct 26, 2010, at 01:48 , Oleg Igor wrote: > Why does the method of modules use a dot, and the constants a double > colon? > e.g. > Math::PI and Math.cos For the same reason why inner-classes/modules use double colon, because = they're constants and that's how you look up via constant namespace. Math::PI and ActiveRecord::Base are the same type of lookup... it is = just that Base is a module and PI is a float....

Urgent need """""""""""INFORMATICA DEVELOPER"""""""""""""
Hello Partners, How are you ? Please find the requirements below. Title: Database/ETL Developer Duration: 6 months Location: NY Exp: 7+ Locals preferred Database/ETL requirements (Mandatory) Candidate must have worked with financial instruments, preferably Mutual Funds but, Equities are also ok. PL/SQL - packages, Stored procs, Functions, Aggregate functions, Pipelined Functions Informatica 8.6 - especially complex mappings, complex maplets, complex workflows, transformations Oracle 10g/11g Unix/Linux shell scripting Database/ETL requirements (Optional) Data warehousing experience Threading and job concepts in 10g/11g Cost based Optimizer concepts in 10g/11g Must : Experience with XML files and partitioning concepts in Oracle, Collections, Material Views Note : No phone calls please. : send Resumes to karthik@bhaninfo.com Thanks & Regards Karthik BhanInfo karthik@bhaninfo.com ...

"In" "Out" and "Trash"
I just bought a new computer and I re-installed Eudora Light on my new computer. But when I open Eudora, the "In", "Out" and "Trash" links are not on the left side of the screen the way they were on my old computer. How can I get these links back on the left side of the screen? Thank you. On 25 Mar 2007 09:49:22 -0700, "abx" <abfunex@yahoo.com> wrote: >I just bought a new computer and I re-installed Eudora Light on my new >computer. But when I open Eudora, the "In", "Out" and "Trash" links >are ...

Urgent JAVA Requirement in """"""NEW YORK"""""""""
Hello Partners, How are you ? Please find the requirement below. Location : NY Duration : 8 mnths Rate :Open Job description: Java/J2EE Web Service Developer =B7 (4+ years of application development experience in Java/J2EE and Web service technologies. =B7 Experience with spring & Hibernate. =B7 Experience with J2EE Application Server (preferably Web logic). =B7 Preferable Aqua logic DSP Experience =B7 Preferable Sonic ESB Composite Service experience Experience working in UNIX environment. Good working knowledge of Oracle RDBMS and PL/SQL) Note : Please send your updated resume along with contact no to Karthik@bhaninfo.com. Thanks & Regards Karthik BhanInfo karthik@bhaninfo.com # 1572, 7 th Floor Suites, 1 Blue Hill Plaza Pearl River,NY-10965 www.bhaninfo.com ...

Question about "sprintf" "@" "do for"
Hello, this works: A1=3D1 A2=3D2 A3=3D3 i=3D1 vari=3Dsprintf("A%.f",i) print vari,"=3D",@vari i=3Di+1 vari=3Dsprintf("A%.f",i) print vari,"=3D",@vari i=3Di+1 vari=3Dsprintf("A%.f",i) print vari,"=3D",@vari do for [i=3D1:3]{ vari=3Dsprintf("A%.f",i) print vari } But I want to have "print vari,"=3D",@vari" in the loop. But it dosen't=20 work. Why can't I use "print vari,"=3D",@vari" in the loop? Is there a=20 solution for? J=C3=B6rg Jörg ...

Gary Sokolich """"""
"""""""""" http://www.manta.com/c/mmlq5dm/w-gary-sokolich W Gary Sokolich 801 Kings Road Newport Beach, CA 92663-5715 (949) 650-5379 http://www.tbpe.state.tx.us/da/da022808.htm TEXAS BOARD OF PROFESSIONAL ENGINEERS February 28, 2008 Board Meeting Disciplinary Actions W. Gary Sokolich , Newport Beach, California �V File B-29812 - It was alleged that Dr. Sokolich unlawfully offered or attempted to practice engineering in Texas (...) Dr. Sokolich chose to end the proceedings by signing a Consent Order that was accepted by ...

puts "\\".gsub("\\", "\\\\")
Hello, I have a mini-ruby quiz. Guess what this line of code writes to the console, then try it for yourself: puts "\\".gsub("\\", "\\\\") Why is that so? Martin From: martinus [mailto:martin.ankerl@gmail.com]=20 # Hello, I have a mini-ruby quiz. Guess what this line of code writes to # the console, then try it for yourself: # puts "\\".gsub("\\", "\\\\") puts "\\".gsub("\\", "\\\\") \ #=3D> nil # Why is that so? faq. escaping the escape in sub/gsub. search the archives. maybe you want somethin...

Use "range," not "for"?
Thanks to everyone who suggested ways to get a proc to recognize all variables in the global scope--here's why I wanted to know. (Maybe someone has done this better already--if so, don't hesitate to let me know!) I've found that a simple "foreach" loop fairly often won't do the jobs I want done, and I need to use "for"--ugh! Well, I recently read at least part of a book about Python. It didn't look like an improvement over Tcl for the most part, but it has at least a few good features, one of which is the "range" expression. I thought a "range" procedure in Tcl might be a good substitute for the clumsy "for." So, here's my first effort. Instead of writing this: for {set i 1} {$i<= 10} {incr i} {puts "$i. \"for\" is ugly!"} you can write this: range i 1 to 10 {puts "$i. \"range\" is beautiful!"} Or, if you have a list called "lum," instead of writing this: for {set i 0} {$i < [llength $lum]} {incr i} {puts [lindex $lum $i]} you can write this: range i 0 no [llength $lum] {puts [lindex $lum $i]} To go backward, skipping every other number, instead of this: for {set i 10} {$i >= 0} {incr i -2} {puts $i} you can write this: range i 10 to 0 -2 {puts $i} The usage is pretty obvious: "range var start cutoff end ?incr? body." In other words, (1) the word "range"; (2) a vari...

Why does "One Zero" mean "Ten" and not "One Zero"?
Who asked if we said "10" equals "One Zero" like it says what could we conceive as a benefit from this? The answer to the question is me: "Eleaticus" (actually) Musatov. Though we may have answered the question we have not answered [the] question contained [within] the question. Let us do this now: I can think of two benefits. 1. The disparaging difference between logic numbers and language narrows. (And) 2. A new system is adopted: one better suited to sensible expression. Let us examine the problem of the "Ill Placed Zero": 1. When we say 0 it...

"==" is NOT TRUE "==", WHY?
[CODE START] x=5; y=8; if (x + y + 1E-15 == 13) a = 3 else a = 8 end [CODE END] When x + y + 1E-15, the code above returns a = 8. When x + y + 1E-16, the code above returns a = 3. Why? Kindly advise. Thanks. "onemilimeter Chen" <onemm@example.com> wrote in message <g7adrj$5tr$1@fred.mathworks.com>... > [CODE START] > x=5; > y=8; > if (x + y + 1E-15 == 13) > a = 3 > else > a = 8 > end > [CODE END] > > When x + y + 1E-15, the code above returns a = 8. > When x + y + 1E-16, the code above returns a = 3. > > Why? ...

how to change "/" to "\"
iam new to shell scripting and i have plz can anyone help in changing the pattrern "/" to "\" using the sed command. olympie@gmail.com wrote: > iam new to shell scripting and i have plz can anyone help in changing > the pattrern "/" to "\" using the sed command. sed 's/\//\\/g' will replace all '/' with '\' srp -- http://saju.net.in Saju Pillai <saju.pillai@gmail.com> wrote: >> iam new to shell scripting and i have plz can anyone help in changing >> the pattrern "/" to "\" using ...

Re: "out" and "in out"
"no reliable initial value" means, you cannot rely on the value, but it may have a value. This depends on the parameter passing mechanism, which is _not_ related to the parameter mode (contrary to what many people think). So the parameter mode is there (nearly) solely for the information of the reader. The parameter passing mechanism for all kinds of parameters is defined in the RM. There are parameters passed by copy (in and out), by reference; for some it is explicitly left undefined. In your case, the passing mechanism is by reference, so you get what you get. But don't rely...

why have both "." and "->" ?
I used to remember why c++ needed both ? Could somebody help me here ? For example class A{ f(); }; A* aa; You could do either "aa->f()" or "(*aa).f()". So why does C++ need both operators. Raj "raj" <rajkumar@hotmail.com> schreef in bericht news:d7fee6d0.0406230748.694b966b@posting.google.com... > I used to remember why c++ needed both ? > Could somebody help me here ? > > For example > > class A{ > f(); > }; > > A* aa; > > You could do either "aa->f()" or "(*aa).f()". So why does C++...

"A" is not equal "A"?
I was trying to convert String Array to String A through cycle and than compare the result with String B but eventhough results were the same it returned false. I'am working in BlueJ 3.1.0 Code is: public boolean Method () { String[] Example = {"a","b","c"}; String A = ""; String B = "abc" for (int i = 0; i<3 ; i++) { A = A + Example[i]; } if (A == B) { return true; } } never got true... I appreciate any suggestions how to solve this Den 08.04.2014 10:06, skrev 245dav@gmail.com:...

what does the "+" and the "-" mean?
I recently read what the "+" and the "-" signs mean for compact discs. Of course, a friend has just asked me and I now forget. I've searched all over the place with no results. Anyone know? TIA! .. -------------------------------------- Mike Richter, were you born with "Scam Artist" emblazoned on your face? -------------------------------------- roscoe james wrote: > > I recently read what the "+" and the "-" signs mean for compact discs. Of > course, a friend h...

Is there any """Anti Stringizing operator #"""
Hi everybody:D I've a string that contains the name of a class. Some members told that I can use """Stringizing Operator (#)""", but the problem is here, that I have the string, & I want something vice- versa. As we know with """Stringizing Operator (#)""", we can get the stirng name of a class or ... str <--- #ClassA But I want to instantiate a class that I have just it's name as a string. ClassA * clsA = new ??? <--- Str ??? How can I do it? I don't want to use """HardCoding"&qu...

"plot", "imshow" and "quiver"
Hi all. So recently I made the discovery that when use "imshow" command, the YDir parameter under Axis Properties is set to "reverse" by default. To me this seems counter intuitive. Why wouldn't YDir be "normal" by default? When set at "normal" it turns out that my image is actually flipped upside down. On the contrary, when use "quiver" command to plot a vector field, the YDir is set at "normal" by default. Even though this makes more sense to me, because "reversed" YDir is the correct way to display an image (as in "imshow"), the result of "quiver" is that the vector fields this time depict the objects as if they are upside down... So every time I use "quiver" I always add the following line to revert the objects back to its normal orientation... set(gca, 'YDir', 'reverse')... In addition, "plot" uses "normal" setting for YDir. The only issue I have is that I use these commands fairly frequently in my job and sometimes they can get a little bit confusing in terms of display. So I wonder why MATLAB hasn't standardized them so that the objects always get displayed the same way. It's just a comment I have. Thank you. "Clare " <flyingclare@gmail.com> wrote in message news:hefvdk$3md$1@fred.mathworks.com... > Hi all. So recently I made the discovery that when use "imshow" command, > the ...

["a", "b", "c", "d"] to "a, b, c, d"?
I want to process each element of an array, but the last element should be handled special. Here is an example: def p_ary(ary) str = "" ary.each do |elem| str << elem << ", " end str.chomp!(", ") str end so p_ary(["a", "f", "x", "test"]) produces "a, f, x, test". The code works, but isn't there an easier and more general way for this behaviour? martinus On Tue, 06 Apr 2004 04:23:22 -0700, Martin wrote: > I want to process each element of an array, but the last ele...

Using "fsolve": How to control iteration using "residuals" and "norm of step"??
Hello Everyone, I am using fsolve to solve a system of linear equations and it&#8217;s working fine. However, I have a coupe of issues: (i) Even when the solution (alpha) converges, I am getting the message to choose a new point. I am guessing that it has to do with &#8220;norm of step&#8221; as probably the code is trying a smaller step. I am using option like this: options = optimset('Jacobian','on', 'algorithm', 'Levenberg-Marquardt','Display','iter-detailed', 'MaxFunEvals', 10000, 'MaxIter', 10000,'TolX'...

help with "timefmt" and "using"...
Hi, is it possible to plot a data file like the following fragment without rebuild the archive? #DATE TIME-BEG TIME-END VALUE 7/03/2001 15:15:00 16:15:00 40115 7/03/2001 16:15:00 17:15:00 39960 7/03/2001 17:15:00 18:15:00 39990 8/03/2001 09:15:00 10:15:00 39985 8/03/2001 10:15:00 11:15:00 39855 8/03/2001 11:15:00 12:15:00 40310 I need to use as X data the 1st and the 3th columns, Y the last. I've spent a lot of time to try but with no result... Thanks a lot!!! Ciao, Francesco. Francesco <odiolaspam@tin.it> wrote: > Hi, > is it possible to plot a data file like the following fragment > without rebuild the archive? Sort of. You'll have to filter the file, but you can do that on-the-fly if you're on Unix or something sufficiently like it: plot '< awk "{print $1,$3,$4}" datafile' using 1:3 For other than timefmt data, that wouldn't be so much of a hassle, but unfortunately, the strptime() function that is the bases of 'set timefmt' offers no way of ignoring parts of the input. -- Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de) Even if all the snow were burnt, ashes would remain. On 23 Oct 2003 09:01:23 GMT, Hans-Bernhard Broeker <broeker@physik.rwth- aachen.de> wrote: > if you're on Unix or something sufficiently like it: > > plot '< awk "{print $1,$3,$4}" datafile' using 1:3 > > For other than timefmt data...

Using "If" and "NDSolve" together
Hello! I have a tricky job to do: I want to simulate something, and want to assign a value to a variable under certain conditions. Those are, if time is within a certain interval, I want that variable to have that value. Otherwise, I want it to have other value. Until now, I have been trying to use the following command: Funcao[first_, last_] := Plot[A[t] /. NDSolve[ {A'[t] == k0 A0 - k1 A[t], B'[t] == k1 A[t] - k2 B[t], C'[t] == k2 B[t] - k3 C[t], A[0] == 2 10^-6, B[0] == 0, C[0] == 0} /. k0 -> 10^-2 /. k1 -> 10^-5 /. k2 -&g...

When to use "perror" and "fprintf"
Reading the man pages and some code did not really help me in understanding the difference between - or better when I should use - perror("...") and fprintf(stderr, "...") Any hint or help is appreciated. L. Westmeier "L. Westmeier" <westmeier@informatik.hu-berlin.de> wrote: > Reading the man pages and some code did not really help me in > understanding the difference between - or better when I should use - > perror("...") and fprintf(stderr, "...") They do rather different things. You use perror() to print a message to...

Web resources about - when should one use "and" and "or" - comp.lang.ruby

Resources last updated: 3/20/2016 8:04:17 PM