Ruby快速入门 | LiJun's Blog
当我有了一定的编程经验后,熟悉了一门语言,然后再去学习另一门语言,会发现要找教程都比较麻烦,因为基本上所有语言的基础教程,都会不厌其烦的讲各个语言都差不多的一些编程基础内容。
我比较认同《七周七语言》书中的观点,若要领会一门语言的精髓,应该从以下5个方面入手:
- 语言的类型模型是什么?强类型或弱类型,静态类型或动态类型。
- 语言的编程范式是什么?是面向对象、函数式、过程式、还是其它的,或者是各种范式的综合体。
- 怎样和语言交互?编译型还是解释型。
- 语言的判断结构和核心数据结构是什么。
- 哪些核心特性让这门语言与众不同。
Ruby简介
- Ruby是由日本人松本行弘在1993年发明的
- Ruby是一门脚本语言、解释型、面向对象、动态类型的语言
Ruby基础语法特点
Ruby是一门解释型语言,不需要编译,能直接运行源代码。
在mac的终端上执行irb命令,就能进入Ruby的执行环境。
我们可以先输入一些简单的代码进去看看
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 5 => 5 5 + 5 => 10 'lijun' => "lijun" 'lijun' + 'hello' => "lijunhello" a = 'Ruby' => "Ruby" puts a Ruby => nil puts "hello, #{a}" hello, Ruby => nil puts 'hello, #{a}' hello, #{a} => nil |
从上面这段代码中,我们可以看出几点:
- Ruby是一门解释执行,不需要编译
- 不用声明变量
- 每条 ruby 代码都会返回某个值
- 单引号表示直接解释
- 双引号包含的字符串会进行字符串替换
纯面向对象,数字、函数都是对象,函数作为参数传递
Ruby 是一门纯面向对象的语言
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | irb(main):011:0* 5 => 5 5.class => Fixnum 5.class.class => Class 5.class.class.superclass => Module Module => Module Module.superclass => Object Object.class => Class Object.superclass => BasicObject Object.superclass.superclass => nil BasicObject => BasicObject BasicObject.methods => [:allocate, :new, :superclass, :freeze, :===, :==, :<=>, :<, :<=, :>, :>=, :to_s, :inspect, :included_modules, :include?, :name, :ancestors, :instance_methods, :public_instance_methods, :protected_instance_methods, :private_instance_methods, :constants, :const_get, :const_set, :const_defined?, :const_missing, :class_variables, :remove_class_variable, :class_variable_get, :class_variable_set, :class_variable_defined?, :public_constant, :private_constant, :singleton_class?, :include, :prepend, :module_exec, :class_exec, :module_eval, :class_eval, :method_defined?, :public_method_defined?, :private_method_defined?, :protected_method_defined?, :public_class_method, :private_class_method, :autoload, :autoload?, :instance_method, :public_instance_method, :nil?, :=~, :!~, :eql?, :hash, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__] |
在Ruby中一切皆对象,包括数字,也包括函数。
在ruby中是通过def ... end来定义一个函数
1 2 3 4 5 6 7 8 | irb(main):045:0* def say_hello(name) puts "hello, #{name}" end => :say_hello say_hello('lijun') hello, lijun => nil |
ruby的函数默认是将最后一行的代码的运行结果返回,当然也可以写return,指定返回的内容。
1 2 3 4 5 6 7 8 9 10 11 | irb(main):060:0* def is_equal(a,b) a == b end => :is_equal bool = is_equal(3,4) => false bool => false bool = is_equal(5,5) => true |
Ruby中也有类似oc中block的代码块,实际上就是一个匿名函数。
1 2 3 4 5 6 7 | irb(main):070:0* 5.times { puts 'hello, ruby' } hello, ruby hello, ruby hello, ruby hello, ruby hello, ruby => 5 |
动态类型、鸭子类型,强类型
判断结构和核心数据
Ruby核心特性
module and mixin
Ruby元编程
Ruby gem,rake,task
**未完待续**