Ideas on "Why Living Dangerous can be A Good Thing" in Ruby?

On Jan 8, 2006, at 2:44 AM, Gregory B. wrote:

Don’t let the java guys off so easily, either.

  • In Java it’s possible to add classes to a package unless that
    package is sealed.
  • When inner classes are compiled in Java they are converted to
    ordinary classes. Any private fields of the containing class are
    converted to public fields.
  • Cloning an object in Java bypasses its constructor.
  • Non-clonable classes can be extended and the child can implement
    cloneable.
  • Java objects can be serialized exposing state, including private
    fields.
  • Non-serializable objects can be sub-classed just like non-cloneable
    objects.
  • Serialized objects can be deserialized, bypassing the constructor.
  • Static fields are essentially globals, discoverable and settable by
    anyone.

All of these can be addressed with good, but tedious, coding
practices, but still when trying to secure Java code from another
programmer, you have your work cut out for you. This is especially
tricky in Java “frameworks” and development tools that routinely
serialize/deserialize objects and use reflection to create objects at
runtime.

Pete

On Jan 8, 2006, at 9:00 AM, [email protected] wrote:

One of the things I really liked about Bertrand Meyer’s Object
Oriented
Software Engineering
was his ‘programming by contract’ point of view.
I’ve found that concept to be useful in almost any program I’ve
written
after reading that book regardless of the language I’ve used.

One of the most useful ideas is the notion that if client code
refuses to adhere to the pre-conditions then all bets are off and the
library code has no responsibility to ensure correct behavior.

This is an excellent point, in my opinion. If I write code like:

not_my_object.instance_eval { @secret_value += 1 }

I know I’m crossing the line. When it eventually breaks, I’ll know
who’s fault it is. For now though, I’m telling Ruby, “Just trust me;
I know what I’m doing here.” The great part is that she believes me
and does it. :wink:

James Edward G. II

On 1/8/06, James Edward G. II [email protected] wrote:

refuses to adhere to the pre-conditions then all bets are off and the
library code has no responsibility to ensure correct behavior.

This is an excellent point, in my opinion. If I write code like:

not_my_object.instance_eval { @secret_value += 1 }

I know I’m crossing the line. When it eventually breaks, I’ll know
who’s fault it is. For now though, I’m telling Ruby, “Just trust me;
I know what I’m doing here.” The great part is that she believes me
and does it. :wink:

This is definitely a point worth making, that most people know that
meta-programming is dangerous stuff, so their either extremely careful
or their pretty open about letting you know that their efforts may
well have unsavory effects.

The key point to be made, I believe, is that ruby puts all the
power… and the responsibility, in the programmers hands. Which in
my opinion, is where it should be anyway.

On Jan 8, 2006, at 2:04 PM, Gregory B. wrote:

This is definitely a point worth making, that most people know that
meta-programming is dangerous stuff

If you are compiling this information for a written piece, I suggest
beginning with some definitions. I do not consider the above example
metaprogramming, for example, just a violation of encapsulation. :slight_smile:

James Edward G. II

On 1/8/06, James Edward G. II [email protected] wrote:

I know I’m crossing the line. When it eventually breaks, I’ll know
who’s fault it is. For now though, I’m telling Ruby, “Just trust me;
I know what I’m doing here.” The great part is that she believes me
and does it. :wink:

Let’s think of examples where this certainly is true and results in
good things. The first one that came to my head, and the example I
used in class was automated memoization. I’d like to hear some of the
greater hacks and accomplishments people have seen/done using the
openness and dynamicity of ruby as a feature, instead of a risk.
(There are plenty of them out there).

Does anyone have a good example of a domain specific language that
heavily uses metaprogramming to make it ‘work’ ?

On Jan 8, 2006, at 2:21 PM, Gregory B. wrote:

How would you (the community) define meta-programming

Code that writes code.

open class system

Classes that can be changed (methods added, removed, etc.) at runtime.

and the dynamic nature of ruby?

That’s a lot harder. :slight_smile:

I like to think that Ruby does away with much of the compile time vs
runtime separation and that is a big source of it’s dynamic nature.

You will need a better definition than that though, of course… :wink:

James Edward G. II

On 1/8/06, James Edward G. II [email protected] wrote:

This is definitely a point worth making, that most people know that
meta-programming is dangerous stuff

If you are compiling this information for a written piece, I suggest
beginning with some definitions. I do not consider the above example
metaprogramming, for example, just a violation of encapsulation. :slight_smile:

Well that depends on who put the secret value there, doesn’t it? :wink:

But good point. How would you (the community) define
meta-programming, open class system, and the dynamic nature of ruby?

On 1/8/06, James Edward G. II [email protected] wrote:

On Jan 8, 2006, at 2:21 PM, Gregory B. wrote:

How would you (the community) define meta-programming

Code that writes code.

I think this is most of the way there. From the wikipedia:

“Metaprogramming is the writing of programs that write or manipulate
other programs (or themselves) as their data or that do part of the
work that is otherwise done at runtime during compile time.”

open class system

Classes that can be changed (methods added, removed, etc.) at runtime.

It’s probably worthwhile to note that this makes things like irb
possible, no?
Seeing as the ‘main’ area of Ruby is just within an Object.

Classes can be fully manipulated too, I’m not sure if this is part of
a complete definition or not. That classnames themselves are first
class values and can be manipulated as such.

and the dynamic nature of ruby?

I like to think that Ruby does away with much of the compile time vs
runtime separation and that is a big source of it’s dynamic nature.

I like this idea. My professor had the misconception about ruby that
you could modify a class however you wanted, but could not remove it’s
original set of methods or undefine fields or things like that. This
misconception is due to the fact that static languages really do tend
to make their class definitions rather solid, where they’re as free as
anything else in ruby :stuck_out_tongue:

Hi –

On Mon, 9 Jan 2006, James Edward G. II wrote:

On Jan 8, 2006, at 2:21 PM, Gregory B. wrote:

How would you (the community) define meta-programming

Code that writes code.

Do you mean it’s synonymous with code generation? I don’t think I’d
make that association – not that I have a great definition for it.
In fact, I tend to think of metaprogramming in Ruby as programming
that someone things would seem cooler if it were called meta :slight_smile:
Another way to put which is: in Ruby, I’m not sure there’s really a
strict line.

Then there’s reflection and/or introspection, which I think are part
of it. If you examine what methods exist on a given object, rather
than just calling them, that’s reflective and also could be called
metaprogramming. But, again, I don’t really have a definition. Ruby
lets you eat the tablecloth, so to speak.

You will need a better definition than that though, of course… :wink:

“Ruby is dynamic, like human nature” – Matz, RubyConf 2001 (or 2002;
I think 2001).

David


David A. Black
[email protected]

“Ruby for Rails”, from Manning Publications, coming April 2006!

Gregory B. wrote:

Well that depends on who put the secret value there, doesn’t it? :wink:

But good point. How would you (the community) define
meta-programming, open class system, and the dynamic nature of ruby?

An element (the key element?) of meta-programming is the creation or
modification of methods, classes, and modules based on runtime
information. I.e. run-time introduction of new behavior.

So, as JEGII pointed out, merely bypassing encapsulation is not
metaprogramming by this (rough) definition.

But the means of introducing new behavior needs some examination; I can
think of ways of adding new behavior that probably wouldn’t strike many
people as examples of metaprogramming.

James

“One man’s meta is another man’s poison.”
or
“I never meta program I didn’t like.”

(I’m getting flashbacks of Whatsamatta U. Paging Jay Ward …)

http://www.ruby-doc.org - Ruby Help & Documentation
Ruby Code & Style - Ruby Code & Style: Writers wanted
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys
http://www.30secondrule.com - Building Better Tools

Hi –

On Mon, 9 Jan 2006, Gregory B. wrote:

open class system

Classes that can be changed (methods added, removed, etc.) at runtime.

It’s probably worthwhile to note that this makes things like irb possible, no?
Seeing as the ‘main’ area of Ruby is just within an Object.

Classes can be fully manipulated too, I’m not sure if this is part of
a complete definition or not. That classnames themselves are first
class values and can be manipulated as such.

I’d make a distinction between the “classes are objects too” thing,
and the matter of what you can do to classes. They could, for
example, be frozen, but still be first-class objects. So there are
separate things going on.

anything else in ruby :stuck_out_tongue:
Another key aspect of dynamicness in Ruby is that objects are not
constrained by the set of capabilities with which they are born –
essentially, the divergence of type from class.

David


David A. Black
[email protected]

“Ruby for Rails”, from Manning Publications, coming April 2006!

On 1/8/06, James B. [email protected] wrote:

So, as JEGII pointed out, merely bypassing encapsulation is not
metaprogramming by this (rough) definition.

How does this sound for a definition:

“When code is designed to dynamically write or modify it’s own code or
other code, this is considered to be Meta-programming. This often
makes heavy use of reflection and introspection, two key tools for
dynamic manipulation of code.”

Hi –

On Mon, 9 Jan 2006, Gregory B. wrote:

How does this sound for a definition:

"When code is designed to dynamically write or modify it’s own code or

s/'// :slight_smile:

other code, this is considered to be Meta-programming. This often
makes heavy use of reflection and introspection, two key tools for
dynamic manipulation of code."

I’m not sure I’d say that reflection and introspection (and by the
way, is there an established difference between those two terms?) are
“manipulation” techniques. In fact, they’re sort of non-manipulation
techniques; they give you information about what’s going on, without
actually changing anything.

(I’m still chickening out of trying to define metaprogramming myself
:slight_smile:

David


David A. Black
[email protected]

“Ruby for Rails”, from Manning Publications, coming April 2006!

On Sunday 08 January 2006 12:32 pm, James Edward G. II wrote:

Sentences like the above always read to me as: “Java was designed to
protect the programmer from doing programmer things.” Always sounds
funny to me.

Hi Edward,

I like being protected from myself. I’m an excellent analyst, designer,
and
yes, coder. But I’m somewhat careless. I make mistakes. That’s why I’m
glad I
have a cover on my circular saw. That’s why I’m glad my electric stove
has
lights saying which burners are still hot. And that’s why I enjoy
programming
in languages like Ruby, Python and Java, that protect me from myself.

I spent 10 years coding in C, and every time I had to track down
intermittents
that usually turned out to be one uninitialized variable, or a picket
fence
condition where I went off the end of the allocated array by one, or
forgot
to free a variable or freed it twice, and that kind of thing.

I know a lot of people who never make mistakes. They can code C all day
long
and never get a segfault. They needn’t be protected from themselves. But
not
all excellent programmers are like them.

SteveT

Steve L.

[email protected]

On Jan 8, 2006, at 3:42 PM, Steve L. wrote:

On Sunday 08 January 2006 12:32 pm, James Edward G. II wrote:

Sentences like the above always read to me as: “Java was designed to
protect the programmer from doing programmer things.” Always sounds
funny to me.

Hi Edward,

James actually. :wink:

And that’s why I enjoy programming in languages like Ruby, Python
and Java, that protect me from myself.

Wow, that’s a might unusual alliance of languages. I wonder how most
Java programmers would feel about being lumped in with Python and
Ruby on safety…

James Edward G. II

On 1/8/06, Steve L. [email protected] wrote:

That’s why I’m glad my electric stove has
lights saying which burners are still hot. And that’s why I enjoy programming
in languages like Ruby, Python and Java, that protect me from myself.

Java: Yes. (in the average scenario)
Python: Pretty much kinda maybe.
Ruby: You’ll shoot your eye out! :wink:

Hi –

On Mon, 9 Jan 2006, Gregory B. wrote:

How can I best rephrase, rewrite, etc the definition to show that
reflection is a key tool for doing manipulation, not something that
does manipulation itself?

I still don’t have what I consider a complete fix on what we
collectively mean by metaprogramming, but I think I’d say that Ruby’s
R&I facilities are preconditions of, or create a hospitable
environment for, metaprogrammming techniques.

For example:

unless obj.respond_to?(meth) # R&I
obj.singleton_class.class_eval {
define_method(meth) &block # metaprogramming, arguably
}
end

(using my favorite non-existent method, singleton_class :slight_smile:

David


David A. Black
[email protected]

“Ruby for Rails”, from Manning Publications, coming April 2006!

On 1/8/06, [email protected] [email protected] wrote:

Hi –

On Mon, 9 Jan 2006, Gregory B. wrote:

How does this sound for a definition:

"When code is designed to dynamically write or modify it’s own code or

s/'// :slight_smile:

Thanks :slight_smile:

other code, this is considered to be Meta-programming. This often
makes heavy use of reflection and introspection, two key tools for
dynamic manipulation of code."

I’m not sure I’d say that reflection and introspection (and by the
way, is there an established difference between those two terms?)

I suppose not, but they were your words. Perhaps the only difference
between them is that introspection is looking within oneself and
reflection is slightly more general. Maybe they’re the same thing.
I don’t know.

are “manipulation” techniques. In fact, they’re sort of non-manipulation
techniques; they give you information about what’s going on, without
actually changing anything.

Right. I didn’t intend to mean that they were manipulation
techniques, but that they were important tools for meta-programming.
If meta-programming is a manipulation technique, say… giving you a
haircut. Would you rather use a mirror (reflection) or not? :wink:

How can I best rephrase, rewrite, etc the definition to show that
reflection is a key tool for doing manipulation, not something that
does manipulation itself?

On 1/8/06, [email protected] [email protected] wrote:

R&I facilities are preconditions of, or create a hospitable
environment for, metaprogrammming techniques.

Agreed. It’s what makes us barbers instead of butchers, a nice big
mirror :slight_smile:

On Sunday 08 January 2006 04:52 pm, James Edward G. II wrote:

And that’s why I enjoy programming in languages like Ruby, Python
and Java, that protect me from myself.

Wow, that’s a might unusual alliance of languages. I wonder how most
Java programmers would feel about being lumped in with Python and
Ruby on safety…

Hi James,

Compare them to C :-). Or Perl.

Steve L.

[email protected]