ruby基础

ruby学习基础

inputthing = gets.chomp.to_i

if语句

if inputthing == 1
    puts inputthing
    system "say ruby我的最爱"
end

循环语句

while inputthing == 5
    puts "inputthing"
    inputthing++
end

until inputthing == 5
    puts "inputthing"
    inputthing++
end

case语句 (switch)
case inputthing
when inputthing==5
    puts inputthing
end

循环异常抛出

a = 100
while true
    b = gets.to_i
    begin
        inputs = a/b
        puts inputs
    rescue Exception => e
        puts "error"
    end
end

function 类函数/类方法 带默认值的运算

def add(a = 3,b = 2)
    return a+b
end

ruby类的学习 类似于OC类 EZ
类存在自己的构造方法,生成的实例也会集成类的所有属性和函数

class Student
    #初始化方法
    def initialize(name, no, gender)
        @name = name
        @no = no
        @gender = gender
    end
    
    def sayHi
        puts "我叫#{@name}, 学号#{@no}"
    end
end

类常量 类变量 类方法的访问控制 public(可不写),private (类似于OC optional,require)
class Student
#变量 创建实例变量
attr_accessor :name #可读可写
# attr_reader:no #readonly
# attr_writer:gender #writeonly
attr_accessor :no
attr_accessor :gender

#常量   通常常量的首字母大写   常量不可修改  属于类的常量 实例同时拥有 调用时类名加 ::
    Version = "1.0"


    def initialize(name,no,gender)
        @name = name
        @no = no
        @gender = gender
    end

setter getter方法的生成类似于OC 写法不同而已 并且都可以更优雅实现

    # #setter  
    # def name=(name)
    #   @name = name
    # end
    # #getter
    # def name
    #   return @name
    # end

    private  #public  不写就相当于public
    def sayHi
        puts "我叫#{@name}"
    end
end

类方法,实例方法,类的继承

class Student
    attr_accessor :name
    attr_accessor :no
    attr_accessor :gender
    #初始化方法
    def initialize(name, no, gender)
        @name = name
        @no = no
        @gender = gender
    end
    
    def say_Hello  #实例方法
        puts "我叫#{@name}, 学号#{@no}"
    end

    def self.nickname  #类方法 加self.
        return "学生类"
    end
end

类的继承 可以重写函数 .. (除了书写不一样之外 使用 < ,思路和实现和OC 一毛一样)

class UniversityStudent 

类的扩展 extension ....和OC概念一毛一样 EZ...orz

class Student
    attr_accessor :name
    attr_accessor :no
    attr_accessor :gender
    #初始化方法
    def initialize(name, no, gender)
        @name = name
        @no = no
        @gender = gender
    end
    
    def say_Hello  #实例方法
        p "我叫#{@name}, 学号#{@no}"
    end
end

class Student   #重写类扩充实例方法
    def say_wulala
        p "wulala"
    end
end

class String #重新类添加方法 可以扩充和重写类方法 EZ...orz

    def self.nickname
        p "扩充的类方法"
    end

    def self.name
        p "String类changeName"
    end
end

ruby 模块 module !!!! 不清晰 需要重新看
没有new方法
可以 :: 调用属性
可以 . 调用方法

定义简单模块 module

module Mathematics
    PI = 3.1415

    def self.sqrt(number)  #module无法实例化   所以方法是类方法 self.
        Math.sqrt(number)
    end

    #module调用实例方法也是可以的
    def hello
        p "hello"
    end
    #模块中可以存在有意义使用的方法和常量 #警告
end

class Student
    include Mathematics     #调用module中实例方法也是可以的
    def initialize(no)
        @no = no
    end 
end

ruby运算符 可以自定义运算符 屌的一批 ...orz

1,逻辑运算符 EZ 或且非 && || !
2,条件运算符 EZ c=(a>b)?a:b (和OC...一毛一样)
3,范围运算符 EZ .. ...

4,自定义运算符 相当于创建一个类的实例方法

class Vector
    attr_accessor :x, :y
    def initialize(x, y)
        @x = x
        @y = y
    end
    
    def +(the_vector)
        Vector.new(@x+the_vector.x, @y+the_vector.y)
    end
    
end

ruby数值类和字符串
1.数值类 Numeric
Integer Float
Fixnum Bignum

round 四舍五入计算
ceil 进一位
floor 退一位

Math模型 PI sqrt()开方

创建字符串

a = "2"
a = String.new("2")
a = "#{a}" #字符串插入数据
a = %Q{啦啦啦}
a = %q|啦啦啦|

需要熟练的使用字符串的methods

数组类Array
数组基本使用是和OC相同 []

数组methods操作 需要熟练掌握

哈希类Hash 为无序的集合
hashes = {"a"=>"b"} #键值对原理 值可为大部分类型数据
hashes["a"]

hash methods熟悉和掌握

ruby File FileUtils 文件类,

文件夹 Dir ****不熟!!!

Time 时间类 单位是秒
Time.new Time.now
自定义时间格式 Time.new.strftime("%Y/%m/%d %H:%M:%S")

Date 使用需要require "date" 单位是天

DateTime类 单位是秒

ruby 迭代器 !!! 是ruby自有的特殊的循环方式
数字.times 数组.each hash.each

5.times {|n|
    p n
}

.sort 排序深入学习需要 ****不熟!!!

自定义迭代器

class Book
    attr_accessor :title, :author
    def initialize(title,author)
        @title = title
        @author = author
    end
end

class BookList
    def initialize()
        @book_list = array.new
    end
    
    def add(book)
        @book_list.push(book)
    end

    def delete(book)
        @book_list.delete(book)
    end

    def length
        @book_list.length
    end

    def [](n)
        @book_list[n]
    end

    def []=(n,book)
        @book_list[n] = book
    end

    def eachTitle
        @book_list.each { |book|
            yield(book.title)
        }
    end

end

Mix-in include extend
m模块的模块方法 始终无法被混入 Mix-in
include 调用模块方法为类的实例方法
extend 调用模块方法为类方法

你可能感兴趣的:(ruby基础)