Unless if not == unless post_on_ruby_forum

So everything I have thus far read and heard about the Ruby unless
keyword lead me to believe that it is just another word for if not. This
is not true.

unless 1 + 1 == 2
   puts "a"
elsif 1 + 1 == 5
   puts "b"
else
   puts "c"
end

this code fails and complains that it expects an end to be where elsif
is
however

if not 1 + 1 == 2
   puts "a"
elsif 1 + 1 == 5
   puts "b"
else
   puts "c"
end

works as expected. This took me quite a while to figure out as everyone
had always lead me to believe that unless is just the cool kids way of
saying if not in ruby.

So I guess my question is, are there any other unexpected behaviors of
unless that I should know about? I think I might just start using if not
and ignoring unless, I actually end up having to translate it to if not
in my head anyway to read things clearly, and it doesn't work with
elsif. Why does everyone rave about how unless is exactly like if not
but with clearer language, if this is not the case ?

···

--
Posted via http://www.ruby-forum.com/.

It's most useful for perl-esque one-liners:

  die unless it_worked

···

On 20 July 2012 14:05, roob noob <lists@ruby-forum.com> wrote:

So everything I have thus far read and heard about the Ruby unless
keyword lead me to believe that it is just another word for if not. This
is not true.

unless 1 + 1 == 2
   puts "a"
elsif 1 + 1 == 5
   puts "b"
else
   puts "c"
end

this code fails and complains that it expects an end to be where elsif
is
however

if not 1 + 1 == 2
   puts "a"
elsif 1 + 1 == 5
   puts "b"
else
   puts "c"
end

works as expected. This took me quite a while to figure out as everyone
had always lead me to believe that unless is just the cool kids way of
saying if not in ruby.

So I guess my question is, are there any other unexpected behaviors of
unless that I should know about? I think I might just start using if not
and ignoring unless, I actually end up having to translate it to if not
in my head anyway to read things clearly, and it doesn't work with
elsif. Why does everyone rave about how unless is exactly like if not
but with clearer language, if this is not the case ?

--
Posted via http://www.ruby-forum.com/\.

--
  Matthew Kerwin, B.Sc (CompSci) (Hons)
  http://matthew.kerwin.net.au/
  ABN: 59-013-727-651

  "You'll never find a programming language that frees
  you from the burden of clarifying your ideas." - xkcd

'elsif' has to follow an 'if' NOT an 'unless'. It doesn't make sense to follow unless with an if statement. What you want is 'elsunless', but that doesn't exist in Ruby so chained unless statements aren't possible. Some people even frown upon 'unless/else' which is legal Ruby.

Henry

···

On 20/07/2012, at 4:05 PM, roob noob wrote:

unless 1 + 1 == 2
  puts "a"
elsif 1 + 1 == 5
  puts "b"
else
  puts "c"
end

So everything I have thus far read and heard about the Ruby unless
keyword lead me to believe that it is just another word for if not. This
is not true.

  unless 1 + 1 == 2
    puts "a"
  elsif 1 + 1 == 5
    puts "b"
  else
    puts "c"
  end

[...]

elsif. Why does everyone rave about how unless is exactly like if not
but with clearer language, if this is not the case ?

Your usage of unless is extremely unclear.
Most people would advise you to change

   unless x == 2
     a
   else
     b
   end

to

   if x == 2
     b
   else
     a
   end

Furthermore, I do not understand what you want to achieve:
your "b" case seems useless to me.
Assuming 1 + 1 really would be 5, it could not be equal to 2
at the same time and you would get the "a" case, not "b".

···

Am 20.07.2012 06:05, schrieb roob noob:

--
<https://github.com/stomar/&gt;

Hi,

I think this is a technical misunderstanding. It's true that "unless"
usually is the same as "if not", but that does not mean you can
literally replace every "if not" with "unless". It's not a macro. The
Ruby parser treats "unless" as a keyword on it's own with special rules
where it can occur and how those structures are evaluated.

So when you talk about "unexpected behaviour", this is rather a problem
of you having the wrong expectations. Saying that one thing means the
same as another does usually *not* mean they're actually interchangeable
on syntax level.

···

--
Posted via http://www.ruby-forum.com/.

Personally I find the following much more readable:

   do_something or
     raise SomethingFailedException

···

On 07/20/2012 06:17 AM, Matthew Kerwin wrote:

[unless is] most useful for perl-esque one-liners:

   die unless it_worked

--
Lars Haugseth

Me too, usually. Sometimes it's the case that, for example, you've
iterated through a large collection, performing actions on members and
whatever, and then at the end you only want to proceed if certain
conditions were met during iteration. Then, depending on how you
track the conditions, `bail unless more_needs_to_be_done` can be
appropriate. It always depends on the situation, and where you want
to trade off readability and maintainability (and, sometimes,
efficiency.)

···

On 20 July 2012 19:10, Lars Haugseth <ruby-talk@larshaugseth.com> wrote:

On 07/20/2012 06:17 AM, Matthew Kerwin wrote:

[unless is] most useful for perl-esque one-liners:

   die unless it_worked

Personally I find the following much more readable:

  do_something or
    raise SomethingFailedException

--
  Matthew Kerwin, B.Sc (CompSci) (Hons)
  http://matthew.kerwin.net.au/
  ABN: 59-013-727-651

  "You'll never find a programming language that frees
  you from the burden of clarifying your ideas." - xkcd