java guilt

J

Jos Backus

Jos, we've talked about this two months ago on ruby-core. Maybe it's
only the syntax of libraries like "import-module" which you don't like,
but I think they can do what you want. See below.

Indeed we did.
Changing the interface of import-module to something like Tom's example,
here's your use case:

require "import-module-extended"

class Bar

extending(String) do
def foo
"the foo version of #{self.inspect}"
end
end

def initialize(name)
@name = name
end

def hello(name = nil)
name ||= @name
puts(name.foo)
end

end

b = Bar.new("Jos")
b.hello # => the foo version of "Jos"
b.hello("Pit") # => the foo version of "Pit"

b.hello(1) rescue puts "no Fixnum#foo" # => no Fixnum#foo
"Pit".foo rescue puts "no String#foo" # => no String#foo

You can see that #foo is added to String but is only visible within
class Bar. (The implementation is a quick and dirty hack, though.)

That's sweet! This does indeed seem to do what I'm looking for. Thanks Pit!
And Tom, too. :)

If I wanted this to be used inside class Baz, too, would I stick `foo' in a
module and use `extending' with that module somehow? I need to go check out
`import-module-extended'...

If it's a hacky implementation, how could it be cleaned up? This would be
great to have as part of stdlib.

Cheers,
 
L

Logan Capaldo

Jos, we've talked about this two months ago on ruby-core. Maybe it's
only the syntax of libraries like "import-module" which you don't like,
but I think they can do what you want. See below.


Changing the interface of import-module to something like Tom's example,
here's your use case:

require "import-module-extended"

class Bar

extending(String) do
def foo
"the foo version of #{self.inspect}"
end
end

def initialize(name)
@name = name
end

def hello(name = nil)
name ||= @name
puts(name.foo)
end

end

b = Bar.new("Jos")
b.hello # => the foo version of "Jos"
b.hello("Pit") # => the foo version of "Pit"

b.hello(1) rescue puts "no Fixnum#foo" # => no Fixnum#foo
"Pit".foo rescue puts "no String#foo" # => no String#foo

You can see that #foo is added to String but is only visible within
class Bar. (The implementation is a quick and dirty hack, though.)
What happens if I do
class Q
def initialize(s)
s.foo
end
end

class Bar
def hello2
Q.new("hmm")
end
end

Bar.new.hello2

More importantly than what does happen, is what should happen?
 
T

Trans

What happens if I do
class Q
def initialize(s)
s.foo
end
end

class Bar
def hello2
Q.new("hmm")
end
end

Bar.new.hello2

More importantly than what does happen, is what should happen?

It should raise an error. #foo is not defined for String in the scope
of Q. Which is why my idea doesn't work --at least not as I originally
conceived it.

Any efficient implementation of this becomes quite a problem I
imagine. Seems like it would require all objects to be references, so
they could become any other type on the fly.

T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,237
Messages
2,571,190
Members
47,827
Latest member
wyton

Latest Threads

Top