Test1.rb
==>
1.对象的相同的字符串,他们的对象不同, 但是 属性的值的Id相同
像java里面的Long与long区别
3.数字型的 对象Id相等
Test2.rb
==>
1. 对象属性没有值的时候,为nil, 感觉是"Null Object"
2. ttr_accessor :name 的效果 当然于class B里面的
Test3.rb
==>
1. ruby中的神奇函数inspect,
2. capitalize顾名思义
#TODO
print "1: "
puts "name".object_id
print "1: "
puts "name".object_id
print "1: "
puts "name".object_id
print "2: "
puts :name.object_id
print "2: "
puts :name.object_id
print "2: "
puts :name.object_id
print "3: "
puts 123456.object_id
print "3: "
puts 123456.object_id
print "3: "
puts 123456.object_id
# output
#1: 21719110
#1: 21719080
#1: 21719050
# "name".object --> 对象的Id
#2: 34258
#2: 34258
#2: 34258
# :name.object_id --> 对象的值的Id
#3: 246913
#3: 246913
#3: 246913
==>
1.对象的相同的字符串,他们的对象不同, 但是 属性的值的Id相同
像java里面的Long与long区别
3.数字型的 对象Id相等
Test2.rb
class A
attr_accessor :name
end
a = A.new
print "a.name = "
puts a.name
a.name = "Lily"
print "a.name = "
puts a.name
class B
def name
@name
end
def name=(name)
@name = name
end
end
b = B.new
print "b.name = "
puts b.name
b.name = "Lily"
print "b.name = "
puts b.name
# output
#a.name = nil
#a.name = Lily
#b.name = nil
#b.name = Lily
==>
1. 对象属性没有值的时候,为nil, 感觉是"Null Object"
2. ttr_accessor :name 的效果 当然于class B里面的
Test3.rb
class Symbol
def to_proc
proc {|obj, *args| obj.send(self, *args)}
end
end
words = %w(abc bcd cde def efg)
list = words.map(&:capitalize)
print "1: "
puts list.inspect
list2 = words.map {|w| w.send(:capitalize)}
print "2: "
puts list2.inspect
==>
1. ruby中的神奇函数inspect,
2. capitalize顾名思义
#TODO