10

Sorry if this is a stupid question but I'm a C# guy fumbling his way around ruby..

in ruby i notice a lot of people do this:

do_something(with params) if 1 = 1

is there any difference (even slight) between that and this:

if 1 = 1 do_something(with params)

or is it the same thing written for better clarity?

1
  • It's mostly stylistic. When dealing with a single line of code I use the trailing conditional as it seems to make more sense conversationally. Since a large component of writing software is making it maintainable, I try to write it in a way that allows it to slide into the brain easily. Some books recommend using a block or wrapping conditional if (blah) ... some code ... end only, arguing that it's better for future expansion of the code. If we need to add something the closing end is already there. I find it plenty easy with my vim-powers to work around that so I use single-line mostly. Dec 21, 2010 at 16:28

5 Answers 5

7

The latter is syntactically invalid. You would need to write:

if 1==1 then do_something(with params) end

Single-line conditionals must always trail. And yes, there is a difference. Try these out:

bar1 = if foo1=14
  foo1*3
end
#=> 42

bar2 = foo2*3 if foo2=14
#=> NameError: undefined local variable or method `foo2' for main:Object

In the latter, Ruby sees the assignment after the reference and so treats foo2 as a method instead of a local variable. This is only an issue when:

  • You are intentionally using assignment (not testing for equality) in a conditional, and
  • This is the first time (in terms of source order) that this variable has been assigned in the scope.
5
  • warning: found = in conditional, should be == Dec 21, 2010 at 15:07
  • @JonasElfström Yes, there is also that. But the issue remains. It is (I find personally and in others code) common in Ruby to use assignment in conditionals. if user=User.find(42) then ... end. Sadly, the heuristics for local variables don't work when the assignment is in a single-line trailing conditional.
    – Phrogz
    Dec 21, 2010 at 15:09
  • @Phrogz, I'm trying to understand why the assignment in the trailing conditional fails. Where can I read more about it? Nov 8, 2013 at 20:31
  • 1
    @Leito The problem is that Ruby's interpreter uses a heuristic to decide if an unqualified, unadorned name is a local variable or a method call, since for foo = bar the bar could be a local variable or a method call with no arguments. As I understand it, the heuristic is based on how the name is used the first time it is seen lexically in the source code. In the problematic case, it decides that foo2 is a method call instead of a local variable, and it is assumed to be such from that point forward.
    – Phrogz
    Nov 8, 2013 at 20:37
  • @Leito For example, this horrible hack makes it work again: bar = (foo||=0) && (foo*3) if foo=14 because now the first time it sees foo it knows that it must be a local variable and not a method call. Alternatively, just putting foo=nil on the line before also fixes it.
    – Phrogz
    Nov 8, 2013 at 20:41
4

It's syntactic sugar... allowing us to write code in a way that's easier to read.

http://rubylearning.com/satishtalim/ruby_syntactic_sugar.html

Note: for @Phrogz, the following are NOT the same! Please make sure that you are not trying to assign a value to variable instead of comparing a variable to a value! Also, as Phrogz mentions, the order of variable assignment makes a big difference... see @Phrogz answer for mor details!

if 1 = 1 then do_something(with params) end
if 1 == 1 then do_something(with params) end
0
4

Fire up irb and run your code and you will learn:

  • 1=1 is a syntax error, change to 1==1.
  • You can't have an expression directly after if 1==1, you will have to add a : or then and close with an end.

The trailing if should really only be used for single expressions and you can't add an else. They are called statement modifiers and they are just syntactic sugar for better readability. I'm not totally against them but recommend using them sparingly.

2

You can use either but if you put the if statement first then you will need to close the condition with an 'end'.

if 1==1 
   do_something(with params)
end
2

The following leaves foo as nil.

foo = true unless defined?(foo) #Leaves foo as nil

This is because Ruby creates a variable foo and assigns it to nil when it reads (parsing, I think) the foo = true bit, and then when it reads (executing, I think) the unless defined?(foo), it says that foo is defined (it's defined as nil), and therefore doesn't execute the foo = true part.

If you did

unless defined?(foo)
  foo = true
end

then you'd get foo assinged to true.

I added this to What are the Ruby Gotchas a newbie should be warned about?, because someone got confused about it here.

So yes, in some circumstances it can matter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.