Ruby's equivalent of PHP explode

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.

Frank

On 26/09/06, [email protected] [email protected] wrote:

irb(main):006:0> “this is a string”.split
=> [“this”, “is”, “a”, “string”]

Farrel

On 9/26/06, Farrel L. [email protected] wrote:

On 26/09/06, [email protected] [email protected] 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.

irb(main):006:0> “this is a string”.split
=> [“this”, “is”, “a”, “string”]

Also, can take an argument to split on a character/pattern besides
space:

irb>> “this is a string”.split(‘s’)
=> [“thi”, " i", " a ", “tring”]

irb>> “this is a string”.split(/[aeiou]/)
=> [“th”, "s ", "s “, " str”, “ng”]

Jacob F.

[email protected] 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.

On Sep 26, 2006, at 11:15 AM, Paul L. wrote:

end
No need to slurp a file so we can process it line by line. Also, the
equals-in-value problem is just one more argument to split():

ini = Hash.new

File.foreach(“file_name.ini”) do |line|
key, value = line.split("=", 2)
ini[key] = value
end

James Edward G. II

James Edward G. II wrote:

key,value = line.split("=")
my_hash[key] = value
end

No need to slurp a file so we can process it line by line.

Yes, true, this is an old habit of mine dating from the days of floppy
drives as data sources (mechanical wear issues), but obviously with
large
files, a potential source of difficulty.

Also, the
equals-in-value problem is just one more argument to split():

ini = Hash.new

File.foreach(“file_name.ini”) do |line|
key, value = line.split("=", 2)

Thanks! Hadn’t seen this syntax.

Paul L. wrote:

data = File.read(“filename.ini”)

my_hash = {}

data.each do |line|
key,value = line.split("=")
my_hash[key] = value
end

Here’s a modified version that supports sections:

data = File.read(“filename.ini”)

ini_hash = {}
ini_hash[""] = section_hash = {} # default unnamed section

data.each do |line|
if /^[.+]$/ =~ line # section headers are surrounded by brackets
ini_hash[line.chomp.gsub(/[[]]/, ‘’)] = section_hash = {}
else
key,value = line.chomp.split("=")
section_hash[key] = value
end
end

example of walking through the whole thing

ini_hash.each do |k, v|
puts “-#{k}-”
v.each {|sk, sv| puts " #{sk}=#{sv}"}
end

example of accessing a value by section and key

v = ini_hash[‘section’][‘key’]

Paul L. wrote:

boundaries), you’ve been given an answer:
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?

Ryan E. wrote:

Paul L. wrote:

data = File.read(“filename.ini”)

my_hash = {}

I am trying to understand what {} means in ruby. I understand that it
means an empty block, but what does it mean in this context?

Does it mean nil? Does it simply my_hash as an associative array?

Actually the split method has second parameter where you can specify
number of splits you want, i.e.:

data.each do |line|
key, value = line.split(/=/, 2)
end

HTH.
Mike D.
http://www.rubywizards.com

“Does it simply my_hash as an associative array?”
Hi
I think its better to call arrays arrays, and hashes hashes in Ruby.

I guess php simplifies this a bit too much for my taste,
with array() uniting both arrays and hashes.

Hash.new is the same as {} in ruby, although the second is a
tiny bit faster as far as I know.

nil means that something doesnt really exist, even an empty
‘’ string is not nil

quoth the [email protected]:

Ryan E. wrote:

Paul L. wrote:

data = File.read(“filename.ini”)

my_hash = {}

I am trying to understand what {} means in ruby. I understand that it
means an empty block, but what does it mean in this context?

Does it mean nil? Does it simply my_hash as an associative array?

It just creates an empty hash. It is equivalent to:

my_hash = Hash.new

And no, an empty hash is not nil:

irb(main):001:0> {} == nil
=> false

-d

[email protected] wrote:

Isn’t a ruby hash the same what some other languages call an
associative array?

Yes. But each language has it’s only idiom. For example, python calls
arrays “lists” and assoc. arrays “dictionaries”. Ohter languages use
the terms “map”, “index”, “table” and so forth for assoc. arrays. In
the ruby idiom we say hash.

Regards,
Jordan

quoth the [email protected]:

Marc H. wrote:

“Does it simply my_hash as an associative array?”
Hi
I think its better to call arrays arrays, and hashes hashes in Ruby.

Isn’t a ruby hash the same what some other languages call an
associative array?

Yeah, it’s just semantics. I think Marc’s point is that when using Ruby
it is
best to refer to them as Hashes to prevent confusion, as Ruby officially
calls them hashes. When using PHP call them associative arrays, and in
Python
call them dictionaries, for the same reason. When in Rome and all.

Wikipedia has an interesting page on the subject:

-d

Marc H. wrote:

“Does it simply my_hash as an associative array?”
Hi
I think its better to call arrays arrays, and hashes hashes in Ruby.

Isn’t a ruby hash the same what some other languages call an
associative array?

I wrote my first ruby program and displayed it here -
http://groups.google.co.uk/group/comp.lang.ruby/browse_frm/thread/d9a5608bdfb5ebbd/dfe7da9069b5bf40?lnk=st&q=voipfc&rnum=1&hl=en#dfe7da9069b5bf40

When I apply some text to it I receive this error message

=======
templatefiller.rb:33:in fetch': key not found (IndexError) from templatefiller.rb:33:in run’
from templatefiller.rb:32:in run' from templatefiller.rb:31:in run’
from templatefiller.rb:126
from c:/webdirectory/ruby/lib/ruby/1.8/find.rb:39:in find' from c:/webdirectory/ruby/lib/ruby/1.8/find.rb:38:in find’
from templatefiller.rb:110
from templatefiller.rb:109

Could anyone explain what is happening?