Ruby's equivalent of PHP explode

V

voipfc

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
 
F

Farrel Lifson

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

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

Farrel
 
J

Jacob Fugal

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 Fugal
 
R

Ryan Eibling

Paul said:
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']
 
J

James Edward Gray II

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

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 Gray II
 
V

voipfc

Paul said:
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?

 
M

Mike Dvorkin

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 Dvorkin
http://www.rubywizards.com


Paul said:
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?

 
V

voipfc

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?

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']
 
D

darren kirby

quoth the (e-mail address removed):
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
 
M

Marc Heiler

"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
 
V

voipfc

Marc said:
"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?
 
M

MonkeeSage

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
 
D

darren kirby

quoth the (e-mail address removed):
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:
http://en.wikipedia.org/wiki/Associative_array

-d
 
V

voipfc

I wrote my first ruby program and displayed it here -
http://groups.google.co.uk/group/co...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?

Mike said:
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 Dvorkin
http://www.rubywizards.com


Paul said:
(e-mail address removed) 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?

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top