Looking at your code, you have a few issues there:
- The main one you mentioned in your question and title is that the name you print comes in a new row for each iteration of the loop. That's because you're using
puts
but should be using print
in your case. You can read more about that here
2.You're calling the #read_string
method which isn't defined anywhere in your code. What you want to do is either replace it with gets.chomp
More about gets here or define your #read_string
method like this:
def read_string
gets.chomp
end
3.As Stefan mentioned, you're using or
which probably isn't what you're looking for here (More about that here) You're better off using the ||
operator in your case.
Fixing these mistakes brings us to a working version of your code:
def print_silly_name_60_times
60.times do
print "silly "
end
end
def main
name = gets.chomp
if ( name == "benyamin") || ( name == "jack" )
puts " #{name} that is a nice name."
else
print_silly_name_60_times
end
end
main
After cleaning up a few things and making it a bit more compact, we get:
def print_silly_name_60_times
60.times{print "silly "}
end
def main
name = gets.chomp
%w(benyamin jack).include?(name) ? (puts " #{name} that is a nice name.") : print_silly_name_60_times
end
main