I guess it had to happen sometime...
but apparently, I can't do
"return not condition"
i have to do
"return ! condition"
instead...
*grumbles*
I guess it had to happen sometime...
but apparently, I can't do
"return not condition"
i have to do
"return ! condition"
instead...
*grumbles*
return unless condition
marcel
Marcel Molina Jr. wrote:
Umm... doesnt that just restrict "return"ing to the condition being false? ie; wouldn't I have to
return true unless condition return false
if not... well... i've learned something new today
- Tyler
I'm afraid I can't.
def invert(huh) return unless huh end
if invert false print "it worked!\n" else print "it didn't work\n" end
yields "it didn't work".
it's a small failing. but it still irks me. it's okay though ruby, I can forgive you this one time.
- Tyler
It’s all about bind order:
return (not false) == return ! false
Yes, those two are equivalent, because they both involve extraneous symbols instead of pure expressive english!
Like I said, it's a small thing. And the first thing that's really irked me, even after I've learned it.
- Tyler
I didn't get what you wanted to do from your oringal email.
return unless condition
will indeed return nil if the condition is true and therefore the result of the method invocation will be boolean false, which is not what you had wanted.
Like another poster mentioned, not does not bind as tightly as !, so you'd have to use parens if you wanted to use not.
I find 'return not condition' rather unnatural to read though. If this were the last line of a method I'd just get rid of the return entirely and simply do
!condition
marcel
Tyler MacDonald wrote:
That was a unit test...
I'm sorry if this is wasting your guys' time.. I was just venting, *really*. I'm just using "!" instead of "not" now... it just seemed kind of disappointing that "not" didn't work.
- Tyler
Tyler -- if this is at the end of the function, you don't need the return!
def test_func(foo) not foo end
has the same effect you are going for -- (after all ruby has functional language underpinnings)
-andy:)
Although, just to add (now that I've had a few sips of coffee) I remember being disappointed when I put "return not foo" the first time and it didn't know what I meant. I mean.. return really should be at the bottom of the barrel as far as order of operations is concerned. Last to be evaluated.
-andy:)
Ruby has lots of friends! .... look how quickly everyone came running
to defend
I personally try not to use a "return" unless my method is returning prematurely. and having the last line of a method read
not condition
seems clearer than
return not condition
... I also quite like !condition anyway
can this thread win some kind of OpinionBasedThread of the day
award
farms.
What is ‘condition’? It could be complicated and I think it’s affecting your outcome. ‘not’ is very low in precedence. It will be polite and let everything in ‘condition’ evaluate first. ‘!’ evaluates before most everything else. Did you try doing…
return not (condition)
or
return (not condition)
Jose
…
Jose Hales-Garcia
UCLA Department of Statistics
jose@stat.ucla.edu
class Object def not(value) !value end end
this should help you out!