From: Martin DeMello Date: 2002-12-04T08:48:18+09:00 Subject: Re: Ruby Book for People Who Aren't (Yet) Programmers David A. Black wrote: > Hi -- > > On Wed, 4 Dec 2002, Daniel Carrera wrote: > >> Objects >> >> What is an object? >> >> An /object/ is the fundamental unit of data storage in Ruby. >> Objects have a /type/ (the type of data stored in them) and a /value/ >> (the data itself). Some examples include: > > I know you don't want to throw everything at the learner at once, > but I wonder whether this description of objects might make it > a lot harder later to explain/grasp the idea of an object as an > entity that encapsulates behaviors as well as data -- or even > without data. > > Given the above description, even at the simplest level: > > Object.new > > you'd have to do a lot of backtracking and re-explaining to > deal with questions like, "What is the data stored in this object? > What type of data is it? What is the value of the data?" > All of which would be completely reasonable questions, based on > the data-storage-unit model. Excellent point. How about this: An object is the fundamental unit of data storage in Ruby. A Ruby object consists of 1. An ID. This is a number Ruby assigns to the object when it is created, and uses to keep track of the object. Every object has its own unique ID. Since Ruby takes care of the bookkeeping details behind the scenes, we don't need to bother with the object ID ourselves, but it is a useful handle to grasp mentally when we want to think about a given object. 2. A value. This is the data stored in the object. Examples include numbers like 1, -2 and 3.14, and strings like "Hello World". A value can also be a more complex entity, like a set of numbers, or a collection of several pieces of data. 3. A set of /methods/. A method is a function that acts on an object's data, and returns another object. [Damn, this is hard. What's a 'function'? What does 'return' mean?] In a sense, the value represents the set of things an object /knows/, and the methods represent the set of things it can /do/. To get an object to do something, you /call/ one of its methods; this will give you back another object (known as the method's 'return value'). Methods are called using the syntax object.method_name. An example should make this clear - when we say "Hello World".reverse we are calling the "reverse" method of the object "Hello World". This returns another object, in this case "dlroW olleH". 4. A /type/. Ruby has several basic types of object. A type is a collection of methods, so that if two objects have the same type, they will have the same methods (though their values might be different). Some of Ruby's fundamental types are Fixnum (integers), Float (floating point numbers) and String (text strings). [Diagram here] When we create a new object, we have to specify its type. Ruby then automatically supplies our object with all the methods that type provides, and leaves a placeholder for the value. There are some special types of objects that Ruby lets us create directly by providing a value. These include Fixnum, Float and String. Ruby can automatically deduce the types of these values, and create the objects of the appropriate type to store them. So when you type in "Hello World".reverse the following sequence of actions goes on behind the scenes: 1. Ruby sees something in double quotes, and recognises it as a String 2. A new String object is created, and supplied with all the String methods 3. The value of the new object is set to "Hello World" 4. Ruby sees that we are trying to call the object's 'reverse' method 5. Ruby checks to see if the object has a reverse method 6. Since the object is of type String, it has automatically got a 'reverse' method, so Ruby calls it 7. The 'reverse' method returns a new String object whose value is the reversal of the original String's 8. Ruby returns this new object to us. Okay, that got too verbose and too technical, but it's 3:30am :) I'll have another go at it in the morning. martin