【ruby】ruby语言的几个特性总结_Ruby_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > Ruby > 【ruby】ruby语言的几个特性总结

【ruby】ruby语言的几个特性总结

 2012/10/15 10:38:48  singleant  程序员俱乐部  我要评论(0)
  • 摘要:最近在学习ruby,我觉得学习一门语言要先了解语言的基本特性,然后再深入语法、细节特性等。所以先从ruby语言的特性入手,结合例子简单总结了几点,才真正开始学没多久,绝对不全面,还需后续的学习再补充。1.纯面向对象的在ruby世界里所有的都是对象,包括方法、类、模块。也包括字符、数字、bool等基本类型。如3.times{puts"Hi!"}#完成三次循环输出运行结果:Hi!Hi!Hi!3是一个整型对象,可以直接作为对象调用对象的方法times.方法的参数是一个函数也是一个对象。puts"#
  • 标签:总结 Ruby

最近在学习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, :===, :==, :<=>, :<, :<=, :>, :>=, :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__] [:<=>, :==, :===, :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, :<<, :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, :>, :>=, :<, :<=, :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, :<=>, :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定义了这个三个方法。

其他特性,等有总结再补。。。
发表评论
用户名: 匿名