Ruby: what is correct way to print to the console (puts or print_info)?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I tried both approaches and both print to the console.
puts "Hello World"
print_info "Hello World"
What is the real difference between them and which approach is better than another?
ruby printing console
add a comment |
I tried both approaches and both print to the console.
puts "Hello World"
print_info "Hello World"
What is the real difference between them and which approach is better than another?
ruby printing console
add a comment |
I tried both approaches and both print to the console.
puts "Hello World"
print_info "Hello World"
What is the real difference between them and which approach is better than another?
ruby printing console
I tried both approaches and both print to the console.
puts "Hello World"
print_info "Hello World"
What is the real difference between them and which approach is better than another?
ruby printing console
ruby printing console
asked Jan 3 at 20:24
TiredOfProgrammingTiredOfProgramming
360316
360316
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
To start print_info is not standard ruby. Could be a rails thing? I dunno.
Now two actual choices puts vs p
puts outputs data using to_s to convert to strings. It has a special case for arrays, putting each array element on its own line. It returns nil
p outputs data using inspect to convert to strings. It has no special case for arrays. It returns its arguments singly if there is only one or in an array if there is more than one.
Which is better? It depends if you want to_s or inspect. I find that p is useful in debugging situations as it can "peek" at values without needing special debug code.
For example the code:
result = my_object.my_method(arg1, arg2)
can be instrumented as:
result = p my_object.my_method(p(arg1), p(arg2))
All things said though puts is more popular, so that is what I normally use.
Also don't forget pp which is pretty print, that prints out data in a more meaningful way. Before Ruby 2.6 you'll need to remember to:
require 'pp'
1
It's definitely not a Rails thing either. Not sure where OP got that command.
– anothermh
Jan 4 at 1:50
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54029299%2fruby-what-is-correct-way-to-print-to-the-console-puts-or-print-info%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
To start print_info is not standard ruby. Could be a rails thing? I dunno.
Now two actual choices puts vs p
puts outputs data using to_s to convert to strings. It has a special case for arrays, putting each array element on its own line. It returns nil
p outputs data using inspect to convert to strings. It has no special case for arrays. It returns its arguments singly if there is only one or in an array if there is more than one.
Which is better? It depends if you want to_s or inspect. I find that p is useful in debugging situations as it can "peek" at values without needing special debug code.
For example the code:
result = my_object.my_method(arg1, arg2)
can be instrumented as:
result = p my_object.my_method(p(arg1), p(arg2))
All things said though puts is more popular, so that is what I normally use.
Also don't forget pp which is pretty print, that prints out data in a more meaningful way. Before Ruby 2.6 you'll need to remember to:
require 'pp'
1
It's definitely not a Rails thing either. Not sure where OP got that command.
– anothermh
Jan 4 at 1:50
add a comment |
To start print_info is not standard ruby. Could be a rails thing? I dunno.
Now two actual choices puts vs p
puts outputs data using to_s to convert to strings. It has a special case for arrays, putting each array element on its own line. It returns nil
p outputs data using inspect to convert to strings. It has no special case for arrays. It returns its arguments singly if there is only one or in an array if there is more than one.
Which is better? It depends if you want to_s or inspect. I find that p is useful in debugging situations as it can "peek" at values without needing special debug code.
For example the code:
result = my_object.my_method(arg1, arg2)
can be instrumented as:
result = p my_object.my_method(p(arg1), p(arg2))
All things said though puts is more popular, so that is what I normally use.
Also don't forget pp which is pretty print, that prints out data in a more meaningful way. Before Ruby 2.6 you'll need to remember to:
require 'pp'
1
It's definitely not a Rails thing either. Not sure where OP got that command.
– anothermh
Jan 4 at 1:50
add a comment |
To start print_info is not standard ruby. Could be a rails thing? I dunno.
Now two actual choices puts vs p
puts outputs data using to_s to convert to strings. It has a special case for arrays, putting each array element on its own line. It returns nil
p outputs data using inspect to convert to strings. It has no special case for arrays. It returns its arguments singly if there is only one or in an array if there is more than one.
Which is better? It depends if you want to_s or inspect. I find that p is useful in debugging situations as it can "peek" at values without needing special debug code.
For example the code:
result = my_object.my_method(arg1, arg2)
can be instrumented as:
result = p my_object.my_method(p(arg1), p(arg2))
All things said though puts is more popular, so that is what I normally use.
Also don't forget pp which is pretty print, that prints out data in a more meaningful way. Before Ruby 2.6 you'll need to remember to:
require 'pp'
To start print_info is not standard ruby. Could be a rails thing? I dunno.
Now two actual choices puts vs p
puts outputs data using to_s to convert to strings. It has a special case for arrays, putting each array element on its own line. It returns nil
p outputs data using inspect to convert to strings. It has no special case for arrays. It returns its arguments singly if there is only one or in an array if there is more than one.
Which is better? It depends if you want to_s or inspect. I find that p is useful in debugging situations as it can "peek" at values without needing special debug code.
For example the code:
result = my_object.my_method(arg1, arg2)
can be instrumented as:
result = p my_object.my_method(p(arg1), p(arg2))
All things said though puts is more popular, so that is what I normally use.
Also don't forget pp which is pretty print, that prints out data in a more meaningful way. Before Ruby 2.6 you'll need to remember to:
require 'pp'
answered Jan 3 at 21:02
Peter CamilleriPeter Camilleri
1,5221217
1,5221217
1
It's definitely not a Rails thing either. Not sure where OP got that command.
– anothermh
Jan 4 at 1:50
add a comment |
1
It's definitely not a Rails thing either. Not sure where OP got that command.
– anothermh
Jan 4 at 1:50
1
1
It's definitely not a Rails thing either. Not sure where OP got that command.
– anothermh
Jan 4 at 1:50
It's definitely not a Rails thing either. Not sure where OP got that command.
– anothermh
Jan 4 at 1:50
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54029299%2fruby-what-is-correct-way-to-print-to-the-console-puts-or-print-info%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown