# Problem with "starts\_with?"

**URL:** <https://www.ruby-forum.com/t/problem-with-starts-with/144678>\
**Category:** Ruby\
**Created:** [August 1, 2008, 3:33am UTC](https://www.ruby-forum.com/t/problem-with-starts-with/144678 "2008-08-01T03:33:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![skaverat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@skaverat](https://www.ruby-forum.com/u/skaverat)\
**Post date:** [August 1, 2008, 3:33am UTC](https://www.ruby-forum.com/t/problem-with-starts-with/144678/1 "2008-08-01T03:33:58Z")

</div>

How can I get this code to work in my classes?  
[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/StartsEndsWith.html](http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/StartsEndsWith.html)

I’d like to have all my custom methods located in one file, which I can  
just include

---

<div class="post-metadata">

**Author:** ![skaverat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@skaverat](https://www.ruby-forum.com/u/skaverat)\
**Post date:** [August 1, 2008, 5:11am UTC](https://www.ruby-forum.com/t/problem-with-starts-with/144678/2 "2008-08-01T05:11:19Z")

</div>

Skave R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How can I get this code to work in my classes?  
> [http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/](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.

---

<div class="post-metadata">

**Author:** ![skaverat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@skaverat](https://www.ruby-forum.com/u/skaverat)\
**Post date:** [August 1, 2008, 2:26pm UTC](https://www.ruby-forum.com/t/problem-with-starts-with/144678/3 "2008-08-01T14:26:45Z")

</div>

matt neuburg wrote:

> Skave R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > How can I get this code to work in my classes?  
> > [http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/](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.

---

<div class="post-metadata">

**Author:** ![skaverat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@skaverat](https://www.ruby-forum.com/u/skaverat)\
**Post date:** [August 1, 2008, 4:45pm UTC](https://www.ruby-forum.com/t/problem-with-starts-with/144678/4 "2008-08-01T16:45:31Z")

</div>

ah, I missed the “class String” part. works fine now, thanks

---

<div class="post-metadata">

**Author:** ![skaverat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@skaverat](https://www.ruby-forum.com/u/skaverat)\
**Post date:** [August 1, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/problem-with-starts-with/144678/5 "2008-08-01T16:23:59Z")

</div>

Skave R. 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

---

<div class="post-metadata">

**Author:** ![skaverat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@skaverat](https://www.ruby-forum.com/u/skaverat)\
**Post date:** [August 1, 2008, 9:19pm UTC](https://www.ruby-forum.com/t/problem-with-starts-with/144678/6 "2008-08-01T21:19:29Z")

</div>

Skave R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> 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.
