Means of Optional Expression

T

Trans

Have a look at this. Note that I'm not bothering to implement the
#shuffle method as it is not the point --it's just an foo example.

module Kernel
def express(name)
extend self.class.const_get(name.to_s.capitalize)
end
end

class Array
module Random
def shuffle
[] # ...
end

def shuffle!
replace(shuffle)
end

# ...
end
end

a = [1,2,3]
a.shuffle #=> NoMethodError

a.express:)Random)
a.shuffle #=> []

T.
 
T

Trans

module Kernel
def express(name)
extend self.class.const_get(name.to_s.capitalize)
end
end

class Array
module Random
def shuffle
[] # ...
end

def shuffle!
replace(shuffle)
end

# ...
end
end

a = [1,2,3]
a.shuffle #=> NoMethodError

a.express:)Random)
a.shuffle #=> []

FYI. I'm thinking about using this approach for Facets' Core
extensions in 2.0. There would be two versions of each require, one
that automatically includes the extension and one that does not.

T.
 
T

Todd Benson

I don't see it being worth it really. a.extend(Array::Random); a.shuffle
What is this buying you? (Besides 6 characters, anyway)

I like it. Does anybody see a potential problem for this unobtrusive
'opening' of the class?

Todd
 
T

Trans

I don't see it being worth it really. a.extend(Array::Random); a.shuffle
What is this buying you? (Besides 6 characters, anyway)

Besides being more concise, it also limits us to valid choices. Of
course we can always use extend, but a.extend(String::Random) would
more than likely make a mess of things, and might not even die early
causing peculiar hard to track bugs.

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top