Question about Ruby syntax

C

Chance Dinkins

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..

Thanks for the help!
 
C

Chance Dinkins

Apparently I have to end the if [condition] ? [true] : [false] with an
end? that seems strange.
 
R

Ruby Freak

Get rid of the } between the end and end. It has no starting {

Read David A Black's book: "Ruby for Rails". It is very good, just
ignore the sample code in chapter 1
 
R

Ruby Freak

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
 
F

fedzor

As you accurately spotted, it's with your if-statements.

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:

FileTest.directory?(item) ? output << parse_Dir(item) : output <<
parse_File(item)


Feel free to put in spaces (the code looked convoluted, at least to
me), and you don't need all those parenthesis!

Lose the shoes, and try on some (ruby) slippers!

ari
--------------------------------------------|
If you're not living on the edge,
then you're just wasting space.
 
F

fedzor

I guess if I read the code I would see the {
duh
Try this

requests.each |item| do
requests.each do |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 still, the problem lies with the if in the ternary operator.
Changing the block style won't do anything.

-------------------------------------------------------|
~ Ari
Some people want love
Others want money
Me... Well...
I just want this code to compile
 
P

Peña, Botp

From: fedzor [mailto:[email protected]]=20
# On Jun 16, 2008, at 8:15 PM, Chance Dinkins wrote:
# > def parse_Requests(requests)
# > output =3D Array.new
# > requests.each{|item|
# > if(FileTest.exist?(item))
# Starts an if
# > if FileTest.directory?(item)?output << parse_Dir(item) : =20
# > output <<
# > parse_File(item)
# Starts an If
# > else
# > output << parse_error(item)
# > end
# Ends an if
# > }
# > end
#=20
# with this line here:
# > if FileTest.directory?(item)?output << parse_Dir(item) : =20
# > output <<
# > parse_File(item)
#=20
# You are trying to do a ternary operator. however, you are=20
# starting it =20
# with an if!
# Try this:
#=20
# FileTest.directory?(item) ? output << parse_Dir(item) : output << =20
# parse_File(item)

also the chevrons are leading to the output, so,

output << if(FileTest.exist?(item))
FileTest.directory?(item) ?
parse_Dir(item) : parse_File(item)
else
parse_error(item)
end


kind regards -botp
 
C

Chance Dinkins

Going to make a general response, thanks! I love knowing that there are
so many variations in syntax, it provides a lot of flexability.


Thanks a lot guys,
Chance
 
F

fedzor

Get rid of the } between the end and end. It has no starting {

BUt there is! Look very carefully on the first line of the code.

~ Ari
English is like a pseudo-random number generator - there are a
bajillion rules to it, but nobody cares.
 
M

Marc Heiler

Also, I think instead of

if(FileTest.exist?(item))

you should write

if FileTest.exist?(item)

It's just easier to read without the ()
 

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
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top