Jul 22, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: The Ruby Object Model and Metaprogramming / Not sure how to interpret this?

We have examples of this in the screencast.


metaclass = class X
  class << self
     self
  end
end

simply returns the ghost class (the value of self in the inner class definition). By defining it in object, the method becomes available in all classes.

 
Jul 20, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: The Ruby Object Model and Metaprogramming / Why cool guys use obj.instance_eval instead of class << obj to define singletons?

Victor:

I know people use it. I just think they shouldn’t… :)

 
Jul 20, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: The Ruby Object Model and Metaprogramming / Why cool guys use obj.instance_eval instead of class << obj to define singletons?

As to your first point: that’s one reason I think @@var is unusable in real code. I also suspect that example will no longer work in 1.9. In general, @@var should be avoided.

For the second point: the biggest place the two are different is at the top level of your program…

 
Jul 13, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: Programming Ruby 3 / Will there be a version 2.1 of this book?

There is no version 2.1 of Ruby. 1.9 is the bleeding edge.

Ruby is distrinct from Rails—it it the language that Rails is written in, and it has a totally independent existence. Many (perhaps most) Ruby programmers have never used Rails.

Regards

Dave Thomas

 
Jul 12, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: The Ruby Object Model and Metaprogramming / Episode 2 - class << self

I use NetBeans for my Ruby and Ruby on Rails work and its debugger is very helpful in steppig through the program code. I add a “watch” on the “self” variable and it is fun to see it change as control moves from line to line.

One question I have is when would self become a module in these examples? I see it bounce between the Class names and Object (at the top level).

Try:


module Bharat
    p self
end
 
Jul 11, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: The Ruby Object Model and Metaprogramming / Episode 2 - class << self

Two problems:

First, attr_accessor is a provate method, and so can’t be called with a receiver.

Second, ‘self’ is the current class, so self.attr_accessor (if it worked) would be the same as attr_accessopr, which isn’t what you want. You want attr_accessor to go into the ghost singleton class.

Dave

 
Jul 10, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: Agile Web Development with Rails, 3rd Edition / RJS Error --- Starting over.

Bill:

If you still have the old file, using the ‘diff’ utility on it would tell you what was wrong, and then you’d know… ;)

Dave

 
Jul 9, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: The Ruby Object Model and Metaprogramming / Example 03 from Episode 5

It’s a local variable, just like any other. In this case, it is scoped to the block passed to Class.new.

Dave

 
Jul 8, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: Agile Web Development with Rails, 3rd Edition / RJS Error --- Starting over.

Why not instead download the code form our site and try that. If it works,. run diff between it and your code to see what might be wrong.

Regards

Dave

 
Jul 7, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: Agile Web Development with Rails, 3rd Edition / Is session a hash, a declaration, or both?

When you use session in an action or view, it’s at the instance level. Here, session is a class-level declaration. They’re two different things (although obviously related :)

Dave

 
Jun 27, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: The Ruby Object Model and Metaprogramming / include private?

Bharat:

It’s all contextual. When it’s good, it’s good. When it isn’t, it isn’t. Part of the skill of programming is knowing the difference, and that experience comes from trying, making mistakes, and learning.

In general, the motivation is never simply to write shorter code. It’s to write more maintainable code—code that’s easier to work with and change.

 
Jun 26, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: The Ruby Object Model and Metaprogramming / include private?

Bharat:

A private method is Ruby is one that cannot be called with a receiver, and therefore can only be called in the context of the current object. include() is a private method of class Module, and therefore can only be called when self is a Module or subclass thereof.

 
Jun 24, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: Programming Ruby 3 / Screencast on Meta Programming versus Programming Ruby

The screencast complements the material in the chapter. I feel it goes deeper in places and less deep in others—some things are best expressed in print, while others are expressed better interactively.

 
Jun 23, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: The Ruby Object Model and Metaprogramming / Episode 5: Careful with the cache.

David:

wrt point 1, you’re right—this is a “feature” of all basic caches. In these examples, I really wanted to demonstrate the metaprogramming—caching is just a convenient excuse.

wrt point 2. There’s definitely an issue when passing hashes. Arrays can be more forgiving.

Thanks for the comments

Dave

 
Jun 22, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: The Ruby Object Model and Metaprogramming / Questions regarding episode 1...



  class Dave

    # If self is Dave, why must I explicitly say self?
    #
    def self.meth
    end

Because def meth (without the self) would declare an instance method. You want to define a method callable on the class.

As for the top-level execution environment… well, at that point you’re actually inside something that looks like this:


class Object 
   Object.new.instance_eval do 
     def self.to_s 
       "main" 
     end 

     ## 
     # Your program gets inserted here... 
     ## 

   end 
end 
 
Jun 20, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: Agile Web Development with Rails, 3rd Edition / Does calculator example (pg.516) need a submit_tag method?

Works for me without…

Dave

 
Jun 19, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: The Ruby Object Model and Metaprogramming / define_method in instance_eval

define_method always creates an instance method—now idea why, but it’s a deliberate decision of the implementers.

Dave

 
Jun 19, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: The Ruby Object Model and Metaprogramming / Video chapters - feature request

Mike’s added them to all episodes—you should be able to redownload the ones you have,

 
Jun 13, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: The Ruby Object Model and Metaprogramming / Copy of slides

Etienne:

Thanks!

Unfortunately, many of the slides are constructed on the fly, so we don;t really have them in a form that could be published.

Dave

 
Jun 13, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: The Ruby Object Model and Metaprogramming / Quicktime? Why, oh why...

John:

We’re looking into it. Some people have had success with ffmpeg, but we know it’s an issue, and Mike’s working it this week. We’ll let people know when it is resolved.

 
Jun 11, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: The Ruby Object Model and Metaprogramming / url for downloading the code doesn't work.

We had a temporary problem earlier this afternoon, but I just checked and http://media.pragprog.com/screencasts/v-dtrubyom/code/v-dtrubyom-v-01-code.tgz seems to be working ok now.

Dave

 
Jun 4, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: RubyCocoa / Offensive blurb

I think it’s supposed to be mildly humorous, hence the ”...kind of”.

I also feel that calling some person disconnected with the issue a “jerk” is not really doing your cause much good :)

However, far be it for us to cause offense to Mac developers, given that we run our lives on your software. We’ve changed the blurb.

Dave

 
May 28, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: Erlang in Practice / Welcome To Erlang By Example

Mark:

Perhaps up have to unzip them?

Regards

Dave Thomas

 
May 15, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: Agile Web Development with Rails, 3rd Edition / Typo p.328?

Think of it in the sense of “divining rod”

 
May 9, 2008
Dave_8_trans_small Dave Thomas 39 posts

Topic: Agile Web Development with Rails, 3rd Edition / Pagination

will_paginate seems to be the definite winner in this area, and it’s easy to use…

39 posts

Page: 1 2