Is it allowed to write code without method enclosure in a class / module? what does this code mean? [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this questionFor example a file in acti开发者_运维知识库ve_support/core_ext/exception.rb
it started with:
module ActiveSupport
if RUBY_VERSION >= '1.9'
FrozenObjectError = RuntimeError
else
FrozenObjectError = TypeError
end
end
Then continued with class Exception code
what does it mean? What do you use FrozenObjectError, RuntimeError, and TypeError objects for? How do I know where they are initialized? why do we need these lines of code?
Could you recommend me a good book to learn about this, please?
Thanks
Yes -- every line in Ruby is within an object, even the global scope. For your example, the 'error' for violating a Frozen Object is set based on what version is being run. In 1.9 it's a runtime exception whereas prior is a type error (this has to do with changes introduced in 1.9). This makes the exception as descriptive as possible.
Programming Ruby is very elegant at explaining the language:
Programming Ruby
精彩评论