CSC302 2011S Programming Languages

Reading: Ruby (2)

Reading: Tate, Section 2.3 (Floating Down From The Sky)

Please submit a question by 9 p.m. on Thursday, 27 January 2011.


Questions I've answered

On page 43, in the definition of initialize there is children=[], is this just saying that children is an array?

Yes.

Why does the following not work?

    {puts 'Hello, World!'}.call

Because functions are not quite first-class values in Ruby (at least from my perspective). They are not treated the same way other values are treated.

I do not quite understand the class hierarchy as explain by Tate on pages 41 and 42 in the part titled "Defining Classes." I am particularly confused about the meaning of 4.class.class. Could we go over the class hierarchy in class (pun intended)?

Yes.

Can we talk more about what a mixin is?

Yes.

What are some other practical uses of symbols :keyword in Ruby?

The most obvious use is in hashes that simulate named parameters.

Why is Ruby not able to sort keys start with ":" in a hash?

Because symbols in Ruby are atomic, just like symbols in Scheme. They are not supposed to represent strings. They are, instead, mnemonics that can be quickly compared for equality.

I'm still a bit confused about duck typing. In this section's example of duck typing, Tate writes, The module interacts with the including class at an intimate level. The module will often depend on several class methods. With Java, this contract is explicit: the class will implement a formal interface. With Ruby, this contract is implicit, through duck typing. So I guess the difference is that Java wouldn't even compile if there was a chance that a method were being used by an object that didn't have that method defined. But Ruby doesn't compile, so the difference seems to stem from - or even reduce to - Ruby's doing everything at runtime. Is this so?

It's a bit more complex than that. Ruby could still require you to specify the expected type of a parameter to a function and verify that the paremeter had that type (or included that mixin or ...). But Ruby delays the question until you try to do something with the object, even in executing code.

What does yield do in Ruby?

Yields control to the code block associated with the current thing.


Potential discussion questions

Why does Ruby use an end keyword instead of using whitespace or brackets?

Would it always be beneficial to add comparable and enumerable mixins to our classes whenever possible?

Why doesn't Ruby support named parameters? It seems like having them would provide programmers with some syntactic sugar (which seems like a pretty important factor to Ruby's designer).


Questions I have not yet answered

Can you explain or give an example of when to prepend a variable with @ and when to prepend a variable with @@?

It appears that using the & syntax one can pass blocks around at will. How does this work if blocks aren't objects? Is the interpreter just saving the code snippet behind the scenes?

Why is inject called inject? It sounds a lot like fold. Is the name inject preferred by any other languages?

Is there a way to force an object/class to be a singleton?

Can you explain the relationship between objects, classes, and modules?

What are the advantages / disadvantages of using modules instead of interfaces?

The attr_accessor keyword supposedly creates an accessor and a getter, which seem to be simply 'variable' and 'variable='. In other OO languages I was told to always leave variables private and use accessors/mutators to access and modify them. Why does Ruby not use this idea? (or is there some way to define the accessors and mutators explicitly?)

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Mon Feb 7 08:49:13 2011.
The source to the document was last modified on Mon Feb 7 08:48:40 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC302/2011S/Readings/ruby-2.html.

You may wish to validate this document's HTML ; Valid CSS! ; Creative Commons License

Samuel A. Rebelsky, rebelsky@grinnell.edu