开发者

Is it allowed to write code without method enclosure in a class / module? what does this code mean? [closed]

开发者 https://www.devze.com 2023-01-13 03:35 出处:网络
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post.
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 7 years ago.

Improve this question

For 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

0

精彩评论

暂无评论...
验证码 换一张
取 消