[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

problem with "starts_with?"

Skave Rat

8/1/2008 1:33:00 AM

How can I get this code to work in my classes?
http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/StartsEnd...


I'd like to have all my custom methods located in one file, which I can
just include
--
Posted via http://www.ruby-....

5 Answers

matt

8/1/2008 3:05:00 AM

0

Skave Rat <skaverat@rf-linux.org> wrote:

> How can I get this code to work in my classes?
> http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensio...
> StartsEndsWith.html

require 'rubygems'
require 'activesupport'
include ActiveSupport::CoreExtensions::String

puts "howdy".starts_with?("ho")

Is that the kind of thing you mean? m.
--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

Skave Rat

8/1/2008 12:26:00 PM

0

matt neuburg wrote:
> Skave Rat <skaverat@rf-linux.org> wrote:
>
>> How can I get this code to work in my classes?
>> http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensio...
>> 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.
--
Posted via http://www.ruby-....

John Winters

8/1/2008 2:23:00 PM

0

Skave Rat wrote:
[snip]
> 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.

Surely you just do it? (Or have I missed the point somehow?)


class String
def starts_with?(prefix)
prefix = prefix.to_s
self[0, prefix.length] == prefix
end
end

candidate = "Banana"
puts candidate.starts_with?("Da").to_s


Just make sure the first bit gets executed somewhere early on in your
application's run.

HTH
John
--
Posted via http://www.ruby-....

Skave Rat

8/1/2008 2:45:00 PM

0

ah, I missed the "class String" part. works fine now, thanks
--
Posted via http://www.ruby-....

matt

8/1/2008 6:34:00 PM

0

Skave Rat <skaverat@rf-linux.org> wrote:

> matt neuburg wrote:
> > Skave Rat <skaverat@rf-linux.org> wrote:
> >
> >> How can I get this code to work in my classes?
> >> http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensio...
> >> 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...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...