如何在ruby交互式shell中有多行代码?

时间:2022-05-18 15:05:17

This might be a silly question. But, I am a newb... How can you have a multi-line code in the interactive ruby shell? It seems like you can only have one long line. Pressing enter runs the code. Is there anyway I can skip down to the next line without running the code? Again, sorry if this is a dumb question. Thanks.

这可能是个愚蠢的问题。但是,我是个新人……如何在交互式ruby shell中拥有多行代码?看起来你只能排一个长队。按enter运行代码。我是否可以跳过下一行而不运行代码?再次,抱歉,如果这是一个愚蠢的问题。谢谢。

2 个解决方案

#1


3  

This is an example:

这是一个例子:

2.1.2 :053 > a = 1
=> 1
2.1.2 :054 > b = 2
=> 2
2.1.2 :055 > a + b
 => 3
2.1.2 :056 > if a > b  #The code ‘if ..." starts the definition of the conditional statement. 
2.1.2 :057?>   puts "false"
2.1.2 :058?>   else
2.1.2 :059 >     puts "true"
2.1.2 :060?>   end     #The "end" tells Ruby we’re done the conditional statement.
"true"     # output
=> nil     # returned value

IRB can tell us the result of the last expression it evaluated.

IRB可以告诉我们它求值的最后一个表达式的结果。

You can get more useful information from here(https://www.ruby-lang.org/en/documentation/quickstart/).

您可以从这里获得更多有用的信息(https://www.ruby-lang.org/en/documentation/quickstart/)。

#2


1  

If you are talking about entering a multi-line function, IRB won't register it until you enter the final end statement.

如果您正在讨论输入一个多行函数,IRB在您输入最终的end语句之前不会注册它。

If you are talking about a lot of individual expressions such as

如果你谈论的是很多单独的表达,比如

x = 1
y = 2
z = x + y

It's OK if IRB executes each as you enter it. The end result will be the same (for time-insensitive code of course). If you still want a sequence of expressions executed as fast as possible, you can simply define them inside of a function, and then run that function at the end.

当你输入IRB时,每个人都可以执行。最终结果将是相同的(当然,对于时间不敏感的代码)。如果您仍然希望以尽可能快的速度执行表达式序列,您可以简单地在函数中定义它们,然后在末尾运行该函数。

def f()
    x = 1
    y = 2
    z = x + y
end

f()

#1


3  

This is an example:

这是一个例子:

2.1.2 :053 > a = 1
=> 1
2.1.2 :054 > b = 2
=> 2
2.1.2 :055 > a + b
 => 3
2.1.2 :056 > if a > b  #The code ‘if ..." starts the definition of the conditional statement. 
2.1.2 :057?>   puts "false"
2.1.2 :058?>   else
2.1.2 :059 >     puts "true"
2.1.2 :060?>   end     #The "end" tells Ruby we’re done the conditional statement.
"true"     # output
=> nil     # returned value

IRB can tell us the result of the last expression it evaluated.

IRB可以告诉我们它求值的最后一个表达式的结果。

You can get more useful information from here(https://www.ruby-lang.org/en/documentation/quickstart/).

您可以从这里获得更多有用的信息(https://www.ruby-lang.org/en/documentation/quickstart/)。

#2


1  

If you are talking about entering a multi-line function, IRB won't register it until you enter the final end statement.

如果您正在讨论输入一个多行函数,IRB在您输入最终的end语句之前不会注册它。

If you are talking about a lot of individual expressions such as

如果你谈论的是很多单独的表达,比如

x = 1
y = 2
z = x + y

It's OK if IRB executes each as you enter it. The end result will be the same (for time-insensitive code of course). If you still want a sequence of expressions executed as fast as possible, you can simply define them inside of a function, and then run that function at the end.

当你输入IRB时,每个人都可以执行。最终结果将是相同的(当然,对于时间不敏感的代码)。如果您仍然希望以尽可能快的速度执行表达式序列,您可以简单地在函数中定义它们,然后在末尾运行该函数。

def f()
    x = 1
    y = 2
    z = x + y
end

f()