条件语句出现在表达式之前还是之后有关系吗? [英] Does it matter if a conditional statement comes before or after the expression?

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

问题描述

对不起,如果这是一个愚蠢的问题,但我是一个 C# 家伙,在 ruby​​ 上摸索着.

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.

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

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

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆