Delete char

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.

···

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

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.

it is ok,but i want to know why my code can't run?

···

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

pt@pt-laptop:~$ irb
irb(main):001:0> 'gkhj*&()'[0]
=> 103
irb(main):002:0>

can i get 'gkhj*&()'[0]=>g??

···

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

I think you should take the advice of someone who shows you a Ruby way, but....

I looked at your code and modified it a little.
It seems to work but I'm tired so this is all I can offer now.

item='gkhj*&()'
#item = '((c)?'
item=item.split(//).map{|echar|
if /[a-z]/=~ echar then
   echar=echar
else
   echar=''
end}
p item*""

I would not do it this way but you wanted to know why your code did not work.

Harry

···

On Sun, Aug 29, 2010 at 9:45 PM, Pen Ttt <myocean135@yahoo.cn> wrote:

it is ok,but i want to know why my code can't run?
--
Posted via http://www.ruby-forum.com/\.

p 'gkhj*&()'[0] #Ruby 1.9
p 'gkhj*&()'.split(//)[0] #Ruby 1.8 ??

Harry

···

On Sun, Aug 29, 2010 at 9:56 PM, Pen Ttt <myocean135@yahoo.cn> wrote:

pt@pt-laptop:~$ irb
irb(main):001:0> 'gkhj*&()'[0]
=> 103
irb(main):002:0>

can i get 'gkhj*&()'[0]=>g??

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

pt@pt-laptop:~$ irb
irb(main):001:0> 'gkhj*&()'[0]
=> 103
irb(main):002:0>

can i get 'gkhj*&()'[0]=>g??

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)