Examples of 'Things' that are not Objects

2020-05-25 06:15发布

"Everything is an object" was one of the first things I learned about Ruby, but in Peter Cooper's Beginning Ruby: From Novice to Professional, it is mentioned that "almost everything in Ruby is an object".

Can you give me some examples of things that are not objects in Ruby?

标签: ruby oop
4条回答
The star\"
2楼-- · 2020-05-25 06:37

In the case of variable assignment, i.e. product = 5 * 5 the variable is not an object... so add that to the list

查看更多
等我变得足够好
3楼-- · 2020-05-25 06:45

The most obvious one that jumps into my head would be blocks. Blocks can be easily reified to a Proc object, either by using the &block parameter form in a parameter list or by using lambda, proc, Proc.new or (in Ruby 1.9) the "stabby lambda" syntax. But on its own, they aren't objects.

Another example are operators.

查看更多
欢心
4楼-- · 2020-05-25 06:48

After splitting the script into meaningful tokens by the lexer, everything is an object. Including classes. Even literal constants like 1 are objects. Some objects may have a syntax that is not purely OO (i.e. syntactic sugar) but that's mostly for easy manipulation more than anything. Blocks are not strictly objects though (but can as someone said be converted into one).

查看更多
Ridiculous、
5楼-- · 2020-05-25 06:53
  1. if
  2. else
  3. {
  4. }

general language constructs, etc...

I think pretty much everything else (including methods) are objects.

查看更多
登录 后发表回答