# Means of Optional Expression

**URL:** <https://www.ruby-forum.com/t/means-of-optional-expression/107170>\
**Category:** Ruby\
**Created:** [June 18, 2007, 7:48pm UTC](https://www.ruby-forum.com/t/means-of-optional-expression/107170 "2007-06-18T19:48:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [June 18, 2007, 7:48pm UTC](https://www.ruby-forum.com/t/means-of-optional-expression/107170/1 "2007-06-18T19:48:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [June 18, 2007, 9:47pm UTC](https://www.ruby-forum.com/t/means-of-optional-expression/107170/2 "2007-06-18T21:47:11Z")

</div>

On Jun 18, 1:48 pm, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> [] # ...
> 
> ```
> 
> 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.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [June 18, 2007, 10:32pm UTC](https://www.ruby-forum.com/t/means-of-optional-expression/107170/3 "2007-06-18T22:32:20Z")

</div>

On 6/18/07, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > class Array  
> > end  
> > that automatically includes the extension and one that does not.
> 
> T.

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

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [June 19, 2007, 12:21am UTC](https://www.ruby-forum.com/t/means-of-optional-expression/107170/4 "2007-06-19T00:21:01Z")

</div>

On 6/18/07, Logan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [June 19, 2007, 5:40am UTC](https://www.ruby-forum.com/t/means-of-optional-expression/107170/5 "2007-06-19T05:40:27Z")

</div>

On Jun 18, 4:31 pm, “Logan C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.
