Upgrade to Pro — share decks privately, control downloads, hide ads and more …

我想学 Ruby 系列课程之开篇

icyleaf
June 29, 2015

我想学 Ruby 系列课程之开篇

为什么学习 Ruby,赞同理由很多,反驳理由也很多。那我就着这里给大家分享下为什么要学习它,到底它有哪些魅力

icyleaf

June 29, 2015
Tweet

More Decks by icyleaf

Other Decks in Technology

Transcript

  1. def what_makes ruby = :special features = ['真正的⾯面向对象'] features <<

    '脚本语⾔言' features << '开放源代码' # 使⽤用特性 features << '⾯面向开发者设计的语⾔言' features << '丰富的语法糖' features << '实际问题的快速解决⽅方案' # 技术特性 features << "Mixins" features << "消息传递" features << "闭包、函数式编程" features << "庞⼤大的标准库" features << "Rubygems 管理包⼯工具" features << "⾃自动垃圾回收机制" features << "动态类型和鸭⼦子类型" features << "操作重载" features << "⽀支持灵活强⼤大的 DSL 语法定义" end
  2. 可是有⼈人告诉我 • Ruby 性能不⾏行,没有 C、C++ ⾼高 • Ruby 需要写 begin

    … end 渣渣语法体 • 有的⼈人可能还说 Ruby 已死 • ⼩小⽇日本写的,坚决抵制 • …
  3. Ruby Java Obj-C String String NSString Fixnum Bignum int, long,

    Integer, Long int, long NSNumber Float Float, Double float NSNumber Array Array NSArray Hash HashMap NSDictionary TrueClass FalseClass Boolean bool NSNumber NilClass null nil Symbol - -
  4. Java int[] intArray = new int[1]; intArray[0] = 1; double[]

    doubleArray = {1.4, 4.4, 5.1}; ArrayList list = new ArrayList(); list.add("1"); list.add("2");
  5. Objective-C NSArray *array1 = [[NSArray alloc] initWithObjects:@3.1, nil]; NSArray *array2

    = [NSArray arrayWithObjects:@1, @2.3, @"name", nil]; NSArray *array3 = @[@1, @2.5, @"name"];
  6. Ruby array = [1, 2.4, true, [1, 2, 3, 4]]

    array[0] # => 1 array[2] # => true array[3] # => [1, 2, 3, 4]
  7. Java HashMap<String , Double> map = new HashMap<String , Double>();

    map.put("Apple" , 6.5); map.put("Banana" , 12.0); map.put("Orange" , 8.5);
  8. Objective-C NSDictionary *dict1 = [[NSDictionary alloc] initWithObjectsAndKeys: @6.5, @"Apple", @12,

    @"Banana", @8.5, @"Orange", nil]; NSDictionary *dict2 = @{ @"Apple": @6.5, @"Banana": @12, @"Orange": @8.5 };
  9. Ruby hash1 = { :apple => 6.5, :banana => 12,

    :orange => 8.5 } # Ruby 1.9+ hash2 = { apple: { type1: 6.5, type2: 3 }, banana: 12, orange: 8.5, }
  10. 变量的约定 • 定义弱类型(⽆无需预定义) • 不能以数字开头 • 命名约定 • name -

    局部变量 • Name - 常量 • @name - 实例变量 • @@name - 类的成员变量 • $name - 全局变量字
  11. firstname = 'wang' lastname = 'shen' realname = firstname +

    " " + lastname same_realname = "#{firstname} #{lastname}" # => wang shen not_realname = '#{firstname} #{lastname}' # => \#{firstname} \#{lastname} # 输出 # Java:System.out.println # Obj-C: printf / NSLog puts realname # => “wang shen" p realname # => “wang shen"
  12. Java Obj-C for(int i; i < {length}; i++){…} for (int

    i : integers){…} while(true){…} do{…}while(true) for(int i; i < {length}; i++){…} for (int i : integers){…} while(true){…} do{…}while(true) 居然⼀一样?!
  13. Ruby [1, 2, 3, 4, 6 - 1].each do |i|

    puts i end # same above, all the block [1, 2, 3, 4, 6 - 1].each {|i| puts i} {apple:(6.5 * 4), banana:12, orange:8.5}.each do |name, price| puts "#{name} price is #{price.to_s}" end
  14. Java Ruby Obj-C if (condition){} else if (condition){} else{} if

    condition … elsif condition … else … end if (condition){} else if (condition){} else{} ⼤大同⼩小异
  15. if age.empty? || age.nil? puts "name is empty!" elsif age.length

    <= 0 puts "age must bigger than 0" else puts “my age is #{age}" end
  16. Java Ruby Obj-C switch (expression) { case n: statements; break;

    } case expression when n statements when n statements end switch (expression){ case n: statements; break; } Java SE 7+ support String
  17. case http_status when 200 puts "OK" when 400..600 puts "System

    Error" when String puts "What the fxxk ?!" else puts "Unkown Error" end
  18. Java Obj-C public String function(String x, int y) {…} public

    void static function(String x, int y) {…} - (NSString) function (int x, NSString y) {…} + (void) function (int x, NSString y) {…} 依托类 不能独⽴立存在
  19. def add_role(usename, role) user = User.find_by(username: username) user.role.create(name: role)! end

    add_role("icyleaf", "admin") # same as above add_role "icyleaf", "admin"
  20. Java Obj-C public class User { String name; int age;

    public User(String name){…} } # user.h @interface User: NSObject @property NSString *name; @property int age; - (id) initWithName:(NSString *name); @end # user.m @implementation - (id) initWithName:(NSString *name) {…} @end
  21. class User def initialize(name) @name = name end def name=(name)

    @name = name end def name @name end end user = User.new('icyleaf')
  22. class User attr_accessor :name def initialize(name) @name = name end

    end user = User.new("icyleaf") puts user.name user.name = "三⽕火"
  23. module Qyer module Mobile class User attr_accessor @name def initialize(name)

    @name = name end def say_hello puts "hello, mobile guy! #{name}" end end end end user = Qyer::Mobile::User.new(‘icyleaf’) user.say_hello module Qyer module Web class User attr_accessor @name def initialize(name) @name = name end def hello puts "hello, web guy! #{name}" end end end end user = Qyer::Web::User.new(‘master huang') user.say_hello
  24. 课程回顾 • ruby 的简介 • ruby 的应⽤用 • ruby 的市场供需关系

    • ruby 的基本类型 • ruby 的变量 • ruby 的流程控制 • ruby 的条件判断 • ruby 的⽅方法、类和模型