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 <a href="<%=h!u!link.uri%>"><%=h!link.name%></a> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 <google....-oTuY4Vk9bUDG8MNy1oJpyw@public.gmane.org> wrote:> 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 > > <a href="<%=h!u!link.uri%>"><%=h!link.name%></a> > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> the #h method actully allready exists in railsand 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/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
chrisfarms <chrisfarms-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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] valueYes... 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: <script> document.location="/foo?params=" . <%=h!j!u!url_fragment%> </script> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 <google....-oTuY4Vk9bUDG8MNy1oJpyw@public.gmane.org> wrote:> chrisfarms <chrisfa...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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: > > <script> > document.location="/foo?params=" . <%=h!j!u!url_fragment%> > </script> > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---