ruby秒入门

作者: 廖马儿 | 来源:发表于2017-01-14 18:29 被阅读138次

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.自定义符号

+
-

经验:
导入相对路劲:
require "./Vectory1" 导入的是当前目录下的类

puts 1.methods()
methods 方法可以查看一个对象的类的所有方法。
在ruby 中,一切皆对象。

to_s: 讲一个对象变为字符串。
to_i : 变为数值类型

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

调用: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

调用:

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的数值和字符串:

数值类:Numberic
Integer 和 Float Nuberic子类
Fixnum(普通整数)和 Bignum(大整数)是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.数组类和hash类:

  1. 数组 Array
    使用的是[],取的时候也是[]
    创建Array:

    numbers = [1, 2, 3, 4, 5]
    或者:
    numbers2 = Array.new() #-> 得到是[],一个空数组
    numbers2[1] = 2
    numbers2[2] = 3 # -> [nil, 2, 3]

数组的交集,并集,全集
交集: arr1 & arr2
并集:arr1 | arr2
全集:arr1 + arr2

将一个元素放到数组的末尾去:
arr.push(4)
对应的有一个pop()函数,可以弹出最后一个值:
arr.pop(arg) #注意:如果arg不写,会返回一个最后的元素,参数arg是弹出多少个。
插入:
arr.insert(1, 100):插入到第一个位置,插入的值是100
2)哈希 Hash
代表的是无序集合

hashes = {"a"=>1, "b"=>2, "c"=>3, "d"=>4}
hashes["a"] -> 1

哈希是可以和数组自由转换的:

hashes.to_a   #转换为array
结果为:
[["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/")

创建文件夹:

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
}

遍历数组:

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模块的本质和Mix-in

puts Class.superclass -> Module
puts Module.superclass   -> Object 

ruby中是没有命名空间这个说法的。

includeextend的区别

23.ruby的代码规范:

1)命名
变量
Snake case 方式:也就是下划线
hello_world
常量
HELLO_WORLD = 1
类 模块
驼峰命名法:
Student
HelloWorld

2)规范
不采用分号(;
不要在一行中写代码 def hello_world end
case 语句对齐的问题:

a = "a"
case a
when condition

when condition
end 

判断式

24.ruby的用途:
ruby的特征有:易懂,易学,函数封装简单,方法调用容易
在现在的时代,ruby的用途千奇百怪:
1)作为脚本语言十分优秀
可以使用ruby写一些脚本,去批量修改文件名,文件夹,去自动转换简繁体,去自动上传某些文件等。
2)大数据处理表现的十分优秀
在硅谷ruby十分流行于做大数据处理
比如有一个10GB的数据文件,需要在里面找到有用的信息,大家一般使用ruby去处理
3)ruby可以作为服务器开发
团800
百词斩
暴走漫画
这三个是由ruby开发的后端

twitter
Github
这些ruby在网站开发的实例

Rails 和 Sinatra 就是基于ruby的库

Rack

大数据处理,和服务器开发是ruby的两个常用的方面。

RubyMotion
是用来开发Android 和iOS 的。

相关文章

网友评论

本文标题:ruby秒入门

本文链接:https://www.haomeiwen.com/subject/ppuebttx.html