Skip to content

Instantly share code, notes, and snippets.

@djsun
Created July 29, 2009 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djsun/157789 to your computer and use it in GitHub Desktop.
Save djsun/157789 to your computer and use it in GitHub Desktop.
class Example
def start
puts "start self : #{self.inspect}"
Other.action
puts "start self : #{self.inspect}"
end
def callback
puts "callback self : #{self.inspect}"
end
end
module Other
def self.action
puts "action self : #{self.inspect}"
#
# I want to call callback here, but:
# callback
# won't work.
#
# I'd rather not be explicit, e.g.
# Example.new.callback
#
# Ideas?
#
# I looked at 'caller' but that won't work.
#
puts "action self : #{self.inspect}"
end
end
e = Example.new
e.start
[8:51pm] djsun: ruby metaprogramming question: i'm in a method. how can I get the value of self from the enclosing call?
[8:53pm] Person1: djsun: in what context?
[8:59pm] djsun: Person1: here is an example http://gist.github.com/157789
[9:00pm] djsun: just made a tweak. see comment lines 17 to 23
[9:01pm] Person1: djsun: I don't see how the module and the class are related.
[9:02pm] djsun: Person1: it is a contrived example. I'd like to be able to 'pop' the self stack if that is possible
[9:02pm] Person1: Right, I don't know how to do that.
[9:02pm] Person2: djsun: just pass in self when you call action
[9:03pm] djsun: Person2: yeah, but that reminds me of Python can I get it some other way?
[9:03pm] Person2: there is no reason to try to meta-dance your way around OOP, because OOP can already do whatever you need
[9:03pm] Person1: ugh, your avoiding doing it because it *reminds* you of Python? Please.
[9:03pm] djsun: I'm curious if it is *possible* not if it is a good idea
[9:03pm] Person1: you're* for the grammar nazis.
[9:03pm] Person2: djsun: Python has code, better not use any of that, either.
[9:04pm] djsun: Person1: i say that half jokingly. but Python does require a lot of explicit passing of self, that's why I thought of it
[9:04pm] Person1: Ah, good.
[9:04pm] djsun: Person2: you guys are funny. my question is stumping you, i think
[9:04pm] djsun: a perfectly fine answer is "the language doesn't support it"
[9:04pm] Person2: djsun: the answer is, no, you cannot meta your way around OOP in Ruby.
[9:04pm] djsun: another answer is "you shouldn't want to do that". i get that argument.
[9:05pm] Person2: (unless you want to do it in C)
[9:05pm] djsun: Person2: but my question isn't really about OO
[9:05pm] djsun: i threw things in a class but that wasn't central to the problem
[9:06pm] djsun: anyhow, thanks for the ideas
[9:06pm] Person2: yes, it is. You want some method to reach out of it's context and grab information from somewhere else and have weird side effects, instead of taking arguments.
[9:07pm] djsun: Person2: well, i don't want to split hairs about what OO is and isn't. but the essence of my question was trying to pop the 'self' stack
[9:07pm] djsun: you have to admit that "caller" gets pretty close
[9:08pm] Person2: thinking of it as a "stack" is not the OOP paradigm
[9:08pm] Person2: caller is just a debugging convenience
[9:08pm] Person2: a kindness
[9:09pm] djsun: at any point, i think it does make sense to think of the calling stack. how Ruby stores that internally and does or does not make it available to the coder is another question
[9:11pm] Person2: If you're using LISP, sure. Then it wouldn't even be wrong.
[9:15pm] djsun: Person2: here's a project where being meta with Ruby is useful: http://ruby-prof.rubyforge.org/
[9:17pm] Person2: yeah, and it doesn't work in 1.9, surprise surprise
[9:17pm] djsun: Person2: you are pretty sour today
[9:18pm] Person2: well, I'm trying to stop some guy from blowing up the world and writing awful software with LISP paradigms to avoid doing it the Ruby way because the Ruby way looks like Python... I feel like, "put down the gun and lets talk"
[9:20pm] djsun: Person2: i detected the condescending attitude a mile away, but I don't think it was necessary
[9:21pm] djsun: "to avoid doing it the Ruby way because the Ruby way looks like Python" ,,, we already covered that
[9:21pm] djsun: "stop some guy from blowing up the world and writing awful software" --> i'm playing around and experimenting, its not a big deal
[9:22pm] djsun: "awful software with LISP paradigms" --> I'm not going to even go there.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment