I always get tripped when working together w arrays and strings specially on,
string.first and string.last
of course, they err
wish there were #first and #last in String
just a thought
kind regards -botp
I always get tripped when working together w arrays and strings specially on,
string.first and string.last
of course, they err
wish there were #first and #last in String
just a thought
kind regards -botp
irb(main):001:0> class String
irb(main):002:1> def first
irb(main):003:2> self.split('').first
irb(main):004:2> end
irb(main):005:1> def last
irb(main):006:2> self.split('').last
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> "testing".first
=> "t"
irb(main):010:0> "testing".last
=> "g"
-----Original Message-----
From: botp [mailto:botpena@gmail.com]
Sent: Saturday, August 11, 2007 10:30 AM
To: ruby-talk ML
Subject: first and last charI always get tripped when working together w arrays and
strings specially on,string.first and string.last
of course, they err
wish there were #first and #last in String
just a thought
kind regards -botp
Felix Windt wrote:
of course, they err
wish there were #first and #last in String
just a thought
kind regards -botpirb(main):001:0> class String
irb(main):002:1> def first
irb(main):003:2> self.split('').first
irb(main):004:2> end
irb(main):005:1> def last
irb(main):006:2> self.split('').last
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> "testing".first
=> "t"
irb(main):010:0> "testing".last
=> "g"
Ew, that's awfully complex. You create n new objects from which you
throw n-1 away again...
Think of the memory!
def first; self[0,1]; end; def last; self[-1,1]; end
Regards
Stefan
--
Posted via http://www.ruby-forum.com/.
Hi,
Am Sonntag, 12. Aug 2007, 02:43:00 +0900 schrieb Felix Windt:
> -----Original Message-----
> From: botp [mailto:botpena@gmail.com]
> Sent: Saturday, August 11, 2007 10:30 AM
>
> wish there were #first and #last in Stringirb(main):001:0> class String
irb(main):002:1> def first
irb(main):003:2> self.split('').first
irb(main):004:2> end
irb(main):005:1> def last
irb(main):006:2> self.split('').last
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> "testing".first
=> "t"
irb(main):010:0> "testing".last
=> "g"
Sometimes I wish every young programmer was forced to do a
month in Assembler and another one in C just to see what
cost in time and space some constructions cause.
Sorry, Felix!
Bertram
--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de
Stefan Rusterholz wrote:
Ew, that's awfully complex. You create n new objects from which you throw n-1 away again...
Think of the memory!
def first; self[0,1]; end; def last; self[-1,1]; end
wrong, that's the first and last *bytes*, not characters.
def first; self[/\A./m]; end
def last; self[/.\z/m]; end
>> $KCODE='u'
=> "u"
>> "日本語".first
=> "日"
>> "日本語".last
=> "語"
Daniel
Hi,
Hi,
From: botp [mailto:botpena@gmail.com]
Sent: Saturday, August 11, 2007 10:30 AMwish there were #first and #last in String
irb(main):001:0> class String
irb(main):002:1> def first
irb(main):003:2> self.split('').first
irb(main):004:2> end
irb(main):005:1> def last
irb(main):006:2> self.split('').last
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> "testing".first
=> "t"
irb(main):010:0> "testing".last
=> "g"Sometimes I wish every young programmer was forced to do a
month in Assembler and another one in C just to see what
cost in time and space some constructions cause.Sorry, Felix!
Well, here's something that should be a little bit less cycle intensive (depending on how String#[](arg) is implemented):
class String
def last
self[-1].chr
end
end
Cheers
Stephan
Am 13.08.2007 um 08:04 schrieb Bertram Scharpf:
Am Sonntag, 12. Aug 2007, 02:43:00 +0900 schrieb Felix Windt:
-----Original Message-----
--
Stephan Kämper/IT-Beratung http://www.stephankaemper.de
Softwaretest / Datenanalyse / Entwicklung
No problem :o)
When posting here, I tend to forget that I usually either write throwaway
scripts (one time processing of a problem), or scripts that get run
occasionally on faily large servers. So far, considering processing time and
memory simply stepped into the background over finding a solution quickly -
if a script takes 2 minutes longer to run but took 5 minute fewer to write
while solving something immediate, that's a net win in most situations I use
ruby in.
You're completely right, though, it's far from best practices.
Felix
-----Original Message-----
From: Bertram Scharpf [mailto:lists@bertram-scharpf.de]
Sent: Sunday, August 12, 2007 11:04 PM
To: ruby-talk ML
Subject: Re: first and last charHi,
Am Sonntag, 12. Aug 2007, 02:43:00 +0900 schrieb Felix Windt:
> > -----Original Message-----
> > From: botp [mailto:botpena@gmail.com]
> > Sent: Saturday, August 11, 2007 10:30 AM
> >
> > wish there were #first and #last in String
>
> irb(main):001:0> class String
> irb(main):002:1> def first
> irb(main):003:2> self.split('').first
> irb(main):004:2> end
> irb(main):005:1> def last
> irb(main):006:2> self.split('').last
> irb(main):007:2> end
> irb(main):008:1> end
> => nil
> irb(main):009:0> "testing".first
> => "t"
> irb(main):010:0> "testing".last
> => "g"Sometimes I wish every young programmer was forced to do a
month in Assembler and another one in C just to see what cost
in time and space some constructions cause.Sorry, Felix!
Bertram
--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de
Daniel DeLorme wrote:
Stefan Rusterholz wrote:
Ew, that's awfully complex. You create n new objects from which you
throw n-1 away again...
Think of the memory!
def first; self[0,1]; end; def last; self[-1,1]; endwrong, that's the first and last *bytes*, not characters.
def first; self[/\A./m]; end
def last; self[/.\z/m]; end>> $KCODE='u'
=> "u"
>> "日本語".first
=> "日"
>> "日本語".last
=> "語"Daniel
You are right, your solution is better.
Regards
Stefan
--
Posted via http://www.ruby-forum.com/.
For Ruby 1.9+ it will just be:
def first; self[0]; end; def last; self[-1]; end
my own version is (basically):
def first(pattern=//)
split(pattern).at(0)
end
which is a little more versatile. but I see the point about the
memory, and I'll add an optimization clause come 1.9.
T.
On Aug 11, 6:52 pm, Daniel DeLorme <dan...@dan42.com> wrote:
Stefan Rusterholz wrote:
> Ew, that's awfully complex. You create n new objects from which you
> throw n-1 away again...
> Think of the memory!
> def first; self[0,1]; end; def last; self[-1,1]; endwrong, that's the first and last *bytes*, not characters.
Hi,
Well, here's something that should be a little bit less cycle intensive
(depending on how String#[](arg) is implemented):class String
def last
self[-1].chr
end
end
This is what I would have implemented, too.
The difficult point is that it raises some questions:
- Should it return a Fixnum or a String of lenght 1?
- Should it be able to return UTF-8 characters?
- Should I define String#shift and String#pop now?
I don't recommend to discuss such question in an open forum
since I saw what happened to my String#notempty? proposal.
Bertram
Am Montag, 13. Aug 2007, 15:15:54 +0900 schrieb Stephan Kämper:
Am 13.08.2007 um 08:04 schrieb Bertram Scharpf:
--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de
Hi --
On Mon, 13 Aug 2007, Felix Windt wrote:
-----Original Message-----
From: Bertram Scharpf [mailto:lists@bertram-scharpf.de]
Sent: Sunday, August 12, 2007 11:04 PM
To: ruby-talk ML
Subject: Re: first and last charHi,
Am Sonntag, 12. Aug 2007, 02:43:00 +0900 schrieb Felix Windt:
-----Original Message-----
From: botp [mailto:botpena@gmail.com]
Sent: Saturday, August 11, 2007 10:30 AMwish there were #first and #last in String
irb(main):001:0> class String
irb(main):002:1> def first
irb(main):003:2> self.split('').first
irb(main):004:2> end
irb(main):005:1> def last
irb(main):006:2> self.split('').last
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> "testing".first
=> "t"
irb(main):010:0> "testing".last
=> "g"Sometimes I wish every young programmer was forced to do a
month in Assembler and another one in C just to see what cost
in time and space some constructions cause.Sorry, Felix!
Bertram
--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.deNo problem :o)
When posting here, I tend to forget that I usually either write throwaway
scripts (one time processing of a problem), or scripts that get run
occasionally on faily large servers. So far, considering processing time and
memory simply stepped into the background over finding a solution quickly -
if a script takes 2 minutes longer to run but took 5 minute fewer to write
while solving something immediate, that's a net win in most situations I use
ruby in.You're completely right, though, it's far from best practices.
If it results in a net win, then it sounds like it is a best practice.
Don't worry; there will be plenty of opportunity for performance
examination and critique, where it matters.
Assembler is really cool, though. Definitely worth a look
David
--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
Stefan Rusterholz wrote:
Daniel DeLorme wrote:
def first; self[/\A./m]; end
def last; self[/.\z/m]; end>> $KCODE='u'
=> "u"
>> "日本語".first
=> "日"
>> "日本語".last
=> "語"Daniel
You are right, your solution is better.
Only partly. Unfortunately, end-anchored regular expressions have pretty abysmal performance.
>> require "benchmark"
>> str = "日本語"*1000
>> Benchmark.measure{10000.times{str.first}}.real
=> 0.0704410076141357
>> Benchmark.measure{10000.times{str.last}}.real
=> 5.35788202285767
Daniel
Sad for Ruby.
T.
On Aug 13, 12:06 am, Bertram Scharpf <li...@bertram-scharpf.de> wrote:
I don't recommend to discuss such question in an open forum
since I saw what happened to my String#notempty? proposal.
Hi --
Hi,
Well, here's something that should be a little bit less cycle intensive
(depending on how String#[](arg) is implemented):class String
def last
self[-1].chr
end
endThis is what I would have implemented, too.
The difficult point is that it raises some questions:
- Should it return a Fixnum or a String of lenght 1?
If it ever gets added to Ruby, it will presumably be in 1.9/2.0, where
str[x] gives you a character anyway. If it doesn't get added, then
everyone will write their own, hopefully in a safe way, and can do
whatever they like
- Should it be able to return UTF-8 characters?
- Should I define String#shift and String#pop now?
There's already #chop. I don't know whether there are plans for
#lchop or equivalent.
David
On Mon, 13 Aug 2007, Bertram Scharpf wrote:
Am Montag, 13. Aug 2007, 15:15:54 +0900 schrieb Stephan Kämper:
Am 13.08.2007 um 08:04 schrieb Bertram Scharpf:
--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
Daniel DeLorme wrote:
Stefan Rusterholz wrote:
Daniel
You are right, your solution is better.
Only partly. Unfortunately, end-anchored regular expressions have pretty
abysmal performance.>> require "benchmark"
>> str = "日本語"*1000
>> Benchmark.measure{10000.times{str.first}}.real
=> 0.0704410076141357
>> Benchmark.measure{10000.times{str.last}}.real
=> 5.35788202285767
Daniel
That can be helped. Assuming that there is no encoding with 1 char > 8
bytes:
def first; self[/\A./m]; end
def last; self[/.\z/m]; end
def last2; self[-8,8][/.\z/m]; end
Benchmark.measure{10000.times{str.first}}.real
=> 0.0643939971923828
Benchmark.measure{10000.times{str.last}}.real
=> 7.3151650428772
Benchmark.measure{10000.times{str.last2}}.real
=> 0.167464017868042
That's a 40x improvement for that string. For short strings it will
probably be slightly slower, but I'd say it's worth it.
Regards
Stefan
--
Posted via http://www.ruby-forum.com/.