# Access to the case/when variable

**URL:** <https://www.ruby-forum.com/t/access-to-the-case-when-variable/81079>\
**Category:** Ruby\
**Created:** [January 3, 2007, 5:35pm UTC](https://www.ruby-forum.com/t/access-to-the-case-when-variable/81079 "2007-01-03T17:35:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bwv549](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@bwv549](https://www.ruby-forum.com/u/bwv549)\
**Post date:** [January 3, 2007, 5:35pm UTC](https://www.ruby-forum.com/t/access-to-the-case-when-variable/81079/1 "2007-01-03T17:35:22Z")

</div>

Is there a way to access the variable being tossed through the  
case/when structure?

case object.size.to\_s  
when /33/

# …

when /44/

# …

else  
abort "Invalid variable: " + ???

# Is there a way to access the variable being tossed through

case/when?

# something like: $\_, or $!

end

One can always do this:  
x = object.size.to\_s  
case x  
when /3ft/  
when /4cm/  
else  
abort “Bad match: #{x}”  
end

But it would save a line to be able to access the actual variable being  
used in the case/when structure.

Thanks

---

<div class="post-metadata">

**Author:** ![bwv549](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@bwv549](https://www.ruby-forum.com/u/bwv549)\
**Post date:** [January 3, 2007, 5:41pm UTC](https://www.ruby-forum.com/t/access-to-the-case-when-variable/81079/2 "2007-01-03T17:41:24Z")

</div>

case x = object.size.to\_s  
when /33/: # whatever  
when /44/: #etc.  
else  
x = 999

# whatever

end

Basically a combination approach. 🙂

–Jeremy

On 1/3/07, bwv549 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # Is there a way to access the variable being tossed through
> 
> abort “Bad match: #{x}”  
> end
> 
> But it would save a line to be able to access the actual variable being  
> used in the case/when structure.
> 
> Thanks

–  
My free Ruby e-book:  
[http://www.humblelittlerubybook.com/book/](http://www.humblelittlerubybook.com/book/)

My blogs:

> **[Site not found · DreamHost](http://www.mrneighborly.com/)**
>
> The owner of this domain has not yet uploaded their website.

[http://www.rubyinpractice.com/](http://www.rubyinpractice.com/)

---

<div class="post-metadata">

**Author:** ![bwv549](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@bwv549](https://www.ruby-forum.com/u/bwv549)\
**Post date:** [January 3, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/access-to-the-case-when-variable/81079/3 "2007-01-03T17:51:30Z")

</div>

On 03.01.2007 17:32, bwv549 wrote:

> Is there a way to access the variable being tossed through the  
> case/when structure?

You mean _object_ not _variable_. But no.

> end  
> But it would save a line to be able to access the actual variable being  
> used in the case/when structure.

If you want to save a line you can do

case x = object.size.to\_s  
…

Btw, why do you convert to string and then use regexps on the name? Do  
you want to check for numbers with all the same digits? You can do that  
with this RX: /^(\d)\1\*$/

Kind regards

```
robert
```
