Ruby6 细节补充

代码规范 使用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 局部变量:……

Continue reading

Ruby5 其他的奇技淫巧

变量赋值 # 变量交换 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{……

Continue reading

Ruby4 Blocks and Exceptions

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}"}……

Continue reading

Ruby3 流程控制

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……

Continue reading

Ruby1 数据类型,变量

整数类型: 3,222 小数: 3.14 字符串: hello,world 布尔类型: true(TrueClass),false(FalseClass) 数组: [1,2],["hello","hello world"] Hash(字典): {"name"=>"luo","age"=>24},{:name=>"daoyi",:age=>24} Symbol(符号): :a,:hello,:"hello world" Range: 1..10,1...10(三个点不包括10本身) 正则: /hello/i String 字符串 a = "hello" #=> hello b = "world" #=> world a.class #=> String a + " " + b #=> hello world "#{a} #{b}" #=> hello world string method "hello world".length #=>11 "hello world".capitalize #=> Hello world "hello world".gsub("world","gril").upcase #=> HELLO GIRL 变量赋值 a = "hello" # => hello a.object_id # => 70353313681980 a.replace("hello2") #……

Continue reading

Ruby2 Method and Block

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……

Continue reading

Cpp7 C++的多态实现 — 虚表

多态的实现原理 #include "stdafx.h" #include #include class A { public: int x; virtual void Test() { printf("A \n"); } protected: private: }; class B:public A { public: int x; void Test() { printf("B \n"); } protected: private: }; void Fun(A* p) { p->Test(); } int main(int argc, char* argv[]) { A a; B b; Fun(&b); return 0; } //我们发现在这里 调用的test函数 是b的 因为fun方法传入的对象是b b继承自a 这里体现了多态 //反编译 31: void Fun(A* p) 32: { 00401050 push ebp 00401051 mov ebp,esp 00401053 sub esp,40h 00401056 push ebx 00401057 push esi 00401058 push edi 00401059……

Continue reading

Cpp6 封装、继承和多态

继承 子类从父类继承成员变量 子类从父类继承成员函数 #include "stdafx.h" class Person { public: int Age; int Sex; void Word() { printf("Person:Work"); } }; class Teacher:public Person { public: int Level; }; int main() { Teacher t; t.Age = -1; //合法但是不合理 t.Sex = 2; t.Level = 3; return 0; } 实现数据隐藏 为什么要隐藏数据成员 与前面比较,赋值的时候 合理不合法,手机的电路板也没有暴露在外面啊 根本的目的是可控 不要造相同的轮子 代码……

Continue reading

kali rolling 国内源

#阿里云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……

Continue reading

Cpp5 在堆中创建对象和引用类型

我们可以在什么地方创建对象? 全局变量区 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()……

Continue reading