Sean Lerner
2006-Nov-30 16:46 UTC
Newbie Q: Difference between methods that use () and methods that allow a .
Hello, Can anyone offer a pointer so that I can understand that difference between methods that use ( ) and methods that allow a . If I want to use strip_tags, this will work for me: strip_tags("<b>cow</b>") What I''m wondering is, why does this not work: "<b>cow</b>".strip_tags but for a different method, say pluralize, this works: "cow".pluralize For my particular usage, my code would be a lot cleaner if I could code like so: @comment.name.strip_tags @comment.email.strip_tags @comment.url.strip_tags @comment.comment.strip_tags instead, this is what I''m doing right now: @comment.name = strip_tags(@comment.name) @comment.email = strip_tags(@comment.email) @comment.url = strip_tags(@comment.url) @comment.comment = strip_tags(@comment.comment) Thanks, Sean --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jakob Skjerning
2006-Dec-03 12:57 UTC
Re: [POSSIBLE SPAM] [Rails] Newbie Q: Difference between methods that use () and methods that allow a .
On Nov 30, 2006, at 17:46 , Sean Lerner wrote:> If I want to use strip_tags, this will work for me: > > strip_tags("<b>cow</b>") > > What I''m wondering is, why does this not work: > > "<b>cow</b>".strip_tagsBecause the String object you create by doing "<b>cow</b>" doesn''t have a strip_tags method.> but for a different method, say pluralize, this works: > > "cow".pluralizeBecause String does have a pluralize method.> For my particular usage, my code would be a lot cleaner if I could > code > like so: > > @comment.name.strip_tags > @comment.email.strip_tags > @comment.url.strip_tags > @comment.comment.strip_tagsThere''s nothing stopping you from adding a strip_tags method to the String class: class String def strip_tags .. do stuff .. end end Although based on the two examples you posted, you''d probably want to call the method strip_tags! and have it modify the current object instead of just returning the modified value like the strip_tags helper does. -- Jakob Skjerning - http://mentalized.net --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---