irb(main):092:0> class Cat irb(main):093:1> end => nil irb(main):087:0> c = Cat.new => #<Cat:0x2bacb68> irb(main):088:0> c.class => Cat irb(main):089:0> c.class.superclass => Object irb(main):090:0> c.class.superclass.superclass => nil irb(main):091:0> c.class.superclass.superclass.superclass NoMethodError: undefined method `superclass' for nil:NilClass from (irb):91 from :0
irb(main):100:0> 1.class => Fixnum irb(main):101:0> 1.class.superclass => Integer irb(main):102:0> 1.class.superclass.superclass => Numeric irb(main):103:0> 1.class.superclass.superclass.superclass => Object
irb(main):100:0> String.class => Class irb(main):101:0> String.class.superclass => Module irb(main):102:0> String.class.superclass.superclass => Object
irb(main):020:0> module MyModule irb(main):021:1> def hello irb(main):022:2> puts "Hello World" irb(main):023:2> end irb(main):024:1> end irb(main):025:0> test = MyModule.new NoMethodError: undefined method `new' for MyModule:Module from (irb):25 irb(main):026:0> class MyClass irb(main):027:1> include MyModule irb(main):028:1> end => MyClass irb(main):029:0> test = MyClass.new => #<MyClass:0x2c18bc8> irb(main):030:0> test.hello Hello World => nil
irb(main):113:0> y = Human.new => #<Human:0x319b6f0> irb(main):114:0> def y.paint irb(main):115:1> puts "Can paint" irb(main):116:1> end => nil irb(main):117:0> y.paint Can paint => nil irb(main):118:0> z = Human.new => #<Human:0x3153fc0> irb(main):119:0> z.paint NoMethodError: undefined method `paint' for #<Human:0x3153fc0> from (irb):119
irb(main):120:0> y.singleton_methods => ["paint"]
irb(main):113:0> y = Human.new => #<Human:0x319b6f0> irb(main):114:0> class << y irb(main):115:1> def sing irb(main):116:1> puts "Can sing" irb(main):117:1> end irb(main):118:1>end => nil irb(main):117:0> y.sing Can sing => nil
irb(main):035:0> y = String.new("racecar")
=> "racecar"
irb(main):036:0> y.methods.grep(/palindrome/)
=> [ ]
irb(main):037:0> class String
irb(main):038:1> def palindrome?
irb(main):039:2> self == self.reverse
irb(main):040:2> end
irb(main):041:1> end
irb(main):050:0> y.palindrome?
=> true
class SelfTest def self.test puts "Hello World with self!" end end class SelfTest2 def test puts "This is not a class static method" end end SelfTest.test SelfTest2.test
irb(main):087:0> SelfTest.test Hello World with self! => nil irb(main):088:0> SelfTest2.test NoMethodError: undefined method 'test' for SelfTest2:Class from (irb):88
class TestMe def TestMe.test puts "Yet another static member function" end end TestMe.test # works fine
class MyTest class << self def test puts "This is a class static method" end end end MyTest.test # works fine
irb(main):198:0> class MyTest irb(main):199:1> end => nil irb(main):200:0> y = MyTest.new => #< MyTest:0x2d43fe0> irb(main):201:0> z = class MyTest irb(main):202:1> class << self irb(main):203:2> self irb(main):204:2> end irb(main):205:1> end => #<Class: MyTest > irb(main):206:0> z.class => Class irb(main):207:0> y.class => MyTest
irb(main):094:0> class SelfTest3 irb(main):095:1> def foo irb(main):096:2> self.bar() irb(main):097:2> end irb(main):098:1> def bar irb(main):099:2> puts "Testing Self" irb(main):100:2> end irb(main):101:1> end => nil irb(main):102:0> test = SelfTest3.new => #<SelfTest3:0x2d15750> irb(main):103:0> test.foo Testing Self => nil
irb(main):110:0> class SelfTest4 irb(main):111:1> def method1 irb(main):112:2> self.method2 irb(main):113:2> end irb(main):114:1> def method3 irb(main):115:2> method2 irb(main):116:2> end irb(main):117:1> private irb(main):118:1> def method2 irb(main):119:2> puts "Inside private method" irb(main):120:2> end irb(main):121:1> end => nil irb(main):122:0> y = SelfTest4.new => #<SelfTest4:0x2c13d80> irb(main):123:0> y.method1 NoMethodError: private method `method2' called for #<SelfTest4:0x2c13d80> from (irb):112:in `method1' irb(main):124:0> y.method3 Inside private method => nil
irb(main):104:0> self => main irb(main):105:0> self.class => Object
irb(main):135:0> class Test
irb(main):136:1> def method_missing(method, *args)
irb(main):137:2> puts "Method: #{method} Args: (#{args.join(', ')})"
irb(main):138:2> end
irb(main):139:1> end
=> nil
irb(main):140:0> t = Test.new
=> #<Test:0x2c7b850>
irb(main):141:0> t.f(23)
Method: f Args: (23)
=> nil
irb(main):142:0> class Test
irb(main):143:1> def method1(s, y)
irb(main):144:2> puts "S: #{s} Y: #{y}"
irb(main):145:2> end
irb(main):146:1> end
=> nil
irb(main):147:0>t = Test.new
irb(main):148:0> t.send(:method1, 23, 12)
S: 23 Y: 12
=> nil
irb(main):258:0> class SendTest irb(main):259:1> private irb(main):260:1> def hello irb(main):261:2> puts "Saying Hello privately" irb(main):262:2> end irb(main):263:1> end => nil irb(main):264:0> y = SendTest.new => #< SendTest:0x2cc52c0> irb(main):265:0> y.hello NoMethodError: private method `hello' called for #< SendTest:0x2cc52c0> from (irb):265 irb(main):266:0> y.send(:hello) Saying Hello privately => nil
begin
f = File.open("ruby.txt")
# .. continue file processing
rescue ex => Exception
# .. handle errors, if any
ensure
f.close unless f.nil?
# always execute the code in ensure block
end
irb(main):185:0> catch :label do irb(main):186:1* puts "This will print" irb(main):187:1> throw :label irb(main):188:1> puts "This will not print" irb(main):189:1> end This will print => nil
irb(main):190:0> catch :label do irb(main):191:1* catch :label1 do irb(main):192:2* puts "This will print" irb(main):193:2> throw :label irb(main):194:2> puts "This won't print" irb(main):195:2> end irb(main):196:1> puts "Neither will this print" irb(main):197:1> end This will print => nil
#!/usr/bin/env ruby
def func(id, count)
i = 0;
while (i < count)
puts "Thread #{i} Time: #{Time.now}"
sleep(1)
i = i + 1
end
end
puts "Started at #{Time.now}"
thread1 = Thread.new{func(1, 100)}
thread2 = Thread.new{func(2, 100)}
thread3 = Thread.new{func(3, 100)}
thread4 = Thread.new{func(4, 100)}
thread1.join
thread2.join
thread3.join
thread4.join
puts "Ending at #{Time.now}"
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有