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

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -