Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > print(true and true) #=> the parenthesis issue

Reply
Thread Tools

print(true and true) #=> the parenthesis issue

 
 
Dave Bass
Guest
Posts: n/a
 
      06-17-2008
I've always used "&&" rather than "and". I know the two operators have
different precedences. The "&&" version comes naturally to me, thanks to
a C background.

Can someone give non-contrived examples where "and" works nicely but
"&&" doesn't? I.e. what is the reason for including "and" in the
language, as it seems redundant to me. (The Pickaxe says "just to make
life interesting".)
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
botp
Guest
Posts: n/a
 
      06-17-2008
On Tue, Jun 17, 2008 at 10:07 PM, Dave Bass <> wrote:
> I've always used "&&" rather than "and". I know the two operators have
> different precedences. The "&&" version comes naturally to me, thanks to
> a C background.


hacker

> Can someone give non-contrived examples where "and" works nicely but
> "&&" doesn't? I.e. what is the reason for including "and" in the
> language, as it seems redundant to me. (The Pickaxe says "just to make
> life interesting".)


1 reads better in english. i also would assume you do not like "or"
2 handy for control flow

otoh && is handy for value manipulation, eg,

:001:0> x= true and false
=> false
:002:0> x
=> true
:003:0> x= true && false
=> false
:004:0> x
=> false
irb(main):005:0> x= (true and false)
=> false
irb(main):006:0> x
=> false

"and" and "or" comes natural (look i'm already using them w their
low precedence in ruby, so you do not have to put a lot of parens when
linking compound logical expressions. Sadly, there are special cases
that the opposite (ie parens are mandatory) may be true (bug or not).
Also, "and/or" names suggest their use and i do not even have to dig
the manuals nor think hard just to get their meaning...

kind regards -botp

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      06-17-2008
On 17 Jun., 16:07, Dave Bass <daveb...@musician.org> wrote:
> I've always used "&&" rather than "and". I know the two operators have
> different precedences. The "&&" version comes naturally to me, thanks to
> a C background.


I use "&&" in expressions (because of the precedence) and "and" when I
want to combine some "statements".

> Can someone give non-contrived examples where "and" works nicely but
> "&&" doesn't? I.e. what is the reason for including "and" in the
> language, as it seems redundant to me. (The Pickaxe says "just to make
> life interesting".)


16:35:55 OPSC_Gold_bas_dev_R1.2.2_prj$ ruby -ce 'x = some_work() &&
puts x'
-e:1: syntax error, unexpected tIDENTIFIER, expecting kDO or '{' or
'('
16:36:09 OPSC_Gold_bas_dev_R1.2.2_prj$ ruby -ce 'x = some_work() and
puts x'
Syntax OK

Does that give a clue?

Kind regards

robert
 
Reply With Quote
 
hakunin
Guest
Posts: n/a
 
      06-17-2008
On Jun 17, 10:37*am, Robert Klemme <shortcut...@googlemail.com> wrote:
> On 17 Jun., 16:07, Dave Bass <daveb...@musician.org> wrote:
>
> > I've always used "&&" rather than "and". I know the two operators have
> > different precedences. The "&&" version comes naturally to me, thanks to
> > a C background.

>
> I use "&&" in expressions (because of the precedence) and "and" when I
> want to combine some "statements".
>
> > Can someone give non-contrived examples where "and" works nicely but
> > "&&" doesn't? I.e. what is the reason for including "and" in the
> > language, as it seems redundant to me. (The Pickaxe says "just to make
> > life interesting".)

>
> 16:35:55 OPSC_Gold_bas_dev_R1.2.2_prj$ ruby -ce 'x = some_work() &&
> puts x'
> -e:1: syntax error, unexpected tIDENTIFIER, expecting kDO or '{' or
> '('
> 16:36:09 OPSC_Gold_bas_dev_R1.2.2_prj$ ruby -ce 'x = some_work() and
> puts x'
> Syntax OK
>
> Does that give a clue?
>
> Kind regards
>
> robert


The way 'and' differs from &&, and || differs from 'or' is pretty
clear. Question is - what to do about this special case? It's
completely logical when you follow interpreter logic, as it adheres to
KISS (from interpreter perspective) however, it does not adhere to
POLS from language user's view angle. One would say "you have to learn
your interpreter", but I would argue that language is what we learn
and interpreter is just a tool used by the language. Interpreter is
there to interpret language, language is not there to adhere to the
interpreter. Am I making sense?
 
Reply With Quote
 
botp
Guest
Posts: n/a
 
      06-17-2008
On Tue, Jun 17, 2008 at 10:58 PM, hakunin <> wrote:
> The way 'and' differs from &&, and || differs from 'or' is pretty
> clear. Question is - what to do about this special case?


someone to submit a patch and that others may test.

> Am I making sense?


yes

 
Reply With Quote
 
Dave Bass
Guest
Posts: n/a
 
      06-18-2008
Robert Klemme wrote:
> $ ruby -ce 'x = some_work() and puts x'
> Does that give a clue?


Isn't that the same as this?

x = some_work()
puts x unless x == false

I prefer the two-line version; clarity wins over conciseness every time
for me.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      06-18-2008
2008/6/18 Dave Bass <>:
> Robert Klemme wrote:
>> $ ruby -ce 'x = some_work() and puts x'
>> Does that give a clue?

>
> Isn't that the same as this?
>
> x = some_work()
> puts x unless x == false
>
> I prefer the two-line version; clarity wins over conciseness every time
> for me.


If you're in favour of clarity, why not

x = some_work()
putx x if x

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

 
Reply With Quote
 
Dave Bass
Guest
Posts: n/a
 
      06-18-2008
Robert Klemme wrote:
> x = some_work()
> putx x if x


I carefully set my little trap... and it caught someone!

Note that I wrote "Clarity wins over conciseness every time for me." The
more English-like the code is, the easier it is to understand (for me
anyway). The fact that my version is a few characters longer is
unimportant. Hopefully it won't phase Ruby; anyway, I'm the boss and it
can do what I say.



Dave
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      06-18-2008
Hi --

On Thu, 19 Jun 2008, Dave Bass wrote:

> Robert Klemme wrote:
>> x = some_work()
>> putx x if x

>
> I carefully set my little trap... and it caught someone!
>
> Note that I wrote "Clarity wins over conciseness every time for me." The
> more English-like the code is, the easier it is to understand (for me
> anyway). The fact that my version is a few characters longer is
> unimportant. Hopefully it won't phase Ruby; anyway, I'm the boss and it
> can do what I say.


Your version only tests for false, not both false and nil, so it's not
equivalent to the 'and' one or to Robert's last one. The abstraction
of the nil and false objects into the category of boolean falsehood,
which will fail an 'if' test, isn't *that* hard to get And then
you get to have code that's both short and clear


David

--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS June 16-19 Berlin
ADVANCING WITH RAILS July 21-24 Edison, NJ
See http://www.rubypal.com for details and updates!

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      06-19-2008
2008/6/18 Dave Bass <>:
> Robert Klemme wrote:
>> x = some_work()
>> putx x if x

>
> I carefully set my little trap... and it caught someone!


Yes, I can see you struggling in it.

> Note that I wrote "Clarity wins over conciseness every time for me."


So then why did you care to go through multiple negations?

> The
> more English-like the code is, the easier it is to understand (for me
> anyway).


Exactly. That's why I offered a variant that not only has the same
semantics as my version with "and" but also reads much better than
your double negation.

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

 
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
problems with regular expressions and parenthesis Daniel Fac Ruby 3 11-02-2008 01:26 AM
What do these dollar signs and parenthesis do? gimme_this_gimme_that@yahoo.com Javascript 14 09-28-2008 09:50 PM
OR and AND without parenthesis IveCal Java 4 06-22-2007 08:07 AM
preprocessor and parenthesis effbiae C Programming 5 02-07-2006 03:22 PM
Regular expressions and parenthesis in match text Carl Cunningham Perl Misc 2 09-15-2003 11:02 AM



Advertisments