From: "Mauricio Fernández" Date: 2002-12-04T18:27:50+09:00 Subject: Re: Ruby Book for People Who Aren't (Yet) Programmers On Wed, Dec 04, 2002 at 08:48:18AM +0900, Martin DeMello wrote: > 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 I think it'd be better to state something more abstract, without specifically talking about data (because IMHO we need to introduce objects as active components of the system, not as passive data). Just for reference, here's how the explain objects in the Blue Book: "An object represents a component of the Smalltalk-80 software system. For example, objects represent * numbers * character strings * queues * dictionaties * rectangles [...] An object consists of some private memory and a set of operations. The nature of an object's operations depends on the type of component it represents. Objects representing numbers compute arithmetic functions. Objects representing data structures store and retrieve information. Objects representing positions and areas answer inquiries about their relation to other positions and areas. A message is a request for an object to carry out one of its operations. A message specifies which operation is desired, but not how that operation should be carried out. The receiver, the object to which the message was sent, determines how to carry out the requested operation. [...] The set of messages to which an object can respond is called its interface with the rest of the system. The only way to interact with an object is through its interface." [Goldberg, Adele. Smalltalk-80: the language and its implementation.] > 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. Is this needed for beginners? > 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). Having only "duck typing", I'm not sure whether it's safe to introduce types at first. Or at least, perhaps talking about classes [of objects] rather than types, just to use OO terminology from the beginning (so that people previously exposed to procedural languages don't mistake things, as having types is often confused with static typing). > [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 -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com Make it idiot-proof, and someone will breed a better idiot. -- Oliver Elphick