Question

As a self-taught programmer, my definitions get fuzzy sometimes.

I'm very used to C and ObjC. In both of those your code must adhere to the language "structure". You can only do certain things in certain places. As an example, this is an error:

// beginning of file
NSLog(@"Hello world!"); // can't do this

@implementation MYClass
...
@end

However, in Ruby, anything you put anywhere is executed as the interpreter goes through it. So what is the difference between Ruby and Objective-C that allows this?

At first I thought it was that one was interpreted and the other compiled. Then I read some SO posts and the wikipedia definitions. Interpreted or compiled is a property of the implementation not the language. So that would mean there could (theoretically) be an interpreted implementation of Objective-C? In that case, the fact that a statement cannot be outside the implementation can't be a property of compiled languages, and vice-versa if there was a compiled implementation of Ruby. Or am I wrong in assuming that different implementations of a language would work the same way?

Was it helpful?

Solution 2

Ruby hides lots of stuff.

Ruby is OO like C++, Objective C and Java, and has main like C but you don't see this.

puts(42) is method call. It is a method of the main object called main. You can see it by typing puts self.

If you don't specify the receiver (receiver.method()) Ruby will use the implicit one, main.

Check available methods:
puts Object.private_methods.sort

Why you can put everything anywhere?

C/C++ look for main method called main, and when C/C++ find it, it will be executed.
Ruby on other hands doesn't need main or other method/class to run first.

It execute code from the first line until it meet the end of file(or __END__ on the separate line).

class Strongman
  puts "I'm the best!"
end

is just syntactic sugar for Class.new method:

Strongman = Class.new do
  puts "I'm the best!"
end

The same goes for 'module`.

for calls each and returns some kind of object. So you may think of it as something similar to method.

a = for i in 1..12; 42;end
puts a
# 1..12

In the end, it doesn't matter if it is method call or some kind of structure like C's int main(). Programming language decides what it should run first.

OTHER TIPS

I'm not sure there's a technical term for it, but in most programming languages the context of the statement is extremely important.

Ruby has a concept of a root or main context where code is allowed. Other scripting languages follow this convention, presumably made popular by languages like Perl which allowed for very concise programming.

This allows things like this to be a complete and valid program:

print "Hello world!\n"

In other languages you need to define an entry point, such as a main routine, that is executed instead. Arbitrary code is not really allowed at the top level, which instead is reserved for things like function, type, constant, structure and class definitions.

A language like Ruby has a lot of control over the order in which the code is executed. C, by comparison, is usually composed of separate source files that are then linked together, where there's no inherent order to the way things are linked. All the modules are simply assembled into the final library or executable. This is why the main entry point is required, it defines which function to run first.

In short, it boils down to syntax, context, and language design considerations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top