Question about Ruby syntax

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!

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

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

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.

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.

···

On Jun 16, 2008, at 8:15 PM, Chance Dinkins wrote:

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

···

On Jun 16, 2008, at 8:34 PM, Ruby Freak wrote:

# 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

···

From: fedzor [mailto:fedzor@gmail.com]
#
# 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)

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

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.

···

On Jun 16, 2008, at 8:34 PM, Ruby Freak wrote:

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

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

···

--
Posted via http://www.ruby-forum.com/.

Also, I think instead of

    if(FileTest.exist?(item))

you should write

    if FileTest.exist?(item)

It's just easier to read without the ()

···

--
Posted via http://www.ruby-forum.com/.