0

I'm very new to ruby, in fact I just read it today. But I need to do something quick in Ruby, suppose I have a string ABC=180000. What I want to do is remove the last two chars and makes it become 8 chars of string, so the above case result should be 00001800

in case the string ABC=AA000, the final result is 00000AA0

Could you guys tell me how to do this quickly?

Thanks a lot!

user430926
  • 4,017
  • 13
  • 53
  • 77
  • 1
    Please look at the [Ruby documentation](http://ruby-doc.org/core-2.2.3/), especially the [String docs](http://ruby-doc.org/core-2.2.3/String.html) and tell us what you have tried already. – zwippie Nov 30 '15 at 10:31

2 Answers2

1

Untested as send from mobile:

"180000"[0...-2].rjust 8,"0"

Have a look at the docs: http://ruby-doc.org/core-2.2.3/String.html

pierrebeitz
  • 221
  • 1
  • 7
1

Use this,

a = "AAA000"
puts a[0..-3].rjust(8,"0")
Prasanna Sundar
  • 1,650
  • 1
  • 11
  • 16