与Ruby的混淆';IRB中的s输出机制;p"&引用;“打印”;及;把「;
从输出中可以很清楚地看到,两个与Ruby的混淆';IRB中的s输出机制;p"&引用;“打印”;及;把「;,ruby,Ruby,从输出中可以很清楚地看到,两个p打印了“hi”和“hello”。 然后将打印在下面 >> p "hi"; puts p "hello" "hi" "hello" hello => nil 但问题是:asp返回 hello => nil 什么放置从p收到左边的还是右边的?现在,为了更深入地了解这一点,我尝试了以下方法: "hello" => "hello" 综上所述,我所理解的是p打印了它的一个。与下面的输出部分混淆 >> p "hi"; put
p
打印了“hi”
和“hello”
。
然后将
打印在下面
>> p "hi"; puts p "hello"
"hi"
"hello"
hello
=> nil
但问题是:asp
返回
hello
=> nil
什么放置
从p
收到左边的还是右边的?现在,为了更深入地了解这一点,我尝试了以下方法:
"hello"
=> "hello"
综上所述,我所理解的是p
打印了它的一个。与下面的输出部分混淆
>> p "hi"; puts print "hello"
"hi"
hello
=> nil
hello
是否由print
打印,然后=>nil
的左侧在哪里?如果我认为<>代码> < /C> >已被烧为<代码>打印< /代码>返回值<代码> nIL/COD> >输出应为
hello
=>nil
hello
=>nil # the extra blank line is for `nil.to_s` .
但从实际产出来看,我不能得出这样的结论。如果我认为<>代码> < /C> >已被“<代码> >打印<代码> s打印值<代码> hello <代码>,则输出应为
hello
=>nil
hello
=>nil # the extra blank line is for `nil.to_s` .
但从实际产出来看,我不能得出这样的结论
我正在使用Ubuntu 12.10和Ruby 1.9.3。有谁能帮我弄明白发生了什么事吗?你的例子和我的一样
hello
=>nil # then where the output of print statement went out?
输出中不会出现空行的原因是,print
不会在其输出中追加换行符
print
将hello
写入stdout,返回nil,然后将writes(在同一行上)nil.to_
(即空字符串)后跟一个换行符,因此如果您搜索“ruby p puts print”,您将获得与编写put(“hello”)
完全相同的输出您将得到几个StackOverflow结果,这些结果很好地解释了这些方法的作用。仅供参考,p puts print foo
与p(put(print(foo))
@AmitKumarGupta Humm。。。Amit jip puts print foo和p(puts(print(foo))
这是一个很好的信息。这似乎是@theTinMan的重复或延续。我很抱歉地告诉你,这与这个问题没有关系。这个问题有一个链式结构,分别是p
、print
和puts
puts p“hello”
是一种荒谬的打印方式,你在浪费时间寻找答案,因为没有人会使用它。使用放上“hello”“
除非您绝对需要返回值,p
提供返回值。你在学习一门语言的第一件事上陷入了困境。前面还有很多更难学的东西,没有区别。这对我来说很好