1.邏輯運算符
真:不是nil,并且不是false
假:nil 或者 false
2.條件運算符
三目運算符:
c = (a>b)? a:b
3.范圍運算符
a="Hello ruby"
b = a[0..4] ->Hello
b = a[0...4] -> Hell
x..y
: >=x <=y
x...y
: >=x <y
for i in 1..3
puts i # 1,2,3
end
4.自定義符號
+
-
經(jīng)驗:
導入相對路勁:
require "./Vectory1"
導入的是當前目錄下的類
puts 1.methods()
methods 方法可以查看一個對象的類的所有方法煎饼。
在ruby 中治宣,一切皆對象。
to_s
: 講一個對象變?yōu)樽址?br>
to_i
: 變?yōu)閿?shù)值類型
6.字符串相加會怎么樣
"hello" + " world" -> "hello world"
7.判斷是否為空
"hello".nil? -> false
"".nil? -> false #空字符串,對象是存在的
"".empty? -> true
8.case 語句,while end 語句,until語句:
case:
case xx
when condition
when condition
end
input = 99
while input!=0
puts "input is not zero"
input -= 1
end
puts "input is zero now"
util語句:
until input != 0
puts "aa"
end
9.異常處理:
begin
rescue Exception => e
end
10.導入
require
require "./Vectory1"
11.帶默認值得方法
def add(a = 1, b = 2)
a+b
end
puts add
12.類
class Student2
attr_accessor :name, :gender #可寫可讀
attr_reader :no #只可以讀瑟枫,不可寫
def initialize(name, no, gender)
@name = name
@no = no
@gender = gender
end
end
13.常量抡锈。
在ruby中約定的是,首字母大寫.不可修改拓劝,絕對唯一视卢。
類常量Version
class Student2
Version = 2
調(diào)用:Student2::Version
14.類方法
class Student
attr_accessor :name, :no, :gender
def initialize(name, no, gender)
@name = name
@no = no
@gender = gender
end
def sayHi
puts "我叫#{@name} 你好"
end
def self.nick_name #類方法
"學生類"
end
end
調(diào)用:
puts Student.nick_name # 學生類
15.擴充踱卵,擴展,拓展 類
class String
def self.nick_name
p "小s"
end
end
16.模塊(Module)
模塊不能有實例据过,沒有new方法惋砂,也不可以被繼承。
Math
就是一個模塊绳锅。
Math::PI -> 3.14.... # 常量
Math::sqrt(2) # 方法
寫一個module的例子:
module Mathematicas
PI = 3.14
def self.sqrt(number) #注意是 self.sqrt(number)
Math::sqrt(number)
end
end
17.向類中導入module 班利,模塊
require "./module'
class Student
include CustomModule
模塊混入模塊,模塊混入類榨呆,類中又有模塊等罗标。
18.ruby的數(shù)值和字符串:
數(shù)值類:Numberic
Integer 和 Float Nuberic子類
Fixnum(普通整數(shù))和 Bignum(大整數(shù))是Integer的子類
判斷一個對象是否是nil:
a.nil? -> true or false
判斷一個字符串是否為空:
a.isEmpty?
a.length == 0
注意,說的它是字符串积蜻,就說明他不是一個nil哈闯割。
a = "abc"
b = "abc"
a == b -> true
a.include?(b) ->true
19.數(shù)組類和hash類:
-
數(shù)組 Array
使用的是[]
,取的時候也是[]
創(chuàng)建Array:numbers = [1, 2, 3, 4, 5]
或者:
numbers2 = Array.new() #-> 得到是[],一個空數(shù)組
numbers2[1] = 2
numbers2[2] = 3 # -> [nil, 2, 3]
數(shù)組的交集,并集竿拆,全集
交集: arr1 & arr2
并集:arr1 | arr2
全集:arr1 + arr2
將一個元素放到數(shù)組的末尾去:
arr.push(4)
對應的有一個pop()
函數(shù)宙拉,可以彈出最后一個值:
arr.pop(arg)
#注意:如果arg不寫,會返回一個最后的元素,參數(shù)arg是彈出多少個丙笋。
插入:
arr.insert(1, 100)
:插入到第一個位置谢澈,插入的值是100
2)哈希 Hash
代表的是無序集合
hashes = {"a"=>1, "b"=>2, "c"=>3, "d"=>4}
hashes["a"] -> 1
哈希是可以和數(shù)組自由轉(zhuǎn)換的:
hashes.to_a #轉(zhuǎn)換為array
結(jié)果為:
[["a", 1], ["b", 2]]
20.文件類,文件夾類御板,時間類:
1)文件類锥忿,文件夾類:
重命名:
File.rename("class.rb", "c.rb") #會返回一個值
復制文件:
FileUtils.cp("c.rb", "c_1.rb")
刪除文件:
File.delete("c_1.rb") #返回1,那么這個文件就被刪除了.注意怠肋,在回收站中是不可以找到的敬鬓。
打開文件:
dir=Dir.open("../Desktop/")
創(chuàng)建文件夾:
Dir.mkdir("temp")
Dir.delete("temp")
2)時間類
Time.new
# 2017-01-14 21:35:58 +0800 (+0800 就是北京時間)
t = Time.new
puts t.zone # CST *CST*可視為美國,澳大利亞,古巴或中國的標準時間
puts Time.now.to_s # 2017-01-14 17:41:32 +0800
自定義時間格式:
puts Time.new.strftime("%Y/%m/%d %H:%M:%S")
# 2017/01/14 17:43:42
date 類
require "date"
puts Date.today + 3
date類進行時間加減操作可以更加方便些钉答,Time類進行時間的加減會比較麻煩础芍,是用時間戳timestamp來進行操作的。
可以去了解一個DateTime 類
21.迭代器
1))什么是迭代器数尿?
5.times {
p "helow" #打印5次
}
5.times { |n|
puts n #會打印 1, 2, 3, 4, 5
}
遍歷數(shù)組:
arr = [1, 2, 3, 4, 5]
arr.each { |i|
p i
}
遍歷哈希Hash:
hashes = {"a"=>1, "b"=>2, "c"=>3, "d"=>4}
hashes.each { |key, value|
puts "key=>#{key}, value=>#{value}"
}
2)怎么使用仑性?
3)如何自定義?
22.Module模塊的本質(zhì)和Mix-in
puts Class.superclass -> Module
puts Module.superclass -> Object
ruby中是沒有命名空間這個說法的右蹦。
include
和extend
的區(qū)別
23.ruby的代碼規(guī)范:
1)命名
變量
Snake case 方式:也就是下劃線
hello_world
常量
HELLO_WORLD = 1
類 模塊
駝峰命名法:
Student
HelloWorld
2)規(guī)范
不采用分號(;
)
不要在一行中寫代碼 def hello_world end
case 語句對齊的問題:
a = "a"
case a
when condition
when condition
end
判斷式
24.ruby的用途:
ruby的特征有:易懂虏缸,易學,函數(shù)封裝簡單嫩实,方法調(diào)用容易
在現(xiàn)在的時代,ruby的用途千奇百怪:
1)作為腳本語言十分優(yōu)秀
可以使用ruby寫一些腳本窥岩,去批量修改文件名甲献,文件夾,去自動轉(zhuǎn)換簡繁體颂翼,去自動上傳某些文件等晃洒。
2)大數(shù)據(jù)處理表現(xiàn)的十分優(yōu)秀
在硅谷ruby十分流行于做大數(shù)據(jù)處理
比如有一個10GB的數(shù)據(jù)文件,需要在里面找到有用的信息朦乏,大家一般使用ruby去處理
3)ruby可以作為服務器開發(fā)
團800
百詞斬
暴走漫畫
這三個是由ruby開發(fā)的后端
twitter
Github
這些ruby在網(wǎng)站開發(fā)的實例
Rails 和 Sinatra 就是基于ruby的庫
Rack
大數(shù)據(jù)處理球及,和服務器開發(fā)是ruby的兩個常用的方面。
RubyMotion
是用來開發(fā)Android 和iOS 的呻疹。