零基础学习ruby_学习Ruby:从零到英雄
零基礎學習ruby
“Ruby is simple in appearance, but is very complex inside, just like our human body.” — Matz, creator of the Ruby programming language
“ Ruby的外觀很簡單,但是內部卻非常復雜,就像我們的人體一樣。” — Matz ,Ruby編程語言的創建者
Why learn Ruby?
為什么要學習Ruby?
For me, the first reason is that it’s a beautiful language. It’s natural to code and it always expresses my thoughts.
對我來說,第一個原因是這是一門優美的語言。 編寫代碼很自然,并且總是表達我的想法。
The second — and main — reason is Rails: the same framework that Twitter, Basecamp, Airbnb, Github, and so many companies use.
第二個也是主要的原因是Rails :Twitter,Basecamp,Airbnb,Github和許多公司使用的框架相同。
簡介/歷史 (Introduction/History)
Ruby is “A dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.” — ruby-lang.org
Ruby是一種動態的,開放源代碼的編程語言,其重點是簡單性和生產率。 它具有優雅的語法,易于閱讀且易于編寫。” — ruby-lang.org
Let’s get started with some basics!
讓我們開始一些基礎知識!
變數 (Variables)
You can think about a variable as a word that stores a value. Simple as that.
您可以將變量視為存儲值的單詞。 就那么簡單。
In Ruby it’s easy to define a variable and set a value to it. Imagine you want to store the number 1 in a variable called one. Let’s do it!
在Ruby中,很容易定義變量并為其設置值。 假設您想將數字1存儲在名為one的變量中。 我們開始做吧!
one = 1How simple was that? You just assigned the value 1 to a variable called one.
那有多簡單? 您剛剛將值1分配給了名為1的變量。
two = 2 some_number = 10000You can assign a value to whatever variable you want. In the example above, a two variable stores an integer of 2 and some_number stores 10,000.
您可以將值分配給所需的任何變量。 在上面的示例中, 兩個變量存儲2的整數, some_number存儲10,000。
Besides integers, we can also use booleans (true/false), strings, symbols, float, and other data types.
除了整數外,我們還可以使用布爾值(true / false),字符串, symbols ,float和其他數據類型。
# booleans true_boolean = true false_boolean = false# string my_name = "Leandro Tk"# symbol a_symbol = :my_symbol# float book_price = 15.80條件語句:控制流 (Conditional Statements: Control Flow)
Conditional statements evaluate true or false. If something is true, it executes what’s inside the statement. For example:
條件語句評估是非。 如果為真,則執行語句中的內容。 例如:
if trueputs "Hello Ruby If" endif 2 > 1puts "2 is greater than 1" end2 is greater than 1, so the puts code is executed.
2大于1,因此將執行puts代碼。
This else statement will be executed when the if expression is false:
當if表達式為false時,將執行else語句:
if 1 > 2puts "1 is greater than 2" elseputs "1 is not greater than 2" end1 is not greater than 2, so the code inside the else statement will be executed.
1不大于2,因此將執行else語句中的代碼。
There’s also the elsif statement. You can use it like this:
還有elsif語句。 您可以像這樣使用它:
if 1 > 2puts "1 is greater than 2" elsif 2 > 1puts "1 is not greater than 2" elseputs "1 is equal to 2" endOne way I really like to write Ruby is to use an if statement after the code to be executed:
我真的很喜歡編寫Ruby的一種方法是在要執行的代碼后使用if語句:
def hey_ho?true endputs "let’s go" if hey_ho?It is so beautiful and natural. It is idiomatic Ruby.
它是如此美麗和自然。 它是慣用的Ruby。
循環/迭代器 (Looping/Iterator)
In Ruby we can iterate in so many different forms. I’ll talk about three iterators: while, for and each.
在Ruby中,我們可以以許多不同的形式進行迭代。 我將討論三個迭代器:while,for和each。
While looping: As long as the statement is true, the code inside the block will be executed. So this code will print the number from 1 to 10:
循環時:只要該語句為true,就將執行塊中的代碼。 因此,此代碼將打印從1到10的數字:
num = 1while num <= 10puts numnum += 1 endFor looping: You pass the variable num to the block and the for statement will iterate it for you. This code will print the same as while code: from 1 to 10:
For循環:將變量num傳遞給塊,for語句將為您迭代。 此代碼的打印方式與while代碼相同:從1到10:
for num in 1...10puts num endEach iterator: I really like the each iterator. For an array of values, it’ll iterate one by one, passing the variable to the block:
每個迭代器:我真的很喜歡每個迭代器。 對于值數組,它將一步一步地迭代,并將變量傳遞給塊:
[1, 2, 3, 4, 5].each do |num|puts num endYou may be asking what the difference is between the each iterator and for looping. The main difference is that the each iterator only maintains the variable inside the iteration block, whereas for looping allows the variable to live on outside the block.
您可能會問,每個迭代器和循環之間有什么區別。 主要區別在于,每個迭代器僅將變量保留在迭代塊內部,而對于循環,則允許變量駐留在塊外部。
# for vs each# for looping for num in 1...5puts num endputs num # => 5# each iterator [1, 2, 3, 4, 5].each do |num|puts num endputs num # => undefined local variable or method `n' for main:Object (NameError)數組:集合/列表/數據結構 (Array: Collection/List/Data Structure)
Imagine you want to store the integer 1 in a variable. But maybe now you want to store 2. And 3, 4, 5 …
假設您要將整數1存儲在變量中。 但也許現在您想存儲2和3、4、5…
Do I have a way to store all the integers that I want, but not in millions of variables? Ruby has an answer!
我是否可以存儲所需的所有整數,但不能存儲數百萬個變量? Ruby有一個答案!
Array is a collection that can be used to store a list of values (like these integers). So let’s use it!
數組是一個集合,可用于存儲值列表(如這些整數)。 因此,讓我們使用它!
my_integers = [1, 2, 3, 4, 5]It is really simple. We created an array and stored it in my_integer.
真的很簡單。 我們創建了一個數組并將其存儲在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!
您可能會問:“如何從該數組中獲取值?” 好問題。 數組有一個稱為索引的概念。 第一個元素的索引為0(零)。 第二個為1,依此類推。 你明白了!
Using the Ruby syntax, it’s simple to understand:
使用Ruby語法,很容易理解:
my_integers = [5, 7, 1, 3, 4] print my_integers[0] # 5 print my_integers[1] # 7 print my_integers[4] # 4Imagine 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] # KaioWorks 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和<<。
Push is super simple! You just need to pass the element (The Effective Engineer) as the push parameter:
推送超級簡單! 您只需要傳遞元素(有效工程師)作為push參數:
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 weekThe << method is just slightly different:
<<方法略有不同:
bookshelf = [] bookshelf << "Lean Startup" bookshelf << "Zero to One" print bookshelf[0] # Lean Startup print bookshelf[1] # Zero to OneYou 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 BrazilianIn 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).
關于哈希的另一個很酷的事情是,我們可以使用任何東西作為值。 我將添加密鑰“ 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 BrazilianLet’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:
數組迭代非常簡單。 Ruby開發人員通常使用each迭代器。 我們開始做吧:
bookshelf = ["The Effective Engineer","The 4 hours work week","Zero to One","Lean Startup","Hooked" ]bookshelf.each do |book|puts book endThe 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_valueWe 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}" endYou 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.
作為一種面向對象的編程語言,Ruby使用類和對象的概念。
“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面向對象的編程模式:開 (Ruby Object Oriented Programming Mode: On)
Let’s understand Ruby syntax for classes:
讓我們了解類的Ruby語法:
class Vehicle endWe define Vehicle with class statement and finish with end. Easy!
我們用class語句定義Vehicle并以end結尾。 簡單!
And objects are instances of a class. We create an instance by calling the .new method.
對象是類的實例。 我們通過調用.new方法來創建實例。
vehicle = Vehicle.newHere vehicle is an object (or instance) of the class Vehicle.
這里的Vehicle是Vehicle類的對象(或實例)。
Our vehicle class will have 4 attributes: Wheels, type of tank, seating capacity, and maximum velocity.
我們的車輛類別將具有4個屬性:車輪,油箱類型,座位容量和最大速度。
Let’s define our class Vehicle to receive data and instantiate it.
讓我們定義我們的Vehicle類來接收數據并實例化它。
class Vehicledef 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_velocityend endWe use the initialize method. We call it a constructor method so when we create the vehicle object, we can define its attributes.
我們使用initialize方法。 我們將其稱為構造函數方法,以便在創建車輛對象時可以定義其屬性。
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! :)
想象一下,您喜歡特斯拉Model S,并且想要創建這種對象。 它有4個輪子。 它的儲罐類型是電能。 它可以容納5個座位,最大速度為250公里/小時(155英里/小時)。 讓我們創建對象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.
4輪+電動油箱+ 5個座位+ 250km /小時的最高速度= tesla_model_s。
tesla_model_s # => <Vehicle:0x0055d516903a08 @number_of_wheels=4, @type_of_tank="electric", @seating_capacity=5, @maximum_velocity=250>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 Vehicledef 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_velocityenddef number_of_wheels@number_of_wheelsenddef set_number_of_wheels=(number)@number_of_wheels = numberend endThis 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.
這是兩個方法的實現:number_of_wheels和set_number_of_wheels。 我們稱其為“ getter”和“ setter”。 首先,我們獲得屬性值,其次,為屬性設置一個值。
In Ruby, we can do that without methods using attr_reader, attr_writer and attr_accessor. Let’s see it with code!
在Ruby中,無需使用attr_reader,attr_writer和attr_accessor的方法就可以做到這一點。 我們來看一下代碼吧!
- attr_reader: implements the getter method attr_reader:實現getter方法
- attr_writer: implements the setter method attr_writer:實現setter方法
- attr_accessor: implements both methods attr_accessor:實現兩種方法
So now we’ve learned how to get attribute values, implement the getter and setter methods, and use attr (reader, writer, and accessor).
因此,現在我們已經學習了如何獲取屬性值,實現getter和setter方法以及如何使用attr(讀取器,寫入器和訪問器)。
We can also use methods to do other things — like a “make_noise” method. Let’s see it!
我們還可以使用方法來做其他事情-例如“ make_noise”方法。 讓我們來看看它!
class Vehicledef 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_velocityenddef make_noise"VRRRRUUUUM"end endWhen we call this method, it just returns a string “VRRRRUUUUM”.
當我們調用此方法時,它僅返回字符串“ VRRRRUUUUM”。
v = Vehicle.new(4, 'gasoline', 5, 180) v.make_noise # => "VRRRRUUUUM"封裝:隱藏信息 (Encapsulation: Hiding Information)
Encapsulation is a way to restrict direct access to objects’ data and methods. At the same time it facilitates operation on that data (objects’ methods).
封裝是一種限制直接訪問對象的數據和方法的方法。 同時,它便于對該數據進行操作(對象的方法)。
Encapsulation can be used to hide data members and members function…Encapsulation means that the internal representation of an object is generally hidden from view outside of the object’s definition.
封裝可用于隱藏數據成員和成員函數……封裝意味著對象的內部表示形式通常在對象定義之外的視圖中隱藏。
— Wikipedia
— 維基百科
So all internal representation of an object is hidden from the outside, only the object can interact with its internal data.
因此,對象的所有內部表示都從外部隱藏,只有對象可以與其內部數據進行交互。
In Ruby we use methods to directly access data. Let’s see an example:
在Ruby中,我們使用方法直接訪問數據。 讓我們來看一個例子:
class Persondef initialize(name, age)@name = name@age = ageend endWe just implemented this Person class. And as we’ve learned, to create the object person, we use the new method and pass the parameters.
我們剛剛實現了此Person類。 正如我們所了解的,要創建對象人,我們將使用新方法并傳遞參數。
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和age方法。
tk.name > NoMethodError: undefined method `name' for #<Person:0x0055a750f4c520 @name="Leandro Tk", @age=24>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.
還記得我說過“在Ruby中,我們使用方法直接訪問數據嗎?” 要訪問tk名稱和年齡,我們需要在Person類上實現這些方法。
class Persondef initialize(name, age)@name = name@age = ageenddef name@nameenddef age@ageend endNow 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.
現在我們可以直接訪問此信息。 通過封裝,我們可以確保僅允許對象(在這種情況下為tk)訪問名稱和年齡。 對象的內部表示從外部隱藏。
繼承:行為和特征 (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.
讓我們來看另一個示例,并在Ruby中實現它。
Imagine a car. Number of wheels, seating capacity and maximum velocity are all attributes of a car.
想像一輛汽車。 車輪數量,座位容量和最大速度都是汽車的屬性。
class Carattr_accessor :number_of_wheels, :seating_capacity, :maximum_velocitydef initialize(number_of_wheels, seating_capacity, maximum_velocity)@number_of_wheels = number_of_wheels@seating_capacity = seating_capacity@maximum_velocity = maximum_velocityend endOur 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 # 250Instantiated, 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.
在Ruby中,我們使用<運算符顯示一個從另一個繼承的類。 ElectricCar類可以從我們的Car類繼承。
class ElectricCar < Car endSimple 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!
就那么簡單! 我們不需要實現initialize方法和任何其他方法,因為此類已經擁有了(從Car類繼承)。 讓我們證明一下!
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 # 250Beautiful!
美麗!
模塊:工具箱 (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:
Ruby模塊的一個示例是Math。 我們可以訪問常量PI:
Math::PI # > 3.141592653589793And the .sqrt method:
和.sqrt方法:
Math.sqrt(9) # 3.0And we can implement our own module and use it in classes. We have a RunnerAthlete class:
我們可以實現自己的模塊,并在類中使用它。 我們有一個RunnerAthlete類:
class RunnerAthletedef initialize(name)@name = nameend endAnd implement a module Skill to have the average_speed method.
并實現模塊Skill具有average_speed方法。
module Skilldef average_speedputs "My average speed is 20mph"end endHow do we add the module to our classes so it has this behavior (average_speed method)? We just include it!
我們如何將模塊添加到類中,使其具有此行為(average_speed方法)? 我們只包含它!
class RunnerAthleteinclude Skilldef initialize(name)@name = nameend endSee the “include Skill”! And now we can use this method in our instance of RunnerAthlete class.
請參閱“包含技能”! 現在,我們可以在RunnerAthlete類的實例中使用此方法。
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 Ruby變量如何工作
- How Ruby conditional statements work Ruby條件語句如何工作
- How Ruby looping & iterators work Ruby循環和迭代器如何工作
- Array: Collection | List 數組:集合| 清單
- Hash: Key-Value Collection 哈希:鍵值集合
- How we can iterate through this data structures 我們如何遍歷此數據結構
- Objects & Classes 對象和類
- Attributes as objects’ data 屬性作為對象的數據
- Methods as objects’ behavior 方法作為對象的行為
- Using Ruby getters and setters 使用Ruby的getter和setter
- Encapsulation: hiding information 封裝:隱藏信息
- Inheritance: behaviors and characteristics 繼承:行為和特征
- Modules: a toolbox 模塊:工具箱
而已 (That’s it)
Congrats! You completed this dense piece of content about Ruby! We learned a lot here. Hope you liked it.
恭喜! 您已經完成了有關Ruby的大量內容! 我們在這里學到了很多東西。 希望你喜歡。
Have fun, keep learning, and always keep coding!
玩得開心,繼續學習,并始終保持編碼!
My Twitter & Github. ?
我的Twitter和Github 。 ?
翻譯自: https://www.freecodecamp.org/news/learning-ruby-from-zero-to-hero-90ad4eecc82d/
零基礎學習ruby
總結
以上是生活随笔為你收集整理的零基础学习ruby_学习Ruby:从零到英雄的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux 机器格式化_为什么机器人应该
- 下一篇: 梦到老公给我洗头发是什么意思