Ruby as of 18 Ruby is an instructive example because it appe

\"Ruby (as of 1.8): Ruby is an instructive example because it appears to be the only other currently popular language that, like Python, tries to support statically nested scopes without requiring variable declarations, and thus has to come up with an unusual solution. Functions in Ruby can contain other function definitions, and they can also contain code blocks enclosed in curly braces. Blocks have access to outer variables, but nested functions do not. Within a block, an assignment to a name implies a declaration of a local variable only if it would not shadow a name already bound in an outer scope; otherwise assignment is interpreted as rebinding of the outer name. Ruby\'s scoping syntax and rules have also been debated at great length, and changes seem likely in Ruby 2.0\". Comment on the above. Why does the lack of variable declarations complicate the scope rules? Can you explain \"Blocks have access to outer variables but nested functions do not\". Is this similar to C++/Java or not? Is it better or not? Scope is much less complicated if functions cannot contain other function definitions (C++, Java: cannot; Ada, Pascal, Ada, Python, Ruby, Lisp: can). Give one good reason why functions should not contain other function definitions. Give one good reason why it should be allowed.

Solution

Q8.

a. The lack of variable declarations complicate the scope rules because Ruby support statically nested scopes without requiring variable declarations since blocks have access to outer variables but nested functions don\'t.

b. This pretty much eliminates the major use case for nested methods, since even if Ruby supported nested methods, you couldn\'t use the outer method\'s variables in the nested method.

In Ruby, blocks are not first class citizens - they are special language constructs which are meant to complement the functionality of regular methods. They are not objects (unlike most Ruby constructs), so they can’t be moved around or be referenced outside of their owner methods, and they don’t respond to any methods of their own.

Every method in the Ruby language is able to receive an implicit block. Unfortunately, not much can be done with the block except for calling it before it falls out of scope. It is not similar with Java/C++ and It is not better.

 \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site