ruby - Compound Boolean Expr: Void Value Expression -


    def success?         return @fhosts.empty? , @khosts.empty? , @shosts.any?     end 

when run instance method, error:

/home/fandingo/code/management/lib/ht.rb:37: void value expression             return @fhosts.empty? , @khosts.empty? , @shosts.any? 

i'm confused what's happening since works

    def success?         @fhosts.empty? , @khosts.empty? , @shosts.any?         # works         # r = @fhosts.empty? , @khosts.empty? , @shosts.any?         # return r     end 

i'm coming python background, , don't want implicit returns. programming has plenty of landmines is.

if have arbitrary expression, e, consists of boolean operations and , or together, here operations perform:

  • if e -- works
  • e -- works

* v = e -- works

  • return e -- broken

why doesn't last case work?

edit: v = e doesn't work.

v = ei

is evaluated. ei+1...k ignored.

this due weak binding of and causes parse out differently expect:

 return x , y 

this means:

 (return x) , y 

since you're returning doesn't have chance evaluate remainder of expression.

your version without return correct:

x , y 

this doesn't have binding issue , more idiomatic ruby. remember need put explicit return if you're trying force exit before last line of method. being opposed implicit returns going make code heavily non-ruby. they're 1 of reasons ruby clean , simple, , how things a.map { |v| v * 2 } works.

the when in rome principle applies here. if want write python-style ruby you're going going against grain. it's saying "i don't how x in spoken language, i'll ignore , way."

this should work:

return x && y 

the && method bound return last thing evaluated here.

or if want use and whatever reason:

return (x , y) 

Comments