DEV Community

changj35513
changj35513

Posted on

Ruby Folder Syntax Examples

app > config > routes:

  get("/rock",{:controller => "application", :action => "play_rock"})

  get("/paper",{:controller => "application", :action => "play_paper"})

  get("/scissors",{:controller => "application", :action => "play_scissors"})
Enter fullscreen mode Exit fullscreen mode

app > controllers > applications_controller.rb

class ApplicationController < ActionController::Base
  layout(false)

  # Add your actions below this line
  # ================================

  def play_rock

    index = rand(3) 
    moves = ["rock", "paper", "scissors"] 
    @comp_move = moves.at(index)

    if @comp_move == "rock" 
      @outcome = "tied" 
    elsif @comp_move == "paper"
      @outcome = "lost"
    elsif @comp_move == "scissors" 
      @outcome = "won" 
    end

    render({:template => "game_templates/rock.html.erb"})
  end

  def play_paper

    index = rand(3)
    moves = ["rock", "paper", "scissors"]
    @comp_choice = moves.at(index)

    if @comp_choice == "rock"
      @outcome = "won"
    elsif @comp_choice == "paper"
      @outcome = "tied"
    elsif @comp_choice == "scissors"
      @outcome = "lost"
    end

    render({:template => "game_templates/paper.html.erb"})
  end

  def play_scissors

    index = rand(3)
    moves = ["rock", "paper", "scissors"]
    @comp_option = moves.at(index)

    if @comp_option == "rock"
      @outcome = "lost"
    elsif @comp_option == "paper"
      @outcome = "won"
    elsif @comp_option == "scissors"
      @outcome = "tied"
    end

    render({:template => "game_templates/scissors.html.erb"})
  end

end
Enter fullscreen mode Exit fullscreen mode

app > views > (created from here on out)
game templates >
(rock or paper or scissors).html.erb

<h2>We played (rock/paper/scissors)!</h2>

<h2>They played <%= @comp_choice %>! </h2>

<h2>We <%= @outcome %>! </h2>

Enter fullscreen mode Exit fullscreen mode

Top comments (0)