Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Substring like in PHP's substr()?

Reply
Thread Tools

Substring like in PHP's substr()?

 
 
Joshua Muheim
Guest
Posts: n/a
 
      04-09-2007
Hi all

I'm looking for the coolest way to get the substring

/Webwork/pgbookings

out of

/Users/Josh/Webwork/pgbookings

The part /Users/Josh is saved in the variable my_var.

In PHP I would do something like the following:

substr(/Users/Josh/Webwork/pgbookings, strlen(my_var)) # =>
/Webwork/pgbookings

What's the fastest way to do this in Ruby?
Josh

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      04-09-2007
On Apr 9, 2007, at 6:52 AM, Joshua Muheim wrote:

> Hi all
>
> I'm looking for the coolest way to get the substring
>
> /Webwork/pgbookings
>
> out of
>
> /Users/Josh/Webwork/pgbookings
>
> The part /Users/Josh is saved in the variable my_var.
>
> In PHP I would do something like the following:
>
> substr(/Users/Josh/Webwork/pgbookings, strlen(my_var)) # =>
> /Webwork/pgbookings
>
> What's the fastest way to do this in Ruby?


I'm not sure which way is faster, so let's ask Ruby to time some of
my ideas:

#!/usr/bin/env ruby -w

require "benchmark"

data = "/Users/Josh/Webwork/pgbookings"
prefix = "/Webwork/pgbookings"

TESTS = 1_000_000
Benchmark.bmbm do |results|
results.report("[i..-i]:") { TESTS.times { data
[prefix.length..-1] } }
results.report("[i, l]:") { TESTS.times { data[prefix.length,
data.length] } }
results.report("sub():") { TESTS.times { data.sub(/\A#{prefix}/,
"") } }
results.report("[re, c]:") { TESTS.times { data[/\A#{prefix}(.+)/,
1] } }
end
# >> Rehearsal --------------------------------------------
# >> [i..-i]: 2.500000 0.000000 2.500000 ( 2.51186
# >> [i, l]: 0.630000 0.000000 0.630000 ( 0.634460)
# >> sub(): 6.800000 0.020000 6.820000 ( 6.850561)
# >> [re, c]: 7.180000 0.010000 7.190000 ( 7.204910)
# >> ---------------------------------- total: 17.140000sec
# >>
# >> user system total real
# >> [i..-i]: 2.500000 0.000000 2.500000 ( 2.500795)
# >> [i, l]: 0.630000 0.010000 0.640000 ( 0.63345
# >> sub(): 6.840000 0.000000 6.840000 ( 6.853133)
# >> [re, c]: 7.110000 0.010000 7.120000 ( 7.13039

__END__

Hope that helps.

James Edward Gray II

 
Reply With Quote
 
 
 
 
Jamal Soueidan
Guest
Posts: n/a
 
      04-09-2007
James Gray wrote:
> On Apr 9, 2007, at 6:52 AM, Joshua Muheim wrote:


Looks COOL...


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Rick DeNatale
Guest
Posts: n/a
 
      04-09-2007
On 4/9/07, James Edward Gray II <(E-Mail Removed)> wrote:
>
> data = "/Users/Josh/Webwork/pgbookings"
> prefix = "/Webwork/pgbookings"


I think you meant
prefix = "/Users/Josh"

This doesn't affect the qualitative results of the benchmark but it
does change it to do what the OP was trying to do.

Note that "/Webwork/pgbookings" is NOT a prefix of
"/Users/Josh/Webwork/pgbookings"

And cosmetically there's a typo:

> results.report("[i..-i]:") { TESTS.times { data
> [prefix.length..-1] } }


should be:

results.report("[i..-1:") { TESTS.times { data[prefix.length..-1] } }

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      04-09-2007
On Apr 9, 2007, at 8:33 AM, Rick DeNatale wrote:

> On 4/9/07, James Edward Gray II <(E-Mail Removed)> wrote:
>>
>> data = "/Users/Josh/Webwork/pgbookings"
>> prefix = "/Webwork/pgbookings"

>
> I think you meant
> prefix = "/Users/Josh"
>
> This doesn't affect the qualitative results of the benchmark but it
> does change it to do what the OP was trying to do.


Oops, I did. Thank you.

> And cosmetically there's a typo:
>
>> results.report("[i..-i]:") { TESTS.times { data
>> [prefix.length..-1] } }

>
> should be:
>
> results.report("[i..-1:") { TESTS.times { data
> [prefix.length..-1] } }


Yeah, I blew that one, didn't I?

Thanks for the corrections.

James Edward Gray II

 
Reply With Quote
 
Joshua Muheim
Guest
Posts: n/a
 
      04-09-2007
Thank you guys.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Substring in String test, like Python: if "this" in thisString Ross Javascript 4 11-04-2009 06:29 PM
object-like macro used like function-like macro Patrick Kowalzick C++ 5 03-14-2006 03:30 PM
RegEx search for a substring within a substring colinhumber@gmail.com Perl Misc 3 08-03-2005 04:29 PM
find if there is a given substring inside a string juli ASP .Net 3 12-06-2004 11:52 AM
web sddress substring =?Utf-8?B?Sm9u?= ASP .Net 2 11-30-2004 01:31 PM



Advertisments