Open your terminal: cmd + space
and type terminal
.
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.
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
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.
sudo apt-get install ruby-full
Check your ruby now: ruby -v
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!
http://www.dummies.com/programming/ruby/how-to-install-and-run-ruby-on-windows/
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!
http://www.slideshare.net/secret/arSBak5n3SGNco
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!
var
;
int
,char
,float
;
- 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".
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).
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
andend
should be lowercase, the classname itself needs to be capitalized. - Secondly, this empty space in between the
class
andend
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
;
orend
.
- Little tip: fancy editors do this automagically, but when you write code, ALWAYS finish your code-block immediately, be it a
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.
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??
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!
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, butnew
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.
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 theinitializer
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.
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!