本文介绍了如果条件语句出现在表达式之前或之后,这是否重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果这是一个愚蠢的问题,但我是一个在红宝石中摸索的C#家伙..

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

在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?

推荐答案

这是语法糖...允许我们以更容易阅读的方式编写代码。

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

注意:对于@Phrogz,以下内容不一样!
请确保您没有尝试为变量赋值而不是将变量与值进行比较!另外,正如Phrogz所提到的,变量赋值的顺序有很大的不同......请参阅@Phrogz回答mor的详细信息!

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

这篇关于如果条件语句出现在表达式之前或之后,这是否重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 19:52