Ruby -- 'gets' adds newline character [closed] - ruby

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I wrote this:
print "Enter your name:"
name = gets
puts "Hello #{name}. Please to meet you."
and the result was like this:
Hello Moemen
. Pleased to meet you
Why is the remainder of the string after the variable continued in another line? I want it to be "Hello Moemen. Pleased to meet you." Am I missing something?
I'm using sublime text 2, and I couldn't get the gets method to let me input data; it just prints the outcome in the console without giving me a chance to input anything. Any idea?

When you use gets, the input is going to be followed by a newline so you will need to strip the newline before printing it out.
print "Enter your name:"
name = gets.chomp
puts "Hello #{name}. Pleased to meet you."
That should solve your problem.

What you report would not happen. It cannot be true. When you get a string by gets, you may get a string like "Moemen\n". Since an input is delimited by "\n", the string has that at the end. When it is interpolated into "Hello #{name}. Please to meet you.", you would get:
Hello Moemen
. Please to meet you.
but not
Hello Moemen
. Pleased to meet you
as you report. That does not happen.
In order to get "Hello Moemen. Pleased to meet you.", you need to change the string to "Hello #{name.chomp}. Pleased to meet you.".

Related

Ruby: how to interpolate in a string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
Is a simple question about how to print an interpolation on a string in ruby
My method is this:
def hello(parameter)
puts "hello + #{parameter}"
end
puts hello(name)
but irb, show me these message:
NameError: undefined local variable or method `name' for main:Object
It is about my commands to irb? or what i'm doing wrong in my code, 'cause i know that i 've a mistake but can't find where.
Thank you, it's my first post ejje.
The issue lies in your call to hello(name). name in this case is treated at the name of a variable, which you have not defined. If you meant for the console to print "hello + name", then you should call the function with the string "name", surrounded by quote marks: hello("name").

A ruby program that takes a name input from user and confirms it. I am not fully being able to code it. Help required [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
What can be an appropriate code for this ?
I tried using the name = gets.chomp and following it with a "IS IT CORRECT"-esque question. Followed it with a if-else construct but its not working apparently.
Thanks
What error did you get? This should be quite simple.
puts "Enter name"
name = gets.chomp
puts "Is #{name} correct?"
confirm = gets.chomp
if confirm == 'y'
puts "Your name is #{name}"
else
puts "Your name is not #{name}"
end
Look also at the examples here: http://www.evc-cit.info/cit020/beginning-programming/chp_02/conditionals.html

How do I add a string at the end of another string if there is not match using sub [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to add string1 to the end of an string2 if string2 does not include string1.
Can I do this using sub?
I tried a few combinations but they are not doing anything. I'm not even sure I'm doing it right. I'm very bad at Regex and would really appreciate some help.
Generally you'd use this pattern:
string = "test"
insert = "er"
string << insert unless (string.match(insert))
# => "tester"
Why do you need a sub? you can just do something like:
a = "abcd"
b = "bc"
c = a + b unless a.include?(b)

how to enter value in an array during runtime using ruby [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
how to enter the values in an array dynamically and to use it
and I'm trying to reverse it after getting the input from the keyboard.
This should get you started:
array = []
puts "Please enter each item on a separate line, then"
puts "end the input by hitting ENTER on an empty line."
while line = gets.chomp
break if line.empty?
array << line
end
puts "You entered:"
puts array.reverse
You really need to read a basic tutorial about Ruby, I have recommended this one to a few people and they liked it: https://pine.fm/LearnToProgram/.
If you want to reverse-load an array (that is, load it in reverse order, so oldest is last) then use 'unshift' while you're adding the array elements.
array.unshift = gets.chomp

How to ask a number in Ruby? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to make a program like,ask a number and print 1 to number by using gets and by using loop. So,I am asking about gets and how to do program which is I given below as program title.
How to ask a number by using gets?If possible explain me with example.
By using gets,I want to print 1 to number. My program titleis Ask a number and print 1 to number by using Ruby.
How can I solve that program?Please help me on this.
As Arup, suggested use Kernel#gets to capture a user input from terminal. The remaining bit can be simply done with a for loop:
num = gets.to_i #Convert the user input to integer
for i in 1..num
puts i
end
You can further modify this to suit your need.
Do as below using Kernel#gets. #gets will give you a string, then to convert the number string to a number use String#to_i.
number = gets.to_i
If I want to make program which from 1 to number then what should I do?
Use a Range then.
(1..number).each do |n|
# code
end

Resources