开发者

Does it matter if a conditional statement comes before or after the expression?

开发者 https://www.devze.com 2023-02-01 03:06 出处:网络
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:

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(开发者_C百科with params)

or is it the same thing written for better clarity?


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.


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


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消