From: matt@... (matt neuburg) Date: 2008-08-02T03:39:10+09:00 Subject: Re: problem with "starts_with?" Skave Rat wrote: > matt neuburg wrote: > > Skave Rat wrote: > > > >> How can I get this code to work in my classes? > >> http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/ > >> StartsEndsWith.html > > > > require 'rubygems' > > require 'activesupport' > > include ActiveSupport::CoreExtensions::String > > > > puts "howdy".starts_with?("ho") > > > > Is that the kind of thing you mean? m. > > hm, yea, but the problem is it's not rails but simple ruby. > So I'd just like to add > > def starts_with?(prefix) > prefix = prefix.to_s > self[0, prefix.length] == prefix > end > > to my code where I can use it on all strings inside. Ah - so you mean you want to implement starts_with? yourself. Sure, then, as you've been shown, you can just implement a method on String. However, please do note that my solution *is* "simple ruby", not rails. What I showed you is how I would do it. In other words, there is nothing wrong with "stealing" existing code by requiring (and possibly including) an existing module. I do this kind of thing all the time. For example, I don't use rails, but I do use the active support -> string core extensions quite a bit (esp. the unicode stuff). Why reinvent the wheel? Sure, it happens that in your case it's a very easy wheel to reinvent; I'm just saying that snarfing up stuff from libraries into your scripts isn't something to run away from, it's something to run towards. It's the Ruby way. m. -- matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/ Leopard - http://www.takecontrolbooks.com/leopard-customizing.html AppleScript - http://www.amazon.com/gp/product/0596102119 Read TidBITS! It's free and smart. http://www.tidbits.com