# General delimiters

**URL:** <https://www.ruby-forum.com/t/general-delimiters/192178>\
**Category:** Ruby\
**Created:** [July 25, 2010, 6:54pm UTC](https://www.ruby-forum.com/t/general-delimiters/192178 "2010-07-25T18:54:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![industrial\_ghetto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/d07c76/32.png) [@industrial\_ghetto](https://www.ruby-forum.com/u/industrial_ghetto)\
**Post date:** [July 25, 2010, 6:54pm UTC](https://www.ruby-forum.com/t/general-delimiters/192178/1 "2010-07-25T18:54:37Z")

</div>

I would like to create a new general delimiter.

---------- Background

Suppose you have created a specific data type which is a combination of  
various other native data types, such as strings and numbers.

Suppose that these are needed throughout your code, so you would like to  
quickly express them.

I have found one way to do this,

class WhatEver  
.  
.  
.  
end

def we(o)  
WhatEver.new(o)  
end

so that I can type

```
     a = we " This 73 brqx "

```

in my code as a shorthand for

```
     a = WhatEver.new "This 73 brqx"

```

--------- Question

I think code would be more readable if I could type,

```
     a = %e "This 73 brqx"

```

-\> I know there is metaprogramming in Ruby, and Ruby is quite flexible  
-\> The question is… Is this possible? Where is %q, for example,  
defined? It does not appear to be a method of String or Object or  
Kernel  
-\> OK, so let’s say it’s something in C. Where would I modify the C to  
get this if I would want to?

---

<div class="post-metadata">

**Author:** ![industrial\_ghetto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/d07c76/32.png) [@industrial\_ghetto](https://www.ruby-forum.com/u/industrial_ghetto)\
**Post date:** [July 25, 2010, 8:36pm UTC](https://www.ruby-forum.com/t/general-delimiters/192178/2 "2010-07-25T20:36:22Z")

</div>

Ð˜Ð²Ð°Ð½ Ð¡Ñ‚Ð°ÑˆÐºÐ¾ wrote:

> I would like to create a new general delimiter.
> 
> ---------- Background
> 
> Suppose you have created a specific data type which is a combination of  
> various other native data types, such as strings and numbers.
> 
> Suppose that these are needed throughout your code, so you would like to  
> quickly express them.
> 
> I have found one way to do this,
> 
> class WhatEver  
> .  
> .  
> .  
> end
> 
> def we(o)  
> WhatEver.new(o)  
> end
> 
> so that I can type
> 
> ```
> a = we " This 73 brqx "
> 
> ```
> 
> in my code as a shorthand for
> 
> ```
> a = WhatEver.new "This 73 brqx"
> 
> ```
> 
> --------- Question
> 
> I think code would be more readable if I could type,
> 
> ```
> a = %e "This 73 brqx"
> 
> ```
> 
> -\> I know there is metaprogramming in Ruby, and Ruby is quite flexible  
> -\> The question is… Is this possible? Where is %q, for example,  
> defined? It does not appear to be a method of String or Object or  
> Kernel  
> -\> OK, so let’s say it’s something in C. Where would I modify the C to  
> get this if I would want to?

%q is part of Ruby’s syntax and not a method. You can’t modify how Ruby  
code is parsed via metaprogramming, the only way possible is to modify  
Ruby’s C sources before you compile Ruby.

What is possible is to “absuse” the special `method (you know, it's usually for executing system commands). Although`cmd`looks like a literal, it's actually the method Kernel#` and as such it can be  
overriden:

irb(main):001:0\> def `(str) irb(main):002:1> puts str irb(main):003:1> end => nil irb(main):004:0>`abc`  
abc  
=\> nil  
irb(main):005:0\>

Note that it’s possible to do limit the redefined ` to a namespace:

irb(main):001:0\> class Foo  
irb(main):002:1\> def `(str) irb(main):003:2> puts "Got #{str}." irb(main):004:2> end irb(main):005:1> def bar irb(main):006:2>`echo hello`irb(main):007:2> end irb(main):008:1> end => nil irb(main):009:0>`echo hello`=> "hello\n" irb(main):010:0> Foo.new.bar Got echo hello. => nil irb(main):011:0> module Bar irb(main):012:1> class << self irb(main):013:2> def`(str)  
irb(main):014:3\> puts “Got #{str}.”  
irb(main):015:3\> end  
irb(main):016:2\> end  
irb(main):017:1\> `echo hello`  
irb(main):018:1\> end  
Got echo hello.  
=\> nil  
irb(main):019:0\> `echo hello`  
=\> “hello\n”  
irb(main):020:0\>

I’ve never seen this anywhere, so I suppose it’s considered bad style.

Marvin

---

<div class="post-metadata">

**Author:** ![industrial\_ghetto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/d07c76/32.png) [@industrial\_ghetto](https://www.ruby-forum.com/u/industrial_ghetto)\
**Post date:** [July 26, 2010, 12:39am UTC](https://www.ruby-forum.com/t/general-delimiters/192178/3 "2010-07-26T00:39:53Z")

</div>

Thank you for the answer. So as I understand it, to get the effect that  
I want, I would have to do some C coding. This might be interesting, I  
have to think about it.

Overlaying ` like you describe is also interesting but seems useful to  
protect unsafe code from executing unsafe commands… Other than that,  
a single tick will not stand out in the code clearly to indicate what I  
want.

Well, at least I know the answer now. I will just put this away for a  
while. Thank you.

---

<div class="post-metadata">

**Author:** ![industrial\_ghetto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/d07c76/32.png) [@industrial\_ghetto](https://www.ruby-forum.com/u/industrial_ghetto)\
**Post date:** [July 25, 2010, 8:37pm UTC](https://www.ruby-forum.com/t/general-delimiters/192178/4 "2010-07-25T20:37:45Z")

</div>

Marvin GÃ¼lker wrote:

> What is possible is to “absuse” the special ` method

I meant “abuse”, sorry.

Marvin

---

<div class="post-metadata">

**Author:** ![industrial\_ghetto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/d07c76/32.png) [@industrial\_ghetto](https://www.ruby-forum.com/u/industrial_ghetto)\
**Post date:** [July 26, 2010, 8:11pm UTC](https://www.ruby-forum.com/t/general-delimiters/192178/5 "2010-07-26T20:11:49Z")

</div>

On 7/25/10, Ð˜Ð²Ð°Ð½ Ð¡Ñ‚Ð°ÑˆÐºÐ¾ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> --------- Question
> 
> I think code would be more readable if I could type,
> 
> ```
> a = %e "This 73 brqx"
> 
> ```

I have in the past created preprocessors to achieve this type of  
effect, based on my library RubyLexer. RubyLexer turns ruby source  
into a stream of tokens, which can then be converted back to the same  
source code. It can fairly easily be extended to recognize new token  
types. And it’s all written in ruby, so understanding and modifying  
the MRI lexer c code is not necessary. I believe this would be much  
easier to do than hacking on MRI c code, but it is nowhere near as  
easy as redefining operators is. There is some knowledge of RubyLexer  
internals required, tho, and I can help you with this if you’d like to  
try.
