Inputting string using (gets.chomp) and producing a histogram
I'm having struggles understanding ruby. I wish to have a program in which a user can input a set of text and it come back with asterisks. So far I was able to do it via a .txt file. Can anyone explain where I went wrong? I am struggling with ruby a lot.
Image of outcome when I run it
print "Please enter any length of text:"
user_input = String(gets.chomp)
h = Hash.new
f = user_input
f.each_line { |line|
letters = line.split(//)
letters.each { |w|
if h.has_key?(w)
h[w] = h[w] + 1
else
h[w] = 1
end
}
}
# sort the hash by
h.sort{|a,b| a[1]<=>b[1]}.each { |elem|
puts ""#{elem[0]}": " + '*' * elem[1]
}
Error message I encountered
Undefined method `chomp' for nil:NilClass (NoMethodError)
ruby
add a comment |
I'm having struggles understanding ruby. I wish to have a program in which a user can input a set of text and it come back with asterisks. So far I was able to do it via a .txt file. Can anyone explain where I went wrong? I am struggling with ruby a lot.
Image of outcome when I run it
print "Please enter any length of text:"
user_input = String(gets.chomp)
h = Hash.new
f = user_input
f.each_line { |line|
letters = line.split(//)
letters.each { |w|
if h.has_key?(w)
h[w] = h[w] + 1
else
h[w] = 1
end
}
}
# sort the hash by
h.sort{|a,b| a[1]<=>b[1]}.each { |elem|
puts ""#{elem[0]}": " + '*' * elem[1]
}
Error message I encountered
Undefined method `chomp' for nil:NilClass (NoMethodError)
ruby
I recommend you to read this. ruby-doc.org/docs/ruby-doc-bundle/Tutorial/part_02/…
– Kick Buttowski
Dec 31 '18 at 19:03
What's wrong with your current code?
– Supa Mega Ducky Momo da Waffle
Dec 31 '18 at 19:03
I recieve the error <br>ndefined methodchomp' for nil:NilClass (NoMethodError)`
– Jack Boylan
Dec 31 '18 at 19:03
add a comment |
I'm having struggles understanding ruby. I wish to have a program in which a user can input a set of text and it come back with asterisks. So far I was able to do it via a .txt file. Can anyone explain where I went wrong? I am struggling with ruby a lot.
Image of outcome when I run it
print "Please enter any length of text:"
user_input = String(gets.chomp)
h = Hash.new
f = user_input
f.each_line { |line|
letters = line.split(//)
letters.each { |w|
if h.has_key?(w)
h[w] = h[w] + 1
else
h[w] = 1
end
}
}
# sort the hash by
h.sort{|a,b| a[1]<=>b[1]}.each { |elem|
puts ""#{elem[0]}": " + '*' * elem[1]
}
Error message I encountered
Undefined method `chomp' for nil:NilClass (NoMethodError)
ruby
I'm having struggles understanding ruby. I wish to have a program in which a user can input a set of text and it come back with asterisks. So far I was able to do it via a .txt file. Can anyone explain where I went wrong? I am struggling with ruby a lot.
Image of outcome when I run it
print "Please enter any length of text:"
user_input = String(gets.chomp)
h = Hash.new
f = user_input
f.each_line { |line|
letters = line.split(//)
letters.each { |w|
if h.has_key?(w)
h[w] = h[w] + 1
else
h[w] = 1
end
}
}
# sort the hash by
h.sort{|a,b| a[1]<=>b[1]}.each { |elem|
puts ""#{elem[0]}": " + '*' * elem[1]
}
Error message I encountered
Undefined method `chomp' for nil:NilClass (NoMethodError)
ruby
ruby
edited Dec 31 '18 at 19:12
Jack Boylan
asked Dec 31 '18 at 18:40
Jack BoylanJack Boylan
62
62
I recommend you to read this. ruby-doc.org/docs/ruby-doc-bundle/Tutorial/part_02/…
– Kick Buttowski
Dec 31 '18 at 19:03
What's wrong with your current code?
– Supa Mega Ducky Momo da Waffle
Dec 31 '18 at 19:03
I recieve the error <br>ndefined methodchomp' for nil:NilClass (NoMethodError)`
– Jack Boylan
Dec 31 '18 at 19:03
add a comment |
I recommend you to read this. ruby-doc.org/docs/ruby-doc-bundle/Tutorial/part_02/…
– Kick Buttowski
Dec 31 '18 at 19:03
What's wrong with your current code?
– Supa Mega Ducky Momo da Waffle
Dec 31 '18 at 19:03
I recieve the error <br>ndefined methodchomp' for nil:NilClass (NoMethodError)`
– Jack Boylan
Dec 31 '18 at 19:03
I recommend you to read this. ruby-doc.org/docs/ruby-doc-bundle/Tutorial/part_02/…
– Kick Buttowski
Dec 31 '18 at 19:03
I recommend you to read this. ruby-doc.org/docs/ruby-doc-bundle/Tutorial/part_02/…
– Kick Buttowski
Dec 31 '18 at 19:03
What's wrong with your current code?
– Supa Mega Ducky Momo da Waffle
Dec 31 '18 at 19:03
What's wrong with your current code?
– Supa Mega Ducky Momo da Waffle
Dec 31 '18 at 19:03
I recieve the error <br>
ndefined method chomp' for nil:NilClass (NoMethodError)`– Jack Boylan
Dec 31 '18 at 19:03
I recieve the error <br>
ndefined method chomp' for nil:NilClass (NoMethodError)`– Jack Boylan
Dec 31 '18 at 19:03
add a comment |
3 Answers
3
active
oldest
votes
In the script runner of Atom where you are currently running your Ruby program, you can not read from standard input using gets. It appears that the script-runner package can extend this to provide a real terminal to the script where you can then also use STDIN.
Alternatively, you could also run your program from a real console. For that, you have run it from a command line window, e.g. with ruby name_of_program.rb instead of starting it from Atom.
Discovered issue, was to do with file pathway
– Jack Boylan
Dec 31 '18 at 19:25
add a comment |
The gets method must be called alone. Try it in your second line:
user_input = gets.chomp
without String.
I hope it useful for you. :)
add a comment |
Your code is working as intended. You went wrong by running the code in your text editor instead of through the console. The Kernal#gets method requires user input, which would need to be mocked in order to run within your text-editor. Because your editor is returning nil instead of user input in string format, the chomp method is raising your NoMethodError.
Essentially your code is fine, but your are trying to run it in a limited environment. As a beginner, if your code requires user input, it's easier to test the code by running your ruby file through the console/terminal with ruby <filename.rb>.
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%2f53990488%2finputting-string-using-gets-chomp-and-producing-a-histogram%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
In the script runner of Atom where you are currently running your Ruby program, you can not read from standard input using gets. It appears that the script-runner package can extend this to provide a real terminal to the script where you can then also use STDIN.
Alternatively, you could also run your program from a real console. For that, you have run it from a command line window, e.g. with ruby name_of_program.rb instead of starting it from Atom.
Discovered issue, was to do with file pathway
– Jack Boylan
Dec 31 '18 at 19:25
add a comment |
In the script runner of Atom where you are currently running your Ruby program, you can not read from standard input using gets. It appears that the script-runner package can extend this to provide a real terminal to the script where you can then also use STDIN.
Alternatively, you could also run your program from a real console. For that, you have run it from a command line window, e.g. with ruby name_of_program.rb instead of starting it from Atom.
Discovered issue, was to do with file pathway
– Jack Boylan
Dec 31 '18 at 19:25
add a comment |
In the script runner of Atom where you are currently running your Ruby program, you can not read from standard input using gets. It appears that the script-runner package can extend this to provide a real terminal to the script where you can then also use STDIN.
Alternatively, you could also run your program from a real console. For that, you have run it from a command line window, e.g. with ruby name_of_program.rb instead of starting it from Atom.
In the script runner of Atom where you are currently running your Ruby program, you can not read from standard input using gets. It appears that the script-runner package can extend this to provide a real terminal to the script where you can then also use STDIN.
Alternatively, you could also run your program from a real console. For that, you have run it from a command line window, e.g. with ruby name_of_program.rb instead of starting it from Atom.
answered Dec 31 '18 at 19:01
Holger JustHolger Just
36.1k980100
36.1k980100
Discovered issue, was to do with file pathway
– Jack Boylan
Dec 31 '18 at 19:25
add a comment |
Discovered issue, was to do with file pathway
– Jack Boylan
Dec 31 '18 at 19:25
Discovered issue, was to do with file pathway
– Jack Boylan
Dec 31 '18 at 19:25
Discovered issue, was to do with file pathway
– Jack Boylan
Dec 31 '18 at 19:25
add a comment |
The gets method must be called alone. Try it in your second line:
user_input = gets.chomp
without String.
I hope it useful for you. :)
add a comment |
The gets method must be called alone. Try it in your second line:
user_input = gets.chomp
without String.
I hope it useful for you. :)
add a comment |
The gets method must be called alone. Try it in your second line:
user_input = gets.chomp
without String.
I hope it useful for you. :)
The gets method must be called alone. Try it in your second line:
user_input = gets.chomp
without String.
I hope it useful for you. :)
answered Jan 2 at 5:40
SandraSandra
462
462
add a comment |
add a comment |
Your code is working as intended. You went wrong by running the code in your text editor instead of through the console. The Kernal#gets method requires user input, which would need to be mocked in order to run within your text-editor. Because your editor is returning nil instead of user input in string format, the chomp method is raising your NoMethodError.
Essentially your code is fine, but your are trying to run it in a limited environment. As a beginner, if your code requires user input, it's easier to test the code by running your ruby file through the console/terminal with ruby <filename.rb>.
add a comment |
Your code is working as intended. You went wrong by running the code in your text editor instead of through the console. The Kernal#gets method requires user input, which would need to be mocked in order to run within your text-editor. Because your editor is returning nil instead of user input in string format, the chomp method is raising your NoMethodError.
Essentially your code is fine, but your are trying to run it in a limited environment. As a beginner, if your code requires user input, it's easier to test the code by running your ruby file through the console/terminal with ruby <filename.rb>.
add a comment |
Your code is working as intended. You went wrong by running the code in your text editor instead of through the console. The Kernal#gets method requires user input, which would need to be mocked in order to run within your text-editor. Because your editor is returning nil instead of user input in string format, the chomp method is raising your NoMethodError.
Essentially your code is fine, but your are trying to run it in a limited environment. As a beginner, if your code requires user input, it's easier to test the code by running your ruby file through the console/terminal with ruby <filename.rb>.
Your code is working as intended. You went wrong by running the code in your text editor instead of through the console. The Kernal#gets method requires user input, which would need to be mocked in order to run within your text-editor. Because your editor is returning nil instead of user input in string format, the chomp method is raising your NoMethodError.
Essentially your code is fine, but your are trying to run it in a limited environment. As a beginner, if your code requires user input, it's easier to test the code by running your ruby file through the console/terminal with ruby <filename.rb>.
answered Jan 2 at 20:24
William MillsWilliam Mills
12
12
add a comment |
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%2f53990488%2finputting-string-using-gets-chomp-and-producing-a-histogram%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
I recommend you to read this. ruby-doc.org/docs/ruby-doc-bundle/Tutorial/part_02/…
– Kick Buttowski
Dec 31 '18 at 19:03
What's wrong with your current code?
– Supa Mega Ducky Momo da Waffle
Dec 31 '18 at 19:03
I recieve the error <br>
ndefined methodchomp' for nil:NilClass (NoMethodError)`– Jack Boylan
Dec 31 '18 at 19:03