Best way to implement syntax sugar?

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

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 :slight_smile:

On Feb 20, 8:56 pm, Tyler MacDonald [email protected]

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/

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”

:slight_smile:

On Feb 21, 12:58 am, Tyler MacDonald [email protected]

chrisfarms [email protected] 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. :slight_smile:

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 :slight_smile:

Geeze. :slight_smile:

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