i want to make 'gkhj*&()' into 'gkhj',here is my code
item='gkhj*&()'
item=item.map{|echar|
if /[a-z]/=~ echar then
echar=echar
else
echar=''
end}
puts item
why i can't get what i want?
Any advice appriciated.
Much easier to use gsub with a negative character set for this:
puts item.gsub(/[^a-z]+/, '')
HTH,
Ammar
···
On Sun, Aug 29, 2010 at 12:02 PM, Pen Ttt <myocean135@yahoo.cn> wrote:
i want to make 'gkhj*&()' into 'gkhj',here is my code
item='gkhj*&()'
item=item.map{|echar|
if /[a-z]/=~ echar then
echar=echar
else
echar=''
end}
puts item
why i can't get what i want?
Any advice appriciated.
Not on every ruby version out there. You'd be better using this :
'gkhj*&()'[0, 1]
=> "g"
'gkhj*&()'[0..0]
=> "g"
(First is start index, count, the other is start index..end index ; both
work on rubies 1.8 and 1.9 at least.)
Fred
···
Le 29 août 2010 à 14:56, Pen Ttt a écrit :
--
Hey God, there's nothing left for me to hide
I lost my ignorance, security and pride
I'm all alone in this fucking world you must despise (Nine Inch Nails,
Hey God, I believed your promises, your promises and lies Terrible Lie)