fork(1) download
  1. test = [1, 2, 'X', 3, '太郎', 5, 6]
  2.  
  3. # Use Ternary
  4. p test.map{ |x| x.is_a?(Integer) ? x : (x == "太郎" ? 4 : 0 ) }
  5.  
  6. # Or Use case statement
  7. def to_int(x)
  8. case x
  9. when Integer then x
  10. when "太郎" then 4
  11. else 0
  12. end
  13. end
  14.  
  15. p test.map{ |x| to_int(x) }
  16.  
Success #stdin #stdout 0.01s 28216KB
stdin
Standard input is empty
stdout
[1, 2, 0, 3, 4, 5, 6]
[1, 2, 0, 3, 4, 5, 6]