# Best way to implement syntax sugar?

**URL:** <https://www.ruby-forum.com/t/best-way-to-implement-syntax-sugar/86246>\
**Category:** Rails\
**Created:** [February 20, 2007, 10:11pm UTC](https://www.ruby-forum.com/t/best-way-to-implement-syntax-sugar/86246 "2007-02-20T22:11:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tyler\_MacDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@Tyler\_MacDonald](https://www.ruby-forum.com/u/Tyler_MacDonald)\
**Post date:** [February 20, 2007, 10:11pm UTC](https://www.ruby-forum.com/t/best-way-to-implement-syntax-sugar/86246/1 "2007-02-20T22:11:03Z")

</div>

Hi!

I’m _very_ new to rails, I just dove in about a month ago and I’m  
loving it.

One thing I decided I immediately wanted to do, was shorten the  
number of keystrokes neccessary to URI, HTML, or Jscript encode  
something.  
The project I’ve come into already has helpers defined for this.

Right now, what I’ve done is:

alias h! string\_esacpe\_html  
alias u! string\_escape\_uri

Then later on in my views, I can do stuff like

\<%=h!email\_address%\>

Or

\<%=h!link.name%\>

It works great. However, I understand that “!” and “?” have special  
meanings in ruby. I tried other stuff (like “h~”, etc), but ruby doesnt  
like  
me using those symbols.

Is there a good way to implement such syntax sugar that follows the  
Ruby Way a bit more closely, or should I just not worry about it and  
stick  
with what i’ve got?

Thanks,  
Tyler

---

<div class="post-metadata">

**Author:** ![Tyler\_MacDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@Tyler\_MacDonald](https://www.ruby-forum.com/u/Tyler_MacDonald)\
**Post date:** [February 20, 2007, 11:36pm UTC](https://www.ruby-forum.com/t/best-way-to-implement-syntax-sugar/86246/2 "2007-02-20T23:36:20Z")

</div>

the #h method actully allready exists in rails, and is quite widely  
used.

in general (but certainly not strictly enforced):

obj#method! is a method that will cause obj to be modified or  
something potentially harmful/irreversable.

obj#method? will return a boolean [true/false] value

its nice to be able to read other’s source and have these implied  
expectations.

on a pointless note: if you wanted #h~ open irb and try…

self.class.send(:define\_method, :“h~”) {string\_esacpe\_html}

this is a ruby-is-fun example… and completly pointless as you’d have  
to call it via #send 🙂

On Feb 20, 8:56 pm, Tyler MacDonald [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Tyler\_MacDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@Tyler\_MacDonald](https://www.ruby-forum.com/u/Tyler_MacDonald)\
**Post date:** [February 20, 2007, 11:47pm UTC](https://www.ruby-forum.com/t/best-way-to-implement-syntax-sugar/86246/3 "2007-02-20T23:47:21Z")

</div>

> the #h method actully allready exists in rails

and so does #u method. They are both defined (as aliases for  
html\_escape and url\_encode respectively) in ERB::Util module which is  
included in ActionView::Base.

## Regards, Rimantas

[http://rimantas.com/](http://rimantas.com/)

---

<div class="post-metadata">

**Author:** ![Tyler\_MacDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@Tyler\_MacDonald](https://www.ruby-forum.com/u/Tyler_MacDonald)\
**Post date:** [February 21, 2007, 2:32am UTC](https://www.ruby-forum.com/t/best-way-to-implement-syntax-sugar/86246/4 "2007-02-21T02:32:33Z")

</div>

np…

not sure I like the no-space thing… but if you like that maybe you  
would like:

class String  
def +@  
do\_something(self)  
end  
end

then you can do

+“some text”

🙂

On Feb 21, 12:58 am, Tyler MacDonald [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Tyler\_MacDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@Tyler\_MacDonald](https://www.ruby-forum.com/u/Tyler_MacDonald)\
**Post date:** [February 21, 2007, 2:00am UTC](https://www.ruby-forum.com/t/best-way-to-implement-syntax-sugar/86246/5 "2007-02-21T02:00:31Z")

</div>

chrisfarms [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the #h method actully allready exists in rails, and is quite widely  
> used.

Awesome, I’m switching all of my code over to use that and #u now. 🙂

> obj#method! is a method that will cause obj to be modified or  
> something potentially harmful/irreversable.
> 
> obj#method? will return a boolean [true/false] value

Yes… so here’s why I went for using “!” anyway… I discovered this  
today (keep in mind I’ve only been rubying for about a month now) and it  
made me jump for joy…

When a method ends with punctuation, you dont have to put a space  
between  
a method name and the arguments!!!

That’s why I decided to futz with something like “h~”… To me,

\<%=h~email\_address%\>

or even

\<%=h!email\_address%\>

Looks way neater than

\<%=h email\_address%\>

Especially when you start stacking them, like:

What would be really nice is if there were a few other symbols  
available  
for method suffixing… I think “~” would be a nice one… I’d take that  
to  
mean “perform a match or transformation on the data to my right”, but  
that’s  
probably because I’m coming from the perl world where “=~” is the  
regular  
expression operator… “h~j~u~url\_fragement” would be a darn cool way to  
express that url\_fragement is being modified by “h”, “j”, and “u” on  
it’s  
way out to you… or stuff like:

ip\_address = dns~hostname

> on a pointless note: if you wanted #h~ open irb and try…
> 
> self.class.send(:define\_method, :“h~”) {string\_esacpe\_html}
> 
> this is a ruby-is-fun example… and completly pointless as you’d have  
> to call it via #send 🙂

Geeze. 🙂

Well, thanks both of you (chris and rimantas) for sorting me out. Now  
I  
have to decide if I’m going to keep the bang on the end of my homecooked  
transformers… that’s a touch one…

Thanks,  
Tyler
