Ruby just pissed me off!

I guess it had to happen sometime…

but apparently, I can’t do

“return not condition”

i have to do

“return ! condition”

instead…

grumbles

On Thu, Feb 22, 2007 at 06:37:31PM -0800, Tyler MacDonald wrote:

instead…

grumbles

return unless condition

marcel

Marcel Molina Jr. [email protected]

Marcel Molina Jr. [email protected] wrote:

but apparently, I can’t do
“return not condition”
i have to do
“return ! condition”
instead…
return unless condition

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 :slight_smile:

  • Tyler

Marcel Molina Jr. wrote:

“return ! condition”

instead…

grumbles

return unless condition

marcel

Ok, Tyler, tell Ruby you’re sorry. :wink:

Jamey

It’s all about bind order:

return (not false) == return ! false

Jamey C. [email protected] wrote:

return unless condition
Ok, Tyler, tell Ruby you’re sorry. :wink:

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”. :frowning:

it’s a small failing. but it still irks me. it’s okay though ruby, I can
forgive you this one time.

  • Tyler

Tyler MacDonald wrote:

return unless huh
end

if invert false
print “it worked!\n”
else
print “it didn’t work\n”
end

yields “it didn’t work”. :frowning:

How about this:

def invert(huh)
return huh ? false : true
end

Jamey

Luke I. [email protected] wrote:

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.

:slight_smile:

  • Tyler

Jamey C. [email protected] wrote:

How about this:

def invert(huh)
return huh ? false : true
end

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

On Thu, Feb 22, 2007 at 07:01:11PM -0800, Tyler MacDonald wrote:

if invert false
print “it worked!\n”
else
print “it didn’t work\n”
end

yields “it didn’t work”. :frowning:

it’s a small failing. but it still irks me. it’s okay though ruby, I can
forgive you this one time.

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

Marcel Molina Jr. [email protected]

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:)

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:)

On Feb 22, 10:32 pm, Tyler MacDonald [email protected]

On Feb 22, 2007, at 6:37 PM, Tyler MacDonald wrote:

instead…

grumbles

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
[email protected]

Ruby has lots of friends! … look how quickly everyone came running
to defend :slight_smile:

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 :slight_smile:

farms.

On Feb 23, 12:58 pm, “[email protected][email protected]

class Object
def not(value)
!value
end
end

this should help you out!

2007/2/23, Jose Hales-Garcia [email protected]: