Why a double ampersand Ruby expression and using the return statement cause a syntax error

def foo
  true && return false
end

def bar
  true and return false
end

fooThe method causes a syntax error. barno. Why?

Assuming I want to evaluate single-line expressions with a return statement (similar to some commonly used shell expression / bash), would this be the recommended way to do this, or is there a more recommended approach?

+4
source share
4 answers

Side note

It is noteworthy that and, and &&are not equivalent.

and , && . ?

, and, .

value = nil and value / 42

,

value = nil && value / 42

1

, return ( > / bash), , ?

Ruby, :

value if conditional

, nil . return, !

, unless. ...

return unless arguments_are_valid

2

foo . . ?

. . , , .

(true && return) false
(true) and (return false)
+4

,

true && return false

(true && return) false

true && return

false . , - .

+6

- &&

true && return false

(true && return) false

. ,

def foo
  true && (return false)
end

and , and , &&.

+3

,

def foo
  return false if true
end

def bar
  return false if true
end

return :),

when true and return is ok send a false
0

All Articles