Hash and multiline items

Y

Yannick Turgeon

Hello,

I'm a newbie and I wanted to get a hash with items having multilines string.
Something like this (air coded) :

SQL = {
"Company" => <<-END_SQL
SELECT Name
FROM Company
WHERE Active = 1
END_SQL
,
"Employee" => <<-END_SQL
SELECT Name, Title
FROM Employee
WHERE Active = 1
END_SQL
}

But it doesn't work. I get an error. I suppose I have to use the "\" line
continuation? I prefer this synthaxe though.

Is there a way to use the "labeled" multiline way in a hash? Any other style
suggestion?

Yannick
 
C

Charles Steinman

Yannick said:
Hello,

I'm a newbie and I wanted to get a hash with items having multilines string.
Something like this (air coded) :

SQL = {
"Company" => <<-END_SQL
SELECT Name
FROM Company
WHERE Active = 1
END_SQL
,
"Employee" => <<-END_SQL
SELECT Name, Title
FROM Employee
WHERE Active = 1
END_SQL
}

But it doesn't work. I get an error. I suppose I have to use the "\" line
continuation? I prefer this synthaxe though.

Is there a way to use the "labeled" multiline way in a hash? Any other style
suggestion?

It looks like heredoc statements don't play well with Hashes. How does
this strike your fancy?

SQL = {
"Company" => %q{
SELECT Name
FROM Company
WHERE Active = 1
},

"Employee" => %q{
SELECT Name, Title
FROM Employee
WHERE Active = 1
}
}
 
J

James Edward Gray II

Yannick said:
Hello,

I'm a newbie and I wanted to get a hash with items having
multilines string.
Something like this (air coded) :

SQL = {
"Company" => <<-END_SQL
SELECT Name
FROM Company
WHERE Active = 1
END_SQL
,
"Employee" => <<-END_SQL
SELECT Name, Title
FROM Employee
WHERE Active = 1
END_SQL
}

You just need to move the commas, they go after the heredoc
declaration, not after the end of the heredoc.

SQL = {
"Company" => <<-END_SQL,
SELECT Name
FROM Company
WHERE Active = 1
END_SQL
"Employee" => <<-END_SQL,
SELECT Name, Title
FROM Employee
WHERE Active = 1
END_SQL
}

You can even add calls to gsub() if you want to strip the leading
whitespace:

SQL = {
"Company" => <<-END_SQL.gsub(/^\s+/, "").tr("\n", " "),
SELECT Name
FROM Company
WHERE Active = 1
END_SQL
"Employee" => <<-END_SQL.gsub(/^\s+/, "").tr("\n", " "),
SELECT Name, Title
FROM Employee
WHERE Active = 1
END_SQL
}

Hope that helps.

James Edward Gray II
 
Y

Yannick Turgeon

Pretty nice! :eek:) I actually used %Q because I had a couple of #{} in my
queries to include today's date. I didn't know %q and %Q. Thanks for you
help.

Yannick
 
Y

Yannick Turgeon

Thanks for those informations, especially to strip the leading spaces and
ending nl. In this case it's not necessary but I'll try it anyway. ;o) This
comma after the initial heredoc label is not intuitive at all!

Yannick
 

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

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top