Learning Ruby: Fra nul til helt

"Rubin er enkel i udseende, men er meget kompleks indeni, ligesom vores menneskelige krop." - Matz, skaberen af ​​Ruby programmeringssprog

Hvorfor lære Ruby?

For mig er den første grund, at det er et smukt sprog. Det er naturligt at kode, og det udtrykker altid mine tanker.

Den anden - og hovedårsag er Rails : den samme ramme, som Twitter, Basecamp, Airbnb, Github og så mange virksomheder bruger.

Introduktion / Historie

Ruby er ”Et dynamisk, open source programmeringssprog med fokus på enkelhed og produktivitet. Den har en elegant syntaks, der er naturlig at læse og let at skrive. ” - ruby-lang.org

Lad os komme i gang med nogle grundlæggende!

Variabler

Du kan tænke på en variabel som et ord, der gemmer en værdi. Så simpelt er det.

I Ruby er det let at definere en variabel og indstille en værdi til den. Forestil dig, at du vil gemme nummer 1 i en variabel kaldet en. Lad os gøre det!

one = 1

Hvor simpelt var det? Du har lige tildelt værdien 1 til en variabel kaldet en.

two = 2 some_number = 10000

Du kan tildele en værdi til den variabel, du ønsker. I eksemplet ovenfor gemmer en to variabler et heltal på 2 og nogle_numre gemmer 10.000.

Udover heltal kan vi også bruge booleanske (true / false), strenge, symboler, float og andre datatyper.

# booleans true_boolean = true false_boolean = false # string my_name = "Leandro Tk" # symbol a_symbol = :my_symbol # float book_price = 15.80

Betingede udsagn: Kontrolstrøm

Betingede udsagn vurderer sandt eller falsk. Hvis noget er sandt, udfører det, hvad der er inde i udsagnet. For eksempel:

if true puts "Hello Ruby If" end if 2 > 1 puts "2 is greater than 1" end

2 er større end 1, så putskoden udføres.

Denne anden erklæring udføres, når if-udtrykket er forkert:

if 1 > 2 puts "1 is greater than 2" else puts "1 is not greater than 2" end

1 er ikke større end 2, så koden inde i den anden sætning udføres.

Der er også elsif-erklæringen. Du kan bruge det på denne måde:

if 1 > 2 puts "1 is greater than 2" elsif 2 > 1 puts "1 is not greater than 2" else puts "1 is equal to 2" end

En måde, jeg virkelig kan lide at skrive Ruby, er at bruge en if-sætning efter koden, der skal udføres:

def hey_ho? true end puts "let’s go" if hey_ho?

Det er så smukt og naturligt. Det er idiomatisk Ruby.

Looping / Iterator

I Ruby kan vi gentage i så mange forskellige former. Jeg vil tale om tre iteratorer: mens, for og hver.

Under looping: Så længe udsagnet er sandt, udføres koden inde i blokken. Så denne kode udskriver nummeret fra 1 til 10:

num = 1 while num <= 10 puts num num += 1 end

Til looping: Du sender variablen num til blokken, og for-sætningen gentager den for dig. Denne kode udskrives det samme som mens kode: fra 1 til 10:

for num in 1...10 puts num end

Hver iterator: Jeg kan virkelig godt lide hver iterator. For en række værdier gentager den en efter en og sender variablen til blokken:

[1, 2, 3, 4, 5].each do |num| puts num end

Du spørger måske, hvad forskellen er mellem hver iterator og for looping. Hovedforskellen er, at hver iterator kun opretholder variablen inde i iterationsblokken, hvorimod for looping tillader variablen at leve videre uden for blokken.

# for vs each # for looping for num in 1...5 puts num end puts num # => 5 # each iterator [1, 2, 3, 4, 5].each do |num| puts num end puts num # => undefined local variable or method `n' for main:Object (NameError)

Array: Samling / liste / datastruktur

Forestil dig, at du vil gemme heltal 1 i en variabel. Men måske vil du nu gemme 2. Og 3, 4, 5 ...

Har jeg en måde at gemme alle de heltal, jeg ønsker, men ikke i millioner af variabler? Ruby har et svar!

Array er en samling, der kan bruges til at gemme en liste over værdier (som disse heltal). Så lad os bruge det!

my_integers = [1, 2, 3, 4, 5]

Det er virkelig simpelt. Vi oprettede et array og lagrede det i my_integer .

You may be asking, “How can I get a value from this array?” Great question. Arrays have a concept called index. The first element gets the index 0 (zero). The second gets 1, and so on. You get the idea!

Using the Ruby syntax, it’s simple to understand:

my_integers = [5, 7, 1, 3, 4] print my_integers[0] # 5 print my_integers[1] # 7 print my_integers[4] # 4

Imagine you want to store strings instead of integers, like a list of your relatives’ names. Mine would be something like this:

relatives_names = [ "Toshiaki", "Juliana", "Yuji", "Bruno", "Kaio" ] print relatives_names[4] # Kaio

Works the same way as integers. Nice!

We just learned how array indices works. Now let’s add elements to the array data structure (items to the list).

The most common methods to add a new value to an array are push and <<.

Push is super simple! You just need to pass the element (The Effective Engineer) as the push parameter:

bookshelf = [] bookshelf.push("The Effective Engineer") bookshelf.push("The 4 hours work week") print bookshelf[0] # The Effective Engineer print bookshelf[1] # The 4 hours work week

The << method is just slightly different:

bookshelf = [] bookshelf << "Lean Startup" bookshelf << "Zero to One" print bookshelf[0] # Lean Startup print bookshelf[1] # Zero to One

You may ask, “But it doesn’t use the dot notation like other methods do. How could it be a method?” Nice question! Writing this:

bookshelf << "Hooked"

…is similar to writing this:

bookshelf.<<("Hooked")

Ruby is so great, huh?

Well, enough arrays. Let’s talk about another data structure.

Hash: Key-Value Data Structure/Dictionary Collection

We know that arrays are indexed with numbers. But what if we don’t want to use numbers as indices? Some data structures can use numeric, string, or other types of indices. The hash data structure is one of them.

Hash is a collection of key-value pairs. It looks like this:

hash_example = { "key1" => "value1", "key2" => "value2", "key3" => "value3" }

The key is the index pointing to the value. How do we access the hash value? Using the key!

Here’s a hash about me. My name, nickname, and nationality are the hash’s keys.

hash_tk = { "name" => "Leandro", "nickname" => "Tk", "nationality" => "Brazilian" } print "My name is #{hash_tk["name"]}" # My name is Leandro print "But you can call me #{hash_tk["nickname"]}" # But you can call me Tk print "And by the way I'm #{hash_tk["nationality"]}" # And by the way I'm Brazilian

In the above example I printed a phrase about me using all the values stored in the hash.

Another cool thing about hashes is that we can use anything as the value. I’ll add the key “age” and my real integer age (24).

hash_tk = { "name" => "Leandro", "nickname" => "Tk", "nationality" => "Brazilian", "age" => 24 } print "My name is #{hash_tk["name"]}" # My name is Leandro print "But you can call me #{hash_tk["nickname"]}" # But you can call me Tk print "And by the way I'm #{hash_tk["age"]} and #{hash_tk["nationality"]}" # And by the way I'm 24 and Brazilian 

Let’s learn how to add elements to a hash. The key pointing to a value is a big part of what hash is — and the same goes for when we want to add elements to it.

hash_tk = { "name" => "Leandro", "nickname" => "Tk", "nationality" => "Brazilian" } hash_tk["age"] = 24 print hash_tk # { "name" => "Leandro", "nickname" => "Tk", "nationality" => "Brazilian", "age" => 24 } 

We just need to assign a value to a hash key. Nothing complicated here, right?

Iteration: Looping Through Data Structures

The array iteration is very simple. Ruby developers commonly use the each iterator. Let’s do it:

bookshelf = [ "The Effective Engineer", "The 4 hours work week", "Zero to One", "Lean Startup", "Hooked" ] bookshelf.each do |book| puts book end

The each iterator works by passing array elements as parameters in the block. In the above example, we print each element.

For hash data structure, we can also use the each iterator by passing two parameters to the block: the key and the value. Here’s an example:

hash = { "some_key" => "some_value" } hash.each { |key, value| puts "#{key}: #{value}" } # some_key: some_value

We named the two parameters as key and value, but it’s not necessary. We can name them anything:

hash_tk = { "name" => "Leandro", "nickname" => "Tk", "nationality" => "Brazilian", "age" => 24 } hash_tk.each do |attribute, value| puts "#{attribute}: #{value}" end

You can see we used attribute as a parameter for the hash key and it works properly. Great!

Classes & Objects

As an object oriented programming language, Ruby uses the concepts of class and object.

“Class” is a way to define objects. In the real world there are many objects of the same type. Like vehicles, dogs, bikes. Each vehicle has similar components (wheels, doors, engine).

“Objects” have two main characteristics: data and behavior. Vehicles have data like number of wheels and number of doors. They also have behavior like accelerating and stopping.

In object oriented programming we call data “attributes” and behavior “methods.”

Data = Attributes

Behavior = Methods

Ruby Object Oriented Programming Mode: On

Let’s understand Ruby syntax for classes:

class Vehicle end

We define Vehicle with class statement and finish with end. Easy!

And objects are instances of a class. We create an instance by calling the .new method.

vehicle = Vehicle.new

Here vehicle is an object (or instance) of the class Vehicle.

Our vehicle class will have 4 attributes: Wheels, type of tank, seating capacity, and maximum velocity.

Let’s define our class Vehicle to receive data and instantiate it.

class Vehicle def initialize(number_of_wheels, type_of_tank, seating_capacity, maximum_velocity) @number_of_wheels = number_of_wheels @type_of_tank = type_of_tank @seating_capacity = seating_capacity @maximum_velocity = maximum_velocity end end

We use the initialize method. We call it a constructor method so when we create the vehicle object, we can define its attributes.

Imagine that you love the Tesla Model S and want to create this kind of object. It has 4 wheels. Its tank type is electric energy. It has space for 5 seats and a maximum velocity is 250km/hour (155 mph). Let’s create the object tesla_model_s! :)

tesla_model_s = Vehicle.new(4, 'electric', 5, 250)

4 wheels + electric tank + 5 seats + 250km/hour maximum speed = tesla_model_s.

tesla_model_s # =>

We’ve set the Tesla’s attributes. But how do we access them?

We send a message to the object asking about them. We call it a method. It’s the object’s behavior. Let’s implement it!

class Vehicle def initialize(number_of_wheels, type_of_tank, seating_capacity, maximum_velocity) @number_of_wheels = number_of_wheels @type_of_tank = type_of_tank @seating_capacity = seating_capacity @maximum_velocity = maximum_velocity end def number_of_wheels @number_of_wheels end def set_number_of_wheels=(number) @number_of_wheels = number end end

This is an implementation of two methods: number_of_wheels and set_number_of_wheels. We call it “getter” and “setter.” First we get the attribute value, and second, we set a value for the attribute.

In Ruby, we can do that without methods using attr_reader, attr_writer and attr_accessor. Let’s see it with code!

  • attr_reader: implements the getter method
class Vehicle attr_reader :number_of_wheels def initialize(number_of_wheels, type_of_tank, seating_capacity, maximum_velocity) @number_of_wheels = number_of_wheels @type_of_tank = type_of_tank @seating_capacity = seating_capacity @maximum_velocity = maximum_velocity end end tesla_model_s = Vehicle.new(4, 'electric', 5, 250) tesla_model_s.number_of_wheels # => 4
  • attr_writer: implements the setter method
class Vehicle attr_writer :number_of_wheels def initialize(number_of_wheels, type_of_tank, seating_capacity, maximum_velocity) @number_of_wheels = number_of_wheels @type_of_tank = type_of_tank @seating_capacity = seating_capacity @maximum_velocity = maximum_velocity end end # number_of_wheels equals 4 tesla_model_s = Vehicle.new(4, 'electric', 5, 250) tesla_model_s # =>  # number_of_wheels equals 3 tesla_model_s.number_of_wheels = 3 tesla_model_s # =>
  • attr_accessor: implements both methods
class Vehicle attr_accessor :number_of_wheels def initialize(number_of_wheels, type_of_tank, seating_capacity, maximum_velocity) @number_of_wheels = number_of_wheels @type_of_tank = type_of_tank @seating_capacity = seating_capacity @maximum_velocity = maximum_velocity end end # number_of_wheels equals 4 tesla_model_s = Vehicle.new(4, 'electric', 5, 250) tesla_model_s.number_of_wheels # => 4 # number_of_wheels equals 3 tesla_model_s.number_of_wheels = 3 tesla_model_s.number_of_wheels # => 3

So now we’ve learned how to get attribute values, implement the getter and setter methods, and use attr (reader, writer, and accessor).

We can also use methods to do other things — like a “make_noise” method. Let’s see it!

class Vehicle def initialize(number_of_wheels, type_of_tank, seating_capacity, maximum_velocity) @number_of_wheels = number_of_wheels @type_of_tank = type_of_tank @seating_capacity = seating_capacity @maximum_velocity = maximum_velocity end def make_noise "VRRRRUUUUM" end end

Når vi kalder denne metode, returnerer den bare en streng “VRRRRUUUUM”.

v = Vehicle.new(4, 'gasoline', 5, 180) v.make_noise # => "VRRRRUUUUM"

Indkapsling: Skjul information

Indkapsling er en måde at begrænse direkte adgang til objekters data og metoder. Samtidig letter det operationen på disse data (objekters metoder).

Indkapsling kan bruges til at skjule data medlemmer og medlemmer funktion ... Indkapsling betyder, at den interne repræsentation af et objekt generelt er skjult uden for objektets definition.

- Wikipedia

Så al intern repræsentation af et objekt er skjult udefra, kun objektet kan interagere med dets interne data.

I Ruby bruger vi metoder til direkte adgang til data. Lad os se et eksempel:

class Person def initialize(name, age) @name = name @age = age end end

Vi har lige implementeret denne Person-klasse. Og som vi har lært, bruger vi den nye metode til at oprette objektpersonen og videresende parametrene.

tk = Person.new("Leandro Tk", 24)

So I created me! :) The tk object! Passing my name and my age. But how can I access this information? My first attempt is to call the name and age methods.

tk.name > NoMethodError: undefined method `name' for #

We can’t do it! We didn’t implement the name (and the age) method.

Remember when I said “In Ruby we use methods to directly access data?” To access the tk name and age we need to implement those methods on our Person class.

class Person def initialize(name, age) @name = name @age = age end def name @name end def age @age end end

Now we can directly access this information. With encapsulation we can ensure that the object (tk in this case) is only allowed to access name and age. The internal representation of the object is hidden from the outside.

Inheritance: behaviors and characteristics

Certain objects have something in common. Behavior and characteristics.

For example, I inherited some characteristics and behaviors from my father — like his eyes and hair. And behaviors like impatience and introversion.

In object oriented programming, classes can inherit common characteristics (data) and behavior (methods) from another class.

Let’s see another example and implement it in Ruby.

Imagine a car. Number of wheels, seating capacity and maximum velocity are all attributes of a car.

class Car attr_accessor :number_of_wheels, :seating_capacity, :maximum_velocity def initialize(number_of_wheels, seating_capacity, maximum_velocity) @number_of_wheels = number_of_wheels @seating_capacity = seating_capacity @maximum_velocity = maximum_velocity end end

Our Car class implemented! :)

my_car = Car.new(4, 5, 250) my_car.number_of_wheels # 4 my_car.seating_capacity # 5 my_car.maximum_velocity # 250

Instantiated, we can use all methods created! Nice!

In Ruby, we use the < operator to show a class inherits from another. An ElectricCar class can inherit from our Car class.

class ElectricCar < Car end

Simple as that! We don’t need to implement the initialize method and any other method, because this class already has it (inherited from the Car class). Let’s prove it!

tesla_model_s = ElectricCar.new(4, 5, 250) tesla_model_s.number_of_wheels # 4 tesla_model_s.seating_capacity # 5 tesla_model_s.maximum_velocity # 250

Beautiful!

Module: A Toolbox

We can think of a module as a toolbox that contains a set of constants and methods.

An example of a Ruby module is Math. We can access the constant PI:

Math::PI # > 3.141592653589793 

And the .sqrt method:

Math.sqrt(9) # 3.0

And we can implement our own module and use it in classes. We have a RunnerAthlete class:

class RunnerAthlete def initialize(name) @name = name end end

And implement a module Skill to have the average_speed method.

module Skill def average_speed puts "My average speed is 20mph" end end

How do we add the module to our classes so it has this behavior (average_speed method)? We just include it!

class RunnerAthlete include Skill def initialize(name) @name = name end end

See the “include Skill”! And now we can use this method in our instance of RunnerAthlete class.

mohamed = RunnerAthlete.new("Mohamed Farah") mohamed.average_speed # "My average speed is 20mph"

Yay! To finish modules, we need to understand the following:

  • A module can have no instances.
  • A module can have no subclasses.
  • A module is defined by module…end.

Wrapping Up!

We learned A LOT of things here!

  • How Ruby variables work
  • How Ruby conditional statements work
  • How Ruby looping & iterators work
  • Array: Collection | List
  • Hash: Indsamling af nøgleværdi
  • Hvordan vi kan gentage gennem disse datastrukturer
  • Objekter og klasser
  • Attributter som objekters data
  • Metoder som objekters adfærd
  • Brug af Ruby getters og setters
  • Indkapsling: skjuler information
  • Arv: adfærd og egenskaber
  • Moduler: en værktøjskasse

Det er det

Tillykke! Du har gennemført dette tætte stykke indhold om Ruby! Vi lærte meget her. Håber du kunne lide det.

Hav det sjovt, fortsæt med at lære, og fortsæt altid med at kode!

Min Twitter & Github. ☺