最近在学习ruby,我觉得学习一门语言要先了解语言的基本特性,然后再深入语法、细节特性等。
所以先从ruby语言的特性入手,结合例子简单总结了几点,才真正开始学没多久,绝对不全面,还需后续的学习再补充。
?
?
1.纯面向对象的3.times { puts "Hi!" } #完成三次循环输出运行结果: Hi! Hi! Hi!
puts "#{'a'.class.superclass}"? 类本身也是对象,有成员变量和方法。 这个跟java语言有一些不一样的地方,java有原始基本类型和类之分,对原始基本类型有不一样的处理。
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方法的行为。 类可以随时增加、丢弃或修改自己的方法,这个给程序提供了很大的灵活性。弱类型不必多说,不需要给方法写返回类型,定义变量也不需要制定类型,运行期动态识别类型。
##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
#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定义了这个三个方法。