Newbie Q: Difference between methods that use () and methods

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

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_tags

Because the String object you create by doing “cow” doesn’t
have a strip_tags method.

but for a different method, say pluralize, this works:

"cow".pluralize

Because 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_tags

There’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 S. - http://mentalized.net