[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Question about Ruby syntax

Chance Dinkins

6/17/2008 12:16:00 AM

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

9 Answers

Chance Dinkins

6/17/2008 12:27:00 AM

0

Apparently I have to end the if [condition] ? [true] : [false] with an
end? that seems strange.
--
Posted via http://www.ruby-....

Ruby Freak

6/17/2008 12:31:00 AM

0


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

Ruby Freak

6/17/2008 12:34:00 AM

0

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

fedzor

6/17/2008 12:39:00 AM

0

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:

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.



fedzor

6/17/2008 12:40:00 AM

0


On Jun 16, 2008, at 8:34 PM, Ruby Freak 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


Peña, Botp

6/17/2008 12:59:00 AM

0

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

Chance Dinkins

6/17/2008 3:04:00 PM

0

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

fedzor

6/17/2008 7:08:00 PM

0


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

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


Marc Heiler

6/18/2008 9:25:00 AM

0

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