3
\$\begingroup\$

I'm learning Ruby and I would like to know if I have used the good syntax. My script works, but can I simplify it and make it more elegant.

puts "Hello, what is youre name?"
name = gets.chomp

puts "Are you a boy? (yes/no)"
genre = gets.chomp

if genre == ("yes")
    puts "Do you like soccer #{name}? (yes/no)"
    foot = gets.chomp

        if foot == ("yes")
            puts "And beer? (yes/no)"
            biere = gets.chomp

                if biere == ("yes")
                    puts "Nice buddy!"
                else 
                    puts "Too bad #{name}! But you like soccer!"
                end
        else 
            puts "You're complicated #{name}!!!"
        end
else 
    puts "Do you like romantics movies?"
    film = gets.chomp
end
\$\endgroup\$
  • \$\begingroup\$ How long you think this conversation between user and program will be? It really depends on that whether you can continue this style or need to employ better design. \$\endgroup\$ – Wand Maker Jan 3 '16 at 17:38
  • \$\begingroup\$ Might want to ask yourself what can happen if you don't get the input you want \$\endgroup\$ – nikkypx Jan 4 '16 at 2:22
  • 1
    \$\begingroup\$ Considering that your question has been moved to Code Review, which has a different purpose than SO, I suggest you remove the green checkmark from my answer, to encourage others to post answers. \$\endgroup\$ – Cary Swoveland Jan 12 '16 at 18:41
1
\$\begingroup\$

I suggest defining an ask for free-form answers and a choose function for multiple-choice questions. The choose function can perform validation.

You can use case-when instead of if-else.

def ask(msg)
  puts msg
  gets.chomp
end

def choose(msg, choices)
  loop do
    down_choices = choices.map(&:downcase)
    puts "#{msg} (#{choices.join('/')})" 
    result = gets.chomp.downcase
    return result if down_choices.include?(result)
    puts "That answer is invalid"
  end
end

name = ask "Hello, what's you're name, dude?"
case choose "Are you a boy?", ["yes", "no"] 
when "yes" # boy
  case choose "Do you like soccer #{name}?", ["yes", "no"]
  when "yes" # likes soccer
    case choose "And beer?", ["yes", "no"]
    when "yes" # likes beer
      puts "Nice buddy!"
    else # doesn't likes beer
      puts "Too bad #{name}! But you like soccer!"
    end
  else # doesn't like soccer 
    puts "You're complicated #{name}!!!"
  end
else # girl
  ask "Do you like romantics movies?"
end

Note that nothing is done with the answer to the question, "Do you like romantics movies?".

|improve this answer|||||
\$\endgroup\$
-2
\$\begingroup\$

You can use classes. I would be more thorough, but not simpler.

(Disclaimer: I'm a noob too, so don't take this as the way to do it. I got into some trouble here trying to make sure all the classes had the necessary variables. But it works!)

class Say
  def greeting
    print "Hello, what is youre name?  "
    @name = gets.chomp.capitalize
    @name = "John_Doe" if @name == ''

    print "Are you a boy? [Y/N]  "
    a_boy = gets.chomp.downcase
    gender = Logic.new.evaluate(a_boy)

    {:name => @name.downcase, :gender => (gender ? :male : :female)}
  end

  def response(topic, condition)
    case topic
    when :beer
      if condition == true
        puts "\nNice buddy!"
      else
        puts "\nToo bad #{@name}! But you like soccer!"
      end
    when :movies
      if condition == true
        puts "\nI figured as much..."
      else
        puts "\nWhat kind of girl doesn't like romantic movies?"
      end
    else
      puts "\nYou're complicated #{@name}!!!"
    end
  end

end

class DoYouLike
  attr_accessor :person

  def initialize(person)
    begin
      @person = person
      @name = @person[:name].capitalize
      #@gender = @person[:gender].capitalize
      @logic = Logic.new
    rescue
      @name = @person.capitalize
    end
  end

  def soccer?
    answer = nil

    while answer.nil?
      print "Do you like soccer #{@name}? [Y/N] "
      foot = gets.chomp.downcase

      answer = @logic.evaluate(foot)
    end

    answer
  end

  def beer?
    answer = nil

    while answer.nil?
      print "And beer? [Y/N] "
      beer = gets.chomp.downcase

      answer = @logic.evaluate(beer)
    end
    answer
  end

  def romantic_movies?
    answer = nil

    while answer.nil?
      print "Do you like romantics movies? "
      film = gets.chomp.downcase

      answer = @logic.evaluate(film)
    end
    answer
  end


end

class Logic
  def initialize
    @say = Say.new
  end

  def pass_var(you_like)
    @you_like = you_like
    @person = @you_like.person
  end

  def decision_tree
    if @person[:gender] == :male
      if @you_like.soccer?
        if @you_like.beer?
          @say.response(:beer, true)
        else
          @say.response(:beer, false)
        end
      else
        @say.response(:soccer, false)
      end
    else
      if @you_like.romantic_movies?
        @say.response(:movies, true)
      else
        @say.response(:movies, false)
      end
    end
  end

  def evaluate(answer)
    case answer
    when 'yes', 'y'
      true
    when 'no', 'n'
      false
    else
      puts "Please answer yes or no"
      nil
    end
  end
end


say = Say.new
person = say.greeting
you_like = DoYouLike.new(person)
logic = Logic.new
logic.pass_var(you_like)
logic.decision_tree
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy