Skip to content

Instantly share code, notes, and snippets.

@EsterKais
Last active May 31, 2017 17:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save EsterKais/2059ab78f960ac551a8646e242ed88f4 to your computer and use it in GitHub Desktop.
Save EsterKais/2059ab78f960ac551a8646e242ed88f4 to your computer and use it in GitHub Desktop.
Workshop: Rocking at Ruby

Installation Guide

Mac

Open your terminal: cmd + space and type terminal.

Maybe I already have Ruby!?

First type in ruby -v, if you get a response to that showing something like: ruby 2.3.1 ... that means you already have Ruby and there is nothing else you need to do! Don't concern yourself with the numbers too much. This is the Ruby version, for our purpose, it does not really matter.

If your system does not have Ruby follow the following actions:

Paste the following things in the terminal, one by one as they complete. Be patient! Some of these commands will not show anything, it will just take you to a whole new line. That's okay, just keep going! ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" brew update brew install rbenv echo 'eval "$(rbenv init -)"' >> ~/.bash_profile source ~/.bash_profile rbenv install 2.4.1 rbenv global 2.4.1 Check your ruby now: ruby -v

Linux

Maybe I already have Ruby!?

First type in ruby -v, if you get a response to that saying something like: ruby 2.3.1 ... that means you already have Ruby and there is nothing else you need to do! Don't concern yourself with the numbers too much. This is the Ruby version, for our purpose, it does not really matter.

If your system does not have Ruby follow the following actions:

sudo apt-get install ruby-full Check your ruby now: ruby -v

Windows

Maybe I already have Ruby!?

In your search function, type in Command Prompt and see if you get the option: Start Command Prompt with Ruby If so, all should be well!

If your system does not have Ruby follow the following actions:

http://www.dummies.com/programming/ruby/how-to-install-and-run-ruby-on-windows/

Optional!

In all technicality, you can code in Notepad(Win)/TextEdit(OS) if you'd like! However, it is not a very pleasant experience. That is why smart people have created fancy text editors that help you out a lot by autofilling things for you and it generally looks better! :)

One of these is: https://atom.io/. Feel free to get it! Just follow the instruction on the site and enjoy the softness and comfort of a helpful text editor!

SLIDESHOW

http://www.slideshare.net/secret/arSBak5n3SGNco

SLIDE: GIDDY UP!

There are many programming languages out there. I am sure you have heard of JavaScript, maybe the different variations of C(C++, C#) and so forth. I still hear new names.

So, why so many? They all look the same and complicated to me!

Well, there are technical and not-so-technical reasons for it. The technical reasons might be the speed of processing, for example. But I don’t want to go into many details with this. Some languages are better at some computer things.

However, another important aspect is how you write them! For a beginner, that is especially important!

JAVASCRIPT

  • var
  • ;

ALL THE C'S

  • int, char, float
  • ;

RUBY

  • Yukihiro Matsumoto
  • Nothing! Just clean, easy, easy to read code. Semantic: this allows your code to be a story! We will later see Animal.new("Meow"), you can almost read this: "Make me a new animal, that says meow".

SLIDE: WHAT DOES THE FOX SAY?

If you think of an animal, you can think of many different animals:

dog, cat, sloth - they all look quite different from each other, however they all do similar things: they all eat, they all breathe, they can all move (some slower than others).

The concept/idea/blueprint of the animal, is called a class in Ruby: class Animal; end.

Notice how it starts with the word class and ends with an end. Few things to notice here:

  • First of all, casing matters, otherwise the complier (the cool thing-a-ding that translates your Ruby into something the computer understands) does not understand what you mean. So class and end should be lowercase, the classname itself needs to be capitalized.
  • Secondly, this empty space in between the class and end is called a code block. This is where the code goes.
    • Little tip: fancy editors do this automagically, but when you write code, ALWAYS finish your code-block immediately, be it a ; or end.

So, we are going to give our animals some basic abilities, such as eating, sleeping & breathing.

For this, we use methods. Methods are actions that the class is able to perform. These methods can only work together with a class, so in fancy words, you call a method on the class. So you will call breathe on your Animal - which in simplest terms means that you are telling the Animal: "Hey, I want you to breathe now!" - so you always sort of direct/assign the action on the class/animal.

NEXT SLIDE

So, methods are in other words, what our Animal will be able to do. You define methods in the following way. Convention time: methods are always lowercase and camelcase, which means, if you want to use several words you use underscore.

  • FYI: You CAN do fancy things to it such as put question-, exclamation marks at the end to depict better what they do, however that is a bit out of the scope.

We are also getting in contact with some data-types now. You see the quotation marks? Those are important. This tells Ruby: hey! This is a text/word - called a string in the code world. There are several others, one being integer and float: full number and a decimal number, boolean: true/false. There are more, but these are the most used.

Now, let's make sense of all of this: how does it all come together, the class, the methods, data-types??

SLIDE: RUBY ACTION: WRITE WITH ME!

I learned that the best way to learn to code is by typing - NOT COPYING, typing! Thus this is what we will be doing together from now on. I will be live-coding and you will be writing with me! So, let's write our first piece of code and make what I just talked about more clear!

LIVE CODE:

  • puts - Ruby is magic: thus it also has some built in methods, that can do things on their own and help you out. puts for example tells the program "Hey! Could you print this out on the screen, when I run this program/call this method?"
  • new - This magic-method is used to bring your animal/class to life! Animal on its own is nothing, but just an idea/blueprint, but new breathes life into it and makes it active and ready to use.
  • For simplicity, I am assigning our newly risen animal to a variable, so it is easier for us to keep track of our animals. Otherwise we will not know which of our newly risen animals is which.

Here you are! This little piece of code will run and create your animals, who will do what you just told them to do! But how do you know? Well, let's run it.

Save it! To keep it easy, let's put everything on the desktop for now.

In your terminal, lets navigate to the folder your code is at:

  • ls to see where you are and where you can go.
  • cd <folder/file> to navigate to the place you want to go to.
  • ruby animal.rb to run your program.

WHAT DOES THE FOX SAY?

However, animals can do things differently, for example speak. How do we deal with that? We could write “Meow”, but then all of our animals will say "Meow"?! Which the computer will believe as you are always right in the computer’s eyes (that's why the tiger_rabbit)! However, you know it’s not right. So how do we fix this?

For that we need to readjust our animal class so the Animal will actually behave according to our rules.

initializer - this scary word is nothing more than an action/method that will construct your animal. Let's say you want your rabbit to be tiger print and say "roar" and eat cereal - the initializer will take this info and morph it into your tiger_rabbit! Remember new? It basically makes the new more advanced!

We can use it the following way:

  • cat = Animal.new("Meow") - here we give the initializer the material to create our cat from. Please notice the quotation marks!
  • This "Meow" will be received in our method def initialize(info); end.
  • So, in order for us to use this "Meow" we need to assign it to an instance variable. Normal variables can only be used inside the method, however instance variables can be used anywhere in the class.

So there you have it! In this way, you can do pretty much anything! These are the basics, but at the same time, this is how applications are built in Ruby! Class by class, method by method.

SLIDE: LET'S TAKE IT UP A NOTCH

We will be building a rock-paper-scissors game. In human language: you will be able to make a choice, between rock, paper and scissors and we will give the computer the same 3 options to respond with.

You will learn further about what you can do with methods, you will learn a new type of a variable: array and also see more of these magical built in methods that are available for all classes and situations.

We will be writing this together all the way, me here and you copying me - if you are faster than the group as a whole, feel free to add some sparkle to your game: you can add nice texts, ask for a player name, maybe extra functions like a point system (best of three), make it into a fully two player game (you enter your choice, your mate enters another and the program determines who won etc..).

Let’s get to it!

class Animal
def breathe
puts "*Heavy Breathing*"
end
def sleep
puts "ZzzZZzz"
end
def eat
puts "OmNomNomNom"
end
end
# To actually see your code working, we need to call it:
cat = Animal.new
puts "My cat is breathing: "
cat.breathe
dog = Animal.new
puts "My dog is sleeping: "
dog.sleep
tiger_rabbit = Animal.new
puts "My tiger rabbit is eating: "
tiger_rabbit.eat
class Animal
def initialize(speech)
@speak = speech
end
def speak
@speak
end
end
# To see it in action:
cat = Animal.new("Meow")
puts "My cat says: "
cat.speak
dog = Animal.new("Woof")
puts "My dog says: "
dog.speak
# Step 1: Defining the class:
class Game
# Step 2: The game should have a start - see it as the start button:
def start
end
# Step 3: This version of the game will have two turns.
def my_turn
end
def your_turn
end
# Step 4: And most importantly:
def who_won
end
end
class Game
# Step 1: So what would that imaginary start button do:
def start
# Remember our friend `puts`?
puts "Welcome to Rock-Paper-Scissors!"
end
def my_turn
end
def your_turn
end
def who_won
end
end
# Step 2: Remember in our animal example, we had to make sure that the terminal shows us something. Most people start
# reading from the top and from left to right. Compliers do the same, thus you cannot call a class before it is actually defined,
# thus we need to put our terminal stuff below our code.
# To keep track of this specific game, let's assign it to a variable. You can see it as a save game:
current_game = Game.new
current_game.start
# Awesome, we have an imaginary start button and if that gets pressed (i.e. we run our game in the terminal)
# we will see our welcome message.
class Game
def start
puts "Welcome to Rock-Paper-Scissors!"
end
# Step 2: So, how do we now accept your choice?
def my_turn
# Let's make it clear that we want you to type something:
puts "What will it be? Rock, paper, or scissors?"
# Remember instance variables?
@my_guess = gets.chomp
# Now, there are two weird new words there. `gets` is a method that will tell Ruy to expect input from the user, in this
# case, you. When the code reaches `gets` the terminal will give you the option to type in your option and `enter` will
# register this choice. `chomp` makes sure that the `enter` will not be recorded by Ruby as part of your input.
end
def your_turn
end
def who_won
end
end
# Game flow:
current_game = Game.new
current_game.start
# Step 1: Now we need a way to get -you- to give us your choice. Let's first call it:
current_game.my_turn
class Game
def start
puts "Welcome to Rock-Paper-Scissors!"
end
def my_turn
puts "What will it be? Rock, paper, or scissors?"
@my_guess = gets.chomp
end
# How can we make the computer play? Remember, you're a mama-bird!
def your_turn
# Step 2: We need to give the computer the options it can choose from.
# For this, we can use an `array`, which is basically a bucket of stuff.
# A collection of data that can be accessed by its position in the `array`:
# IMPORTANT: In the programming world, we start counting from 0!
# NOTE: quotation marks = strings!
options_bucket = ["rock", "paper", "scissors"]
# Step 3: So, the computer has to select one of these options, randomly.
# This must be super difficult!? Well, remember the cheerful Japanese Programmer? He thought about this:
@computer_guess = options_bucket.sample
end
def who_won
end
end
# Game flow:
current_game = Game.new
current_game.start
current_game.my_turn
# Step 1: It's the computer's turn:
current_game.your_turn
class Game
def start
puts "Welcome to Rock-Paper-Scissors!"
end
def my_turn
puts "What will it be? Rock, paper, or scissors?"
@my_guess = gets.chomp
end
def your_turn
options_bucket = ["rock", "paper", "scissors"]
@computer_guess = options_bucket.sample
end
# Step 2: This is the heart of the game - the logic, thus also the most complicated part of the game!
# Who wins? Who loses?
def who_won
# Step 3: Let's start with the simplest case
if @my_guess == @computer_guess
puts "It's a tie!"
# Step 4: Now, in real life, this is where developers might take a pen and paper and start writing!
# Let's write out all the options we might have!
# Rock > Scissors
# Paper > Rock
# Scissors > Paper
# How would we write this in Ruby?
elsif @my_guess == "rock" && @computer_guess == "scissors" ||
@my_guess == "paper" && @computer_guess == "rock" ||
@my_guess == "scissors" && @computer_guess == "paper"
puts "You win!"
else
puts "You lose!"
end
end
end
# Game flow:
current_game = Game.new
current_game.start
current_game.my_turn
current_game.your_turn
# Step 1: Most importantly, who won??
current_game.who_won
# Here you are! You have a fully operational game!
# But before we are completely done, developers usually take time after they finish to clean up their code and make it better.
# Let's do that!
class Game
def start
puts "Welcome to Rock-Paper-Scissors!"
puts "*******************************"
end
def my_turn
puts "What will it be? Rock, paper, or scissors?"
puts "*******************************"
# `downcase` makes sure that no matter how you enter your response: rOck, Paper, SCIssors, Ruby will convert it to "rock".
@my_guess = gets.chomp.downcase
# It might be nice to see what the computer got from you:
puts "You chose #{@my_guess}."
# These weird #curlybracers in there tell Ruby to show you the -value- of the variable, not the variable itself "@my_guess".
# This is called string interpolation.
end
def your_turn
options_bucket = ["rock", "paper", "scissors"]
@computer_guess = options_bucket.sample
# It might also be nice to see what the computer chose:
puts "You chose #{@computer_guess}."
end
def who_won
if @my_guess == @computer_guess
puts "*******************************"
puts "It's a tie!"
puts " ¯\\_(ツ)_/¯"
elsif @my_guess == "rock" && @computer_guess == "scissors" ||
@my_guess == "paper" && @computer_guess == "rock" ||
@my_guess == "scissors" && @computer_guess == "paper"
puts "*******************************"
puts "You win!"
puts "〈( ^.^)ノ"
else
puts "*******************************"
puts "You lose!"
puts "(✖╭╮✖)"
end
end
end
# Game flow:
current_game = Game.new
current_game.start
current_game.my_turn
current_game.your_turn
current_game.who_won
class Game
def start
puts "Welcome to Rock-Paper-Scissors!"
puts "*******************************"
end
def my_turn
puts "What will it be? Rock, paper, or scissors?"
puts "*******************************"
@my_guess = gets.chomp.downcase
puts "You chose #{@my_guess}."
end
def your_turn
options_bucket = ["rock", "paper", "scissors"]
@computer_guess = options_bucket.sample
puts "You chose #{@computer_guess}."
end
def who_won
if @my_guess == @computer_guess
puts "*******************************"
puts "It's a tie!"
puts " ¯\\_(ツ)_/¯"
elsif @my_guess == "rock" && @computer_guess == "scissors" ||
@my_guess == "paper" && @computer_guess == "rock" ||
@my_guess == "scissors" && @computer_guess == "paper"
puts "*******************************"
puts "You win!"
puts "〈( ^.^)ノ"
else
puts "*******************************"
puts "You lose!"
puts "(✖╭╮✖)"
end
end
end
# Game flow:
current_game = Game.new
current_game.start
current_game.my_turn
current_game.your_turn
current_game.who_won
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment