代码规范 使用UTF-8编码 使用空格缩进,不使用tab, 1 tab = 2 spaces 不需要使用分号(;)和反斜杠()连接代码 Demo # basic types a = 1 b = "hello world" # one line c = ["pear", "cat", "dog"] # or multiple lines c2 = [ "pear", "cat", "dog" ] d = { name:"world", age:18 } d2 = { name:"world", age:18 } # source layout, conversion # with or without() def hello(name, age = 18) puts "hello #{name}, and age is #{age}" end def hello name, age = 18 puts "hello #{name}, and age is #{age}" end 变量类型 local variables 局部变量:……
变量赋值 # 变量交换 a = 1 b = 2 b,a = a,b puts a puts b puts "-" * 30 x = [1, 2, 3] a, b = x #默认会把数组中的值依次赋值给 a ,b puts a puts b puts "-" * 30 x = [1, 2, 3] a, *b = x #这里a会接受第一个元素 b用了*号 表示接受剩下所有的元素 puts a p b #output 2 1 ------------------------------ 1 2 ------------------------------ 1 [2, 3] Fixnum & Float # number puts 1 / 10 puts 1 / 10.0 puts "-" * 30 #output 0.1 ------------------------------ String # string a = "world" b = %Q{……
Block 代码块 Block是一个参数 匿名参数 Callback 使用do/en或者{}来定义 {puts "hello"} Demo: # block usage def hello puts "hello method start" yield yield puts "hello method end" end hello {puts "i am in block"} #output hello method start i am in block i am in block hello method end # yield with parameter def hello puts "hello method start" yield("hello","world") puts "hello method end" end hello {|x,y| puts "i am in block,#{x} #{y}"} #output hello method start i am in block,hello world hello method end # yield with paramter def hello name puts "hello method start" result = "hello " + name yield(result) puts "hello method end" end hello("world"){|x| puts "i am in block,i got #{x}"}……
if/else/elsif a = "hello" b = false if a p a elsif b p b else p "ok" end unless unless相当于if的反向断言 unless false "ok" end # => "ok" ## if/unless a = 1 if a != 1 #如果a不是1 则a复制为1 b = 2 unless defined?(b) #如果b未定义 那么就定义b赋值为2 ## case case a when 1 1 when /hello/ "hello world" when Array [] else "ok" end ## while a = 0 while a < 100 do p a a += 1 end ruby 没有++和–操作符 Iterators for x in [1, 2, 3] do p x……
Method(Function) def hi(name) p "hi " + name end hi("666") # => "hi 666" hi "code" #括号省略 => "hi code" def hello name p "hello #{name}" end hello "world" # => "hello world" Method 参数 def hi name="code" p "hi " +name end hi # => "hi code" def hi name,age=32 p "#{name} is #{age}" end hi "code" # => "code is 32" hi "code", 54 # => "code is 54" Method 返回值 凡有方法都有返回值,方法体最后一行代码的返回值默认会做为方法的返回值,也可以显式的使用return关键字 def hi p "ok" end hi # =>nil……
#阿里云kali源 deb http://mirrors.aliyun.com/kali kali-rolling main non-free contrib deb-src http://mirrors.aliyun.com/kali kali-rolling main non-free contrib deb http://mirrors.aliyun.com/kali-security kali-rolling/updates main contrib non-free deb-src http://mirrors.aliyun.com/kali-security kali-rolling/updates main contrib non-free #中科大kali源 deb http://mirrors.ustc.edu.cn/kali kali-rolling main non-free contrib deb-src http://mirrors.ustc.edu.cn/kali kali-rolling main non-free contrib deb http://mirrors.ustc.edu.cn/kali-security kali-current/updates main contrib non-free deb-src http://mirrors.ustc.edu.cn/kali-security kali-current/updates main contrib non-free……
我们可以在什么地方创建对象? 全局变量区 Person p; 栈 void Max() { Person p; } 堆 new 和 delete //在堆中创建对象: Person* p = new Person(); //释放对象占用的内存 delete p; 在堆中创建对象: new``delete 在C语言中我们使用malloc申请堆空间 使用完毕后使用free释放空间 C++: class Person { private: int x; int y; public: Person() { printf("Person()……