Hey guys, I’m just getting my feet wet with Ruby (trying to learn the
syntax before jumping off into RoR) but I’m having some difficulty with
the parser understanding the order of operation…
I have the following code:
def parse_Requests(requests)
output = Array.new
requests.each{|item|
if(FileTest.exist?(item))
if FileTest.directory?(item)?output << parse_Dir(item) : output <<
parse_File(item)
else
output << parse_error(item)
end
}
end
but I am getting the following errors:
ls.rb:31: syntax error, unexpected ‘}’, expecting kEND
ls.rb:37: syntax error, unexpected $end, expecting kEND
it seems like once I breach the typical 1 liner for the each block that
it throws a fit. If this is the case, how do I navigate around it? If
its not the case, did I screw up on the logic somewhere? I’m not used to
using end to kill an if / else block so I guess I could have goofed
there…
I guess if I read the code I would see the {
duh
Try this
requests.each |item| do
if(FileTest.exist?(item))
if FileTest.directory?(item)?output << parse_Dir(item) : output
<<
parse_File(item)
else
output << parse_error(item)
end
end
As you accurately spotted, it’s with your if-statements.
On Jun 16, 2008, at 8:15 PM, Chance Dinkins wrote:
def parse_Requests(requests)
output = Array.new
requests.each{|item|
if(FileTest.exist?(item))
Starts an if
if FileTest.directory?(item)?output << parse_Dir(item) :
output <<
parse_File(item)
Starts an If
else
output << parse_error(item)
end
Ends an if
}
end
with this line here:
if FileTest.directory?(item)?output << parse_Dir(item) :
output <<
parse_File(item)
You are trying to do a ternary operator. however, you are starting it
with an if!
Try this: