Re: Why, oh, why, little regexp?
- From: James Edward Gray II <james@xxxxxxxxxxxxxxxxxxx>
- Date: Wed, 31 Oct 2007 22:00:18 +0900
On Oct 31, 2007, at 7:46 AM, 7stud -- wrote:
Gavin Kistner wrote:On Oct 30, 9:30 pm, Daniel Waite <rabbitb...@xxxxxxxxx> wrote:That's clever use of ?a, which I recognize but have never seen anyone
use before. Thanks for the example!
My current favorite use for the ?x syntax is converting single-
character strings representing digits into their integer form:
# Jenny jenny, who can I turn to?
irb(main):006:0> "8675309".each_byte{ |x| p x - ?0 }
8
6
7
5
3
0
9
Perhaps this is clearer:
"8675309".each_byte{|code| puts code.chr}
...although slightly slower.
Printed content aside, it's not equivalent. The original code is making Integers, not Strings.
James Edward Gray II
.
- References:
- Why, oh, why, little regexp?
- From: Daniel Waite
- Re: Why, oh, why, little regexp?
- From: Daniel Waite
- Re: Why, oh, why, little regexp?
- From: Phrogz
- Re: Why, oh, why, little regexp?
- From: 7stud --
- Why, oh, why, little regexp?
- Prev by Date: Can we define data types explicitly?
- Next by Date: Re: Can we define data types explicitly?
- Previous by thread: Re: Why, oh, why, little regexp?
- Next by thread: Ruby Logo
- Index(es):
Relevant Pages
|