Using a method to puts variables - ruby

I want to use capitalize method and puts a string with the user input.
puts "What is your name?"
name = gets.chomp
puts "Hi, " name.capitalize "how are you?"
Here is the error after line 2:
syntax error, unexpected tIDENTIFIER, expecting $end puts "Hi, "
name.capitalize "how are you?"

You need to concatenate the strings or interpolate. Your options are:
puts "What is your name?"
name = gets.chomp
puts "Hi, " + name.capitalize + " how are you?"
or
puts "What is your name?"
name = gets.chomp
puts "Hi, #{name.capitalize} how are you?"

You need to use string interpolation. Here is a RubyMonk tutorial you may enjoy.
puts "Hi, #{name.capitalize}, how are you?"

Related

Using regex in simple if statements?

Instead of doing:
puts "what type of input?"
input = gets.chomp
if %W[Int INT i I Ints ints].include?(input)
puts "enter int"
i = gets.to_i
I want to use regex to interpret string user input. For example,
puts "are you entering in a string, an int or a float?"
case gets
when /\A(string|s)\z/i
puts "enter in a string"
gets.chomp
when /\A(int|i)\z/i
puts "enter an int"
gets.to_i
when /\A(float|f)\z/i
puts "enter a float"
gets.to_f
end
What is the syntax in order to get the same result but using if statements instead of case statement?
gets returns a string with a trailing carriage return. What you need is to match the ending against \Z, not \z.
puts "are you entering in a string, an int or a float?"
case gets
when /\As(tring)?\Z/i
puts "enter in a string"
gets.chomp
when /\Ai(nt)?\Z/i
puts "enter an int"
gets.to_i
when /\Af(loat)?\z/i
puts "enter a float"
gets.to_f
else puts "Didn’t work"
end
I also slightly updated regexps to clearly show the intent.
If you want to turn your case into an if, you have to store the expression intended for the gets into a variable:
response=gets.chomp
if /..../ =~ response
...
elsif /.../ =~ response
....
....
else
...
end

Check if a string is a string in ruby

I want to check a string entered by a user. If it is a string, then move along, if not, then throw an error. I do not know how to check if the user input is a string or an int. Below is my code:
puts "what is your name?"
name = gets.chomp
if name == Int
puts "error enter a string"
end
if birthdate != Int
puts "error enter your birthdate"
end
puts "how old are you "
age = gets.to_i
if age == String
puts "error please enter your age"
end
puts "hello" + name + " wow that is a good day to be born" + "thats a great age"
puts "the half of your age is #{age/2.0} that is good to know"
It may be checked as this:
puts "what is your name?"
name = gets.chomp
if !(name =~ /[0-9]/).nil?
puts "error enter a string"
end
puts "what is your age?"
age = gets.chomp
if age.to_i.to_s == age
puts "error enter a integer"
end
I think this is what you are looking for.
if name.class == String
Also this will always be false
age = gets.to_i # this is converting to integer
if age.class == String # I think this is what you meant
age will always be an Integer her because you have cast it as such with .to_i

Ruby won't divide string

I am trying to divide a user-input age by 2. My code is below:
puts "what is your name?"
name = gets.chomp
puts "when were you born please enter your birthdate"
birthdate = gets.chomp
puts "how old are you "
age = gets.chomp
puts "hello" + name + " wow that is a good day to be born" + "thats a great age"
puts "the half of your age is" + age/2 + " that is good to know"
It does not work.
Your age is a string
age = gets.to_i
Now it's a number. But you can't concatenate a string and a number. Two options:
interpolation
puts "the half of your age is #{age/2} that is good to know"
or
puts "the half of your age is " + (age/2).to_s + " that is good to know"

How can I store my `gets` method in a variable?

I need to store a user input in a variable. This is my code:
puts "Hi! I'm HAL, what's your name?"
gets.strip
name = gets.strip
greeting(name)
This isn't working.
This may not be what you want, but it answers the question posed in the title.
You can hold the method Kernel#gets in a variable like so:
m = method(:gets)
#=> #<Method: Object(Kernel)#gets>
Now let's use it.
def greeting(name)
puts "Me? I'm #{name}"
end
puts "Hi! I'm HAL, what's your name?"
name = m.call.strip # "Dave Bowman" is entered
name holds the user's response, after the string is stripped of any enclosing whitespace and the trailing newline character.
greeting(name)
Me? I'm Dave Bowman
Try this:
puts "Hi! I'm HAL, what's your name?"
name = gets.strip
greeting(name)

Ruby redaction not printing

I am running through the Codecademy lessons to get up to speed with the basics of Ruby but I hit a snag with their redacted! exercise. I posted what I think should do the trick but it refuses to print.
The exercise instructions are as such :
Let's start simple: write an .each loop that goes through words and just prints out each word it finds.
Please help if you can. Thank you! Here is my code :
puts "What is your text, bra?"
text = gets.chomp
text.downcase!
puts "What is being hidden?"
redact= gets.chomp
redact.downcase!
words = text.split(" ")
eachredact = redact.split(" ")
#loop through the words in the string
words.each do |x|
print "#{x}" + " "
end
I ran it as below :
puts "What is your text, bra?"
text = gets.chomp
text.downcase!
puts "What is being hidden?"
redact= gets.chomp
redact.downcase!
words = text.split(" ")
eachredact = redact.split(" ")
#loop through the words in the string
words.each do |x|
print "#{x}" + " "
end
Output:
What is your text, bra?
i anm
What is being hidden?
Good
i anm
see here:

Resources