Test multiple variables

Hi,

I need to test for a case where 5 variables are all empty.

Not quite sure how to do this.
Can I use multiple “and” statements?
eg
if e = “” and b = “” and c = “” ect…
then …

Appreciate the help.

Dave

On Fri, Dec 13, 2013 at 9:32 AM, Dave C. [email protected]
wrote:

I need to test for a case where 5 variables are all empty.

One possibility:

2.0.0 (main):0 > a = b = c = d = e = ‘’
=> “”
2.0.0 (main):0 > [ a,b,c,d,e ].all? { |x| x.empty? }
=> true
2.0.0 (main):0 > a = ‘a’
=> “a”
2.0.0 (main):0 > [ a,b,c,d,e ].all? { |x| x.empty? }
=> false
2.0.0 (main):0 >

HTH,

I need to test for a case where 5 variables are all empty.

2.0.0 (main):0 > [ a,b,c,d,e ].all? { |x| x.empty? }
=> true
2.0.0 (main):0 > a = ‘a’
=> “a”
2.0.0 (main):0 > [ a,b,c,d,e ].all? { |x| x.empty? }
=> false
2.0.0 (main):0 >

HTH,

Thanks!

On Fri, Dec 13, 2013 at 7:23 PM, Dave C. [email protected]
wrote:

I need to test for a case where 5 variables are all empty.

2.0.0 (main):0 > [ a,b,c,d,e ].all? { |x| x.empty? }
=> true
2.0.0 (main):0 > a = ‘a’
=> “a”
2.0.0 (main):0 > [ a,b,c,d,e ].all? { |x| x.empty? }
=> false
2.0.0 (main):0 >

Alternative with identical semantics but a tad less typing:

irb(main):002:0> [a, b, c, d, e].all? &:empty?
=> true

Kind regards

robert

Appreciate the help.

Dave

The general idea is correct, but

  • you did an assignment not an equality check: use ‘==’
  • don’t use ‘and’ in a logic statement, use ‘&&’, they are not the same
    thing

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs