Forum: Ruby Confusion with Ruby printing mechanics.

Posted by Love U Ruby (my-ruby)
on 2013-02-16 11:36
Here I just played to see how printing statement behaves with "nil"
values. Here we go:

>> p nil
nil
=> nil #good as expected.
>> puts nil
       #as nil.to_s causes the "blank" in the first line.
=> nil #good as expected.
>> p puts nil
    #as nil.to_s causes the "blank" in the first line
nil
=> nil # good as expected, as p works here on the return value of puts.

=========================

Confusion begins with the below :

>> p(puts(print("hi")))
hi
nil
=> nil # this is the actual output.

But from the above analysis I expected the below:

>> p(puts(print("hi")))
hi

nil
=> nil

Could you explain the gap between my assumption and the actual one?

Thanks.
Posted by Matthew Kerwin (mattyk)
on 2013-02-16 11:50
(Received via mailing list)
Print doesn't append a newline, so the puts'd nil is tacked onto the end 
of
the 'hi'

I'd write more, but I hate typing on my phone.

Sent from my phone, so excuse the typos.
Posted by Love U Ruby (my-ruby)
on 2013-02-16 11:54
Matthew Kerwin wrote in post #1097228:
> Print doesn't append a newline, so the puts'd nil is tacked onto the end
> of
> the 'hi'
>
> I'd write more, but I hate typing on my phone.
>
> Sent from my phone, so excuse the typos.


Humm! perfect catch tried and tested. and you got 100 out of 100. :) :)

>> p(puts(print("hi\n")))
hi

nil
=> nil
Posted by Damián M. González (igorjorobus)
on 2013-02-16 15:26
> Confusion begins with the below :
>
>>> p(puts(print("hi")))
> hi
> nil
> => nil # this is the actual output.
>
> But from the above analysis I expected the below:
>
>>> p(puts(print("hi")))
> hi
>
> nil
> => nil

 Well there you are adding only one newline, it's added with puts. You 
can find what you want also in this way:

 p print("hi\n\n")
Posted by Love U Ruby (my-ruby)
on 2013-02-16 15:40
Damián M. González wrote in post #1097245:
>> Confusion begins with the below :
>>
>  Well there you are adding only one newline, it's added with puts. You
> can find what you want also in this way:
>
>  p print("hi\n\n")

I actually had confusion with the below:

>> p(puts(print("hi")))
hi
nil
=> nil # this is the actual output.

how it comes? to catch this i added one '\n'.
Posted by Hans Mackowiak (hanmac)
on 2013-02-16 17:20
why do you have confusion?? didnt they already told you how the print 
commands work?

in your sample:
print("hi") does "hi" and returns nil
puts(nil) does "\n" and returns nil
p(nil) does "nil\n" and returns nil (the object it gets)

was it that hard to get??
Posted by Love U Ruby (my-ruby)
on 2013-02-16 17:25
Hans Mackowiak wrote in post #1097261:
> why do you have confusion?? didnt they already told you how the print
> commands work?
>
> in your sample:
> print("hi") does "hi" and returns nil
> puts(nil) does "\n" and returns nil
> p(nil) does "nil\n" and returns nil (the object it gets)
>
> was it that hard to get??

Nopes! friend... I am set at all. I just told him,what made me confused.

Thanks
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.