---
title: "【ruby】ruby语言的几个特性总结"
author: "singleant"
date: "2012-09-13T16:16:23Z"
tags: []
categories: ["编程语言"]
slug: "ruby-ruby"
draft: false
lastmod: "2012-09-13T16:36:54Z"
id: 1677954
url: "https://singleant.iteye.com/blog/1677954"
---

最近在学习ruby，我觉得学习一门语言要先了解语言的基本特性，然后再深入语法、细节特性等。

所以先从ruby语言的特性入手，结合例子简单总结了几点，才真正开始学没多久，绝对不全面，还需后续的学习再补充。

 

 

**1.纯面向对象的**

在ruby世界里所有的都是对象，包括方法、类、模块。也包括字符、数字、bool等基本类型。

如 

```
3.times { puts "Hi!" }  #完成三次循环输出
```

运行结果:

Hi!

Hi!

Hi!

3是一个整型对象，可以直接作为对象调用对象的方法times. 方法的参数是一个函数也是一个对象。

```
puts "#{'a'.class.superclass}"  
```

 

类本身也是对象，有成员变量和方法。

这个跟java语言有一些不一样的地方，java有原始基本类型和类之分，对原始基本类型有不一样的处理。

**2.动态性、灵活性、弱类型**

ruby是动态的编程语言。不需要进行编译，没有编译的过程。所有都是在运行期进行构建的。有点类似js的运行机制。在运行期修改一个类的行为是很简单的。这个和java里通过aop等技术实现简单得多。

例：

```
class Integer
  def more
    return self+1
  end
end
puts 3.more
puts 4.more
class Integer
  def more
    return self+2
  end
end
puts 3.more
puts 4.more
```

运行结果：

4

5

6

7

从上面来看 class Integer不代表定义一个新的类，而有可能现成的类，进行结构修改。

给整型增加了一个more方法，同时又在后面修改了more方法的行为。

类可以随时增加、丢弃或修改自己的方法，这个给程序提供了很大的灵活性。弱类型不必多说,不需要给方法写返回类型，定义变量也不需要制定类型，运行期动态识别类型。

**3.模块的概念**

除了类，ruby还有模块的概念。

模块包含方法集，但是没有实例概念，不能实例化。也可以说模块是无状态的，有点类似静态方法。感觉这个是提供面向过程编程语言的一个特性，就是函数复用。

模块的方法可以很简单的的织入到类里面。

如：

```
##module test
module A
def add(a,b)
  return a+b
end
end

module B
     def sub(a,b)
       return a-b
     end
end

class Math2
include A
include B
end

ma=Math2.new
sum1=ma.add(3,4)
sum2=ma.sub(7,3)
puts "sum1: #{sum1}"
puts "sum2: #{sum2}"
```

 

运行结果：

sum1: 7

sum2: 4

以上定义了两个模块，并把模块织如到类里面。织入和继承有区别，感觉就像是组合和继承的差别。

**4.强大的反射和元编程**

反射就是可以了解类本身的结构，ruby提供和强大的反射技术，这个和java类似。

如：

```
#reflect
puts "#{'a'.class}"                    #列出字符串类名
puts "#{'a'.class.superclass}"         #列出字符串父类名
puts "#{'a'.class.methods}"            #列出字符串类方法列表
puts "#{'a'.class.instance_methods}"   #列出字符串实例方法列表
```

  结果：

String

Object

\[:try\_convert, :allocate, :new, :superclass, :freeze, :===, :==, :&lt;=&gt;, :&lt;, :&lt;=, :&gt;, :&gt;=, :to\_s, :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, :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, :initialize\_dup, :initialize\_clone, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?, :inspect, :methods, :singleton\_methods, :protected\_methods, :private\_methods, :public\_methods, :instance\_variables, :instance\_variable\_get, :instance\_variable\_set, :instance\_variable\_defined?, :instance\_of?, :kind\_of?, :is\_a?, :tap, :send, :public\_send, :respond\_to?, :respond\_to\_missing?, :extend, :display, :method, :public\_method, :define\_singleton\_method, :object\_id, :to\_enum, :enum\_for, :equal?, :!, :!=, :instance\_eval, :instance\_exec, :\_\_send\_\_, :\_\_id\_\_]

\[:&lt;=&gt;, :==, :===, :eql?, :hash, :casecmp, :+, :\*, :%, :\[], :\[]=, :insert, :length, :size, :bytesize, :empty?, :=~, :match, :succ, :succ!, :next, :next!, :upto, :index, :rindex, :replace, :clear, :chr, :getbyte, :setbyte, :byteslice, :to\_i, :to\_f, :to\_s, :to\_str, :inspect, :dump, :upcase, :downcase, :capitalize, :swapcase, :upcase!, :downcase!, :capitalize!, :swapcase!, :hex, :oct, :split, :lines, :bytes, :chars, :codepoints, :reverse, :reverse!, :concat, :&lt;&lt;, :prepend, :crypt, :intern, :to\_sym, :ord, :include?, :start\_with?, :end\_with?, :scan, :ljust, :rjust, :center, :sub, :gsub, :chop, :chomp, :strip, :lstrip, :rstrip, :sub!, :gsub!, :chop!, :chomp!, :strip!, :lstrip!, :rstrip!, :tr, :tr\_s, :delete, :squeeze, :count, :tr!, :tr\_s!, :delete!, :squeeze!, :each\_line, :each\_byte, :each\_char, :each\_codepoint, :sum, :slice, :slice!, :partition, :rpartition, :encoding, :force\_encoding, :valid\_encoding?, :ascii\_only?, :unpack, :encode, :encode!, :to\_r, :to\_c, :&gt;, :&gt;=, :&lt;, :&lt;=, :between?, :nil?, :!~, :class, :singleton\_class, :clone, :dup, :initialize\_dup, :initialize\_clone, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :methods, :singleton\_methods, :protected\_methods, :private\_methods, :public\_methods, :instance\_variables, :instance\_variable\_get, :instance\_variable\_set, :instance\_variable\_defined?, :instance\_of?, :kind\_of?, :is\_a?, :tap, :send, :public\_send, :respond\_to?, :respond\_to\_missing?, :extend, :display, :method, :public\_method, :define\_singleton\_method, :object\_id, :to\_enum, :enum\_for, :equal?, :!, :!=, :instance\_eval, :instance\_exec, :\_\_send\_\_, :\_\_id\_\_]

\[:mymethod, :test\_m1, :test\_m2, :test\_m3, :nil?, :===, :=~, :!~, :eql?, :hash, :&lt;=&gt;, :class, :singleton\_class, :clone, :dup, :initialize\_dup, :initialize\_clone, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :to\_s, :inspect, :methods, :singleton\_methods, :protected\_methods, :private\_methods, :public\_methods, :instance\_variables, :instance\_variable\_get, :instance\_variable\_set, :instance\_variable\_defined?, :instance\_of?, :kind\_of?, :is\_a?, :tap, :send, :public\_send, :respond\_to?, :respond\_to\_missing?, :extend, :display, :method, :public\_method, :define\_singleton\_method, :object\_id, :to\_enum, :enum\_for, :==, :equal?, :!, :!=, :instance\_eval, :instance\_exec, :\_\_send\_\_, :\_\_id\_\_]

元编程：

可以认为是写生成代码的代码。

以下例子：

```
#元编程
class TestMeta
def mymethod(n)
puts "in m#{n}"
end
h = {"m1" => 1, "m2" => 2, "m3" => 3}

h.each do |name,index|
define_method("test_#{name}") do
  mymethod(index)
  end
end
end

test=TestMeta.new
puts "#{test.class.instance_methods}"
test.test_m1
test.test_m2
test.test_m3
```

 

结果：

in m1

in m2

in m3

没有显示为TestMeta类定义test\_m1，test\_m2,test\_m3方法，但是可以直接调用，原因是通过definemethod定义了这个三个方法。

其他特性，等有总结再补。。。