2

I want to do something like that.

puts "Please write your age: "
age = gets.chomp

if #{age}<18 
puts "you are illegal"

else #{age}>18
puts "You are legal"
end

the output i get is:

"Please write your age" 15. you are illegal you are legal"

and this

"Please write your age 20 you are illegal you are legal"

Why? And what is the solution please?

What I expect is this If I write 19 or older, it will say "you are legal" And if I write 17 or any number below It will tell me "You are illegal"

eb1212
  • 23
  • 3
  • Welcome to Stackoverflow, please format the output of your code with the `{}` button – Caridorc Jan 20 '23 at 11:31
  • Once you have `age`, say `age = 15`, you could use *string interpolation*: `puts "you are #{ age > 18 ? "" : "not " }of legal age"` displays `you are not of legal age`. – Cary Swoveland Jan 20 '23 at 22:51

4 Answers4

4

Welcome to StackOverflow.

#{} is used for string interpolation, you don't need it there, and else statements don't work like this (elsif does). You also need to convert the string to an integer. You could write it like this:

puts "Please write your age: "
age = gets.chomp.to_i

if age > 18 # Since you want 19 or older. You could use age > 17 or age >= 18 if you actually meant 18 or older.
  puts "You are of legal age"
else
  puts "You are not of legal age"
end

See

Felipe Zavan
  • 1,654
  • 1
  • 14
  • 33
  • thank you all I added the to_i and I canceled the #{} And it helped!! Thank you very much!! – eb1212 Jan 20 '23 at 11:48
  • i do it but is show me this "Thanks for the feedback! You need at least 15 reputation to cast a vote, but your feedback has been recorded." – eb1212 Jan 20 '23 at 12:02
  • 1
    _"#{} is used for string interpolation"_ – true, but only in certain [string literals](https://ruby-doc.org/3.2.0/syntax/literals_rdoc.html#label-String+Literals). Within code, `#` starts a [comment](https://ruby-doc.org/3.2.0/syntax/comments_rdoc.html). – Stefan Jan 20 '23 at 12:47
2

The problem is that your code is equivalent to:

puts "Please write your age: "
age = gets.chomp

if 
puts "you are illegal"
else
puts "You are legal"
end

Because # starts a comment, that makes the interpreter ignore everything after it on that line.

You can use any of the suggestions in the other answers to fix the code.


I suggest using the -w flag to let the interpreter warn you about possible sources of bugs:

$> ruby -w in.rb
in.rb:4: warning: `if' at the end of line without an expression
Caridorc
  • 6,222
  • 2
  • 31
  • 46
0
age = gets.chomp.to_i

if age<18

... to get integer-to-integer comparison.

pts
  • 80,836
  • 20
  • 110
  • 183
-1

You should first convert the input type to Integer and then make your logic. Note that is also important to check if the string input is numeric (since to_i returns 0 on cases like 'a'.to_i). You can do that like so:

puts 'Please write your age: '
# strip removes leading and trailing whitespaces / newlines / tabs
age = gets.strip

unless age.to_i.to_s == age
  puts 'Age must be a number'
  exit
end

age = age.to_i

if age < 18 
  puts 'you are illegal'
else
  puts 'You are legal'
end
jvx8ss
  • 529
  • 2
  • 12