From: voipfc@... Date: 2006-09-27T05:35:15+09:00 Subject: Re: Ruby's equivalent of PHP explode Paul Lutus wrote: > voipfc@googlemail.com wrote: > > > > > What is ruby's equivalent of PHPs explode? > > > > A routine which can breakdown a string and return the results in an > > array or a routine that can parse an .ini file and return the key > > value pairs in an array[key]=value. > > Actually, that is two questions. As to the first (explode a string on word > boundaries), you've been given an answer: > > array = string.split("\s") # many variations > > As to the second, read an .ini file and break it down by line and by > name/value pairs (not tested): > > data = File.read("filename.ini") > > my_hash = {} > > data.each do |line| > key,value = line.split("=") > my_hash[key] = value > end > > The second won't work properly if there are any equals signs in the value > field. The solution for this is only a bit more complex. > Will this fix the multiple = sign problem data.each do |line| my_hash[line.slice[0, line.index("=") - 1] = line.slice[line.index("="), line.length -line.index("=")] end I am new to Ruby and rightly or wrongly it looks rather unRuby like, can it be expressed more elegantly? > -- > Paul Lutus > http://www.arachnoid.com