文章分类 -  Ruby

Why is it bad style to `rescue Exception => e` in Ruby?
摘要:Ryan Davis’sRuby QuickRefsays (without explanation):Don’t rescue Exception. EVER. or I will stab you.Why not? What’s the right thing to do?Exceptionis the root of Ruby's exception hierarchy, so when yourescue Exceptionyou rescue fromeverything, including subclasses such asSyntaxError,LoadError, 阅读全文

posted @ 2013-11-24 20:11 优雅的码农 阅读(248) 评论(0) 推荐(0)

ruby Exception and StandardError
摘要:It is important to understand the place of StandardError in the hierarchy of ruby exceptions:class TestException1 < StandardErrorend class TestException2 < Exceptionend begin begin raise TestException1 rescue puts $!.class.name end begin raise TestException2 rescue puts $!.class.na... 阅读全文

posted @ 2013-11-24 20:04 优雅的码农 阅读(341) 评论(0) 推荐(0)

The meaning of => in ruby
摘要:While learning Ruby I've come across the "=>" operator on occasion. Usually I see it in the form of:symbol => valueand itseemsto be used frequently when passing values to functions. What exactly is that operator called? What does it do/mean? Is it built into Ruby or is it somethi 阅读全文

posted @ 2013-11-23 15:29 优雅的码农 阅读(110) 评论(0) 推荐(0)

理解Proc中的return引起的LocalJumpError
摘要:def procBuilder(message)Proc.new{ puts message;return}enddef test puts "entering method" p = procBuilder("entering proc") p.call puts "exit method"endtestBy design, this is to throw a LocalJumpError, but I don't rightly understand why. If I had to guess what this di 阅读全文

posted @ 2013-11-23 15:21 优雅的码农 阅读(110) 评论(0) 推荐(0)

Instance Variables
摘要:Instance variables begin with an at sign (@) and can be referenced only within class methods. They differ from local variables in that they don't exist within any particular scope. Instead, a similar variable table is stored for each instance of a class. Instance variables live within a class in 阅读全文

posted @ 2013-11-22 22:53 优雅的码农 阅读(516) 评论(0) 推荐(0)

Ruby中Require、Load、Include和Extend的区别
摘要:原文:http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/Require:require方法让你加载一个库,并且只加载一次,如果你多次加载会返回false。只有当你要加载的库位于一个分离的文件中时才有必要使用require。使用时不需要加扩展名,一般放在文件的最前面: require ‘test_library’Load:load用来多次加载一个库,你必须指定扩展名: load ‘test_library.rb’Include:当你的库加载之后,你可以在你的类定义中包含一个module,让module 阅读全文

posted @ 2013-11-20 10:28 优雅的码农 阅读(98) 评论(0) 推荐(0)

导航