Dan Doel [mailto:djd15@po.cwru.edu] humbly replied/asked:
ruby -w test1.rb
“You ask for bark thru beethoven”
“arf!arf!”
“You ask for purr thru beethoven”
“sorry, cannot do purr thru beethoven”
I don’t think you can do this.
Variable names are incidental, essentially they’re just
pointers to the
underlying object.
You can have more than one variable pointing to the same
object, so any
object could
have an infinite number of ‘names’ (theoretically). Really, objects
don’t have a name,
variable names are just ways to help us remember which is which.
Why do you want to do this?
Thank you for asking, sir Dan.
I am a newbie, and am trying to build a lesson plan for learning oop for
beginners like me and non-programmers -and my son fits the bill. I hope to
finish it before summer so that me and my son can learn together.
Reading Dave’s PickAxe, I got to the portion of explaining to myself the
concept of receiver and method. Hoping that I’d simplify things to learn
first, I started off w a definition of a class without any methods defined
(in my example, class Dog). I would then instantiate an object of that kind
of class and bombard it with any methods I like.
However, I was stuck on “how does one know what methods were passed”. I want
to do this so I can say (especially to my son), “See son, it (the dog) got
what you asked for. Next, we will now let it do what we asked…”… I
thought this was possible since there is a method_missing() method wc knows
the method passed (-unfortunately it is missing :-)…
I follow same reasoning of the receiver… (and of course I may be wrong).
kind regards -botp
[snip]
I am a newbie, and am trying to build a lesson plan for learning oop for
beginners like me and non-programmers -and my son fits the bill. I hope to
finish it before summer so that me and my son can learn together.
[snip]
I tried to teach programming to my little bother (13 years old). He
understood the basic concepts (variables, methods), but lacks the
motivation for doing coding himself. I tried…
First teach the apprentice what a variable is: a pocket where you can
store useful things.
Next teach control structures: if then else, loop, 42.times, …
3rd teach methods.
Its more interesting when there is some sort of graphical output.
If you can make a small game: tetris. So that the apprentice can
better see the consequences of changes.
When the apprentice has reached this state, then we can move on to OOP.
Wish you luck 
···
On Sat, 24 Jan 2004 19:47:20 +0900, Peña, Botp wrote:
–
Simon Strandgaard
You could simulate that with some meta-programming magic involving
alias_method and method_added, but I wouldn’t show that code to your
son 
batsman@tux-chan:/tmp$ expand -t2 e.rb
keep this secret 
module MethodCalledMagic
def method_added(id)
@level ||= 0
return if @level == 1
@level += 1
alias_method “real#{id}”, id
module_eval <<-EOF
def #{id}(*a,&b)
puts “You called #{self.inspect}##{id}”
real#{id}(*a,&b)
end
EOF
@level -= 1
end
end
this is what your son sees
class Dog
extend MethodCalledMagic
end
d = Dog.new
class Dog
def foo; puts “the dog does foo” end
def bar; puts “the dog does bar” end
end
d.foo
d.bar
batsman@tux-chan:/tmp$ ruby e.rb
You called Dog#foo
the dog does foo
You called Dog#bar
the dog does bar
···
On Sat, Jan 24, 2004 at 07:47:20PM +0900, “Peña, Botp” wrote:
I am a newbie, and am trying to build a lesson plan for learning oop for
beginners like me and non-programmers -and my son fits the bill. I hope to
finish it before summer so that me and my son can learn together.
Reading Dave’s PickAxe, I got to the portion of explaining to myself the
concept of receiver and method. Hoping that I’d simplify things to learn
first, I started off w a definition of a class without any methods defined
(in my example, class Dog). I would then instantiate an object of that kind
of class and bombard it with any methods I like.
However, I was stuck on “how does one know what methods were passed”. I want
to do this so I can say (especially to my son), “See son, it (the dog) got
what you asked for. Next, we will now let it do what we asked…”… I
thought this was possible since there is a method_missing() method wc knows
the method passed (-unfortunately it is missing :-)…
–
_ _
__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_
_ \ / ` | ’ \
) | (| | |__ \ | | | | | (| | | | |
.__/ _,|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
And Bruce is effectively building BruceIX
– Alan Cox