in ruby how to remove the last part of ip address -


i new ruby.

i have ip address , need strip last part of ip address.

e.g.

208.68.38.12 becomes 208.68.38

how do in ruby?

can done way:

ip = "208.68.38.12" ip.split(".")[0...-1].join(".") #=> "208.68.38" 

if go step step, above used line of code quite self-explanatory. here:

ip.split(".") # splitting string on `.` have array #=> ["208", "68", "38", "12"] ip.split(".")[0...-1] # taking 0th n-1 th element of array (-1 represents last element when size unknown) #=> ["208", "68", "38"] ip.split(".")[0...-1].join(".") # joining array on `.` #=> "208.68.38" 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -