Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
n00b if condition quest
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Brett Boge  
View profile  
(1 user)  More options Jul 26 2007, 6:22 pm
Newsgroups: comp.lang.ruby
From: Brett Boge <brett.b...@igt.com>
Date: Fri, 27 Jul 2007 07:22:45 +0900
Local: Thurs, Jul 26 2007 6:22 pm
Subject: n00b if condition quest
I'm sure theres a way, so how does one do:

if (a = b or a = c or a = d or a = f)

in a shorter, easier to view

if (a = (b c d or f)) kind of way?
--
Posted via http://www.ruby-forum.com/.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
dbl...@rubypal.com  
View profile  
 More options Jul 26 2007, 6:24 pm
From: dbl...@rubypal.com
Date: Fri, 27 Jul 2007 07:24:47 +0900
Local: Thurs, Jul 26 2007 6:24 pm
Subject: Re: n00b if condition quest
Hi --

On Fri, 27 Jul 2007, Brett Boge wrote:
> I'm sure theres a way, so how does one do:

> if (a = b or a = c or a = d or a = f)

> in a shorter, easier to view

> if (a = (b c d or f)) kind of way?

You mean == rather than = , but in any case, try this:

   if [b,c,d,f].include?(a)

David

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
   RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
     & consulting:  Ruby Power and Light, LLC (http://www.rubypal.com)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brett Boge  
View profile  
 More options Jul 26 2007, 6:29 pm
Newsgroups: comp.lang.ruby
From: Brett Boge <brett.b...@igt.com>
Date: Fri, 27 Jul 2007 07:29:54 +0900
Local: Thurs, Jul 26 2007 6:29 pm
Subject: Re: n00b if condition quest
== indeed.

That's gorgeous, thanks! Works brilliantly.

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Phrogz  
View profile  
 More options Jul 26 2007, 6:34 pm
Newsgroups: comp.lang.ruby
From: Phrogz <phr...@mac.com>
Date: Fri, 27 Jul 2007 07:34:59 +0900
Local: Thurs, Jul 26 2007 6:34 pm
Subject: Re: n00b if condition quest
On Jul 26, 4:22 pm, Brett Boge <brett.b...@igt.com> wrote:

> I'm sure theres a way, so how does one do:

> if (a = b or a = c or a = d or a = f)

> in a shorter, easier to view

> if (a = (b c d or f)) kind of way?

case n
  when 1,2,3: puts '1-3'
  when 4..6: puts '4-6'
  else puts 'other'
end

You can, of course, use variables instead of literals, ala

case a
  when b, c, d, f
    ...
end


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
dbl...@rubypal.com  
View profile  
 More options Jul 26 2007, 7:38 pm
From: dbl...@rubypal.com
Date: Fri, 27 Jul 2007 08:38:04 +0900
Local: Thurs, Jul 26 2007 7:38 pm
Subject: Re: n00b if condition quest
Hi --

That doesn't test for equality, though.

David

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
   RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
     & consulting:  Ruby Power and Light, LLC (http://www.rubypal.com)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
 
View profile  
 More options Jul 26 2007, 11:23 pm
From: Peņa, Botp <b...@delmonte-phil.com>
Date: Fri, 27 Jul 2007 12:23:55 +0900
Local: Thurs, Jul 26 2007 11:23 pm
Subject: Re: n00b if condition quest
On Behalf Of Brett Boge:
# == indeed.

also take a look at #any? and #all?

they come in handy in situations like,

if a < b or a < c or a < d

if a > b and a > c and a > d

sometimes, you may want a reverse to #include? behavior so that you'd like to emphasize first the element as compared to the collection. something like,

a.in?[b,c,d,f]

kind regards -botp


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Dober  
View profile  
 More options Jul 27 2007, 6:43 am
From: "Robert Dober" <robert.do...@gmail.com>
Date: Fri, 27 Jul 2007 19:43:56 +0900
Local: Fri, Jul 27 2007 6:43 am
Subject: Re: n00b if condition quest
On 7/27/07, dbl...@rubypal.com <dbl...@rubypal.com> wrote:

> Hi --

> On Fri, 27 Jul 2007, Brett Boge wrote:

> > I'm sure theres a way, so how does one do:

> > if (a = b or a = c or a = d or a = f)

> > in a shorter, easier to view

> > if (a = (b c d or f)) kind of way?

> You mean == rather than = , but in any case, try this:

>    if [b,c,d,f].include?(a)

You are right for sure that OP meant ==, but let us answer the
original question too ;)

a = [b,c,d,e,f].compact.first

Robert
--
[...] as simple as possible, but no simpler.
-- Attributed to Albert Einstein


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Whitlock  
View profile  
 More options Jul 27 2007, 5:58 pm
Newsgroups: comp.lang.ruby
From: Ian Whitlock <iw1j...@comcast.net>
Date: Sat, 28 Jul 2007 06:58:11 +0900
Local: Fri, Jul 27 2007 5:58 pm
Subject: Re: n00b if condition quest

Robert Dober wrote:
> You are right for sure that OP meant ==, but let us answer the
> original question too ;)

> a = [b,c,d,e,f].compact.first

Not quite.

b=c=d=false
e=f = 99
a1 = [b,c,d,e,f].compact.first
a2=b or a2=c or a2=d or a2=e or a2=f
puts a1, a2

a1 is false and a2 is 99

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Phrogz  
View profile  
 More options Jul 27 2007, 6:10 pm
Newsgroups: comp.lang.ruby
From: Phrogz <phr...@mac.com>
Date: Sat, 28 Jul 2007 07:10:01 +0900
Local: Fri, Jul 27 2007 6:10 pm
Subject: Re: n00b if condition quest
On Jul 26, 5:38 pm, dbl...@rubypal.com wrote:

> On Fri, 27 Jul 2007, Phrogz wrote:
> > On Jul 26, 4:22 pm, Brett Boge <brett.b...@igt.com> wrote:
> >> I'm sure theres a way, so how does one do:
> >> if (a = b or a = c or a = d or a = f)
> >> in a shorter, easier to view
> >> if (a = (b c d or f)) kind of way?

> > case n
> >  when 1,2,3: puts '1-3'
> >  when 4..6: puts '4-6'
> >  else puts 'other'
> > end
> That doesn't test for equality, though.

An important semantic point. For example:

  case 1..3
    when 1..3: puts 'yay!'
    else       puts 'boo'
  end

results in "boo", because (1..3) === (1..3) #=> false

Still, as the docs for Object#=== say:
"For class Object, effectively the same as calling #==, but typically
overridden by descendants to provide meaningful semantics in case
statements."

Numbers, strings, arrays, hashes, booleans...all these treat === as
==. I'm not arguing that they should be treated the same, or that we
should sweep the difference under the rug. I'm simply suggesting that
if you know the difference between #== and #===, particularly on the
objects that you place in your case statements, then under many
circumstances you can use a case statement as a convenience for
checking equality on many objects at once.

(Not that there was anything wrong with your initial suggestion of
Array#any?, of course.)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Phrogz  
View profile  
 More options Jul 27 2007, 6:10 pm
Newsgroups: comp.lang.ruby
From: Phrogz <phr...@mac.com>
Date: Sat, 28 Jul 2007 07:10:01 +0900
Local: Fri, Jul 27 2007 6:10 pm
Subject: Re: n00b if condition quest
On Jul 27, 4:43 am, "Robert Dober" <robert.do...@gmail.com> wrote:

> On 7/27/07, dbl...@rubypal.com <dbl...@rubypal.com> wrote:
> > On Fri, 27 Jul 2007, Brett Boge wrote:
> > > if (a = b or a = c or a = d or a = f)
> > You mean == rather than = , but in any case, try this:
> You are right for sure that OP meant ==, but let us answer the
> original question too ;)

> a = [b,c,d,e,f].compact.first

Your solution forces the evaluation of b/c/d/e/f, which the OP's does
not (thanks to the miracle of short-circuit boolean evaluation). I
think the 'correct' answer to the typo-incorrect question is:

  if a = (b or c or d or f)

To be super clear: this is only because we're talking about assignment
instead of an actual equality test.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Dober  
View profile  
 More options Jul 27 2007, 7:47 pm
From: "Robert Dober" <robert.do...@gmail.com>
Date: Sat, 28 Jul 2007 08:47:39 +0900
Local: Fri, Jul 27 2007 7:47 pm
Subject: Re: n00b if condition quest
On 7/27/07, Ian Whitlock <iw1j...@comcast.net> wrote:
> Robert Dober wrote:

> > You are right for sure that OP meant ==, but let us answer the
> > original question too ;)

> > a = [b,c,d,e,f].compact.first

> Not quite.

> b=c=d=false

indeed well,spotted, same trap than in
x ||= 42

> Ian
> --
> Posted via http://www.ruby-forum.com/.

--
[...] as simple as possible, but no simpler.
-- Attributed to Albert Einstein

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google