gpt4 book ai didi

ruby - Ruby 中的语句 block

转载 作者:太空宇宙 更新时间:2023-11-03 17:04:24 24 4
gpt4 key购买 nike

我的背景是使用 {} 的语言说这些是“语句 block ”,但我正在学习 ruby​​ 并且真的很困惑它是如何在那里完成的。

所以让我用 C 说

if ( condition )
{
statement1;
statement2;
}
else if (condition)
{
statement1;
statement2;

// nested if
if (condition)
{
dosomethinghere;
}
}
else
{
statement1;
statement2;
}

如何将此代码放入 ruby​​ 中?并提及如何理解 ruby​​ 中“{}”的同义词,谢谢。

最佳答案

好吧,回答你的第一个问题:

if ( condition )
statement1
statement2
elsif (condition)
statement1
statement2
// nested if
if (condition)
dosomethinghere
end
else
statement1
statement2
end

if 语句的语法规则是:

 if expr [then]
expr...
[elsif expr [then]
expr...]...
[else
expr...]
end

[] 之间的所有内容都是可选的

然而,在其他方面,您也可以创建和传递代码块,检查this发布以阅读有关此主题的更多信息。

关于ruby - Ruby 中的语句 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1995781/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com