[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

LSRC Name Picker

Ari Brown

7/3/2007 7:08:00 AM

Hey all.
I seem to get this error whenever I run my program.

-----------------------------------------------------------|
ERROR
-----------------------------------------------------------|
namePicker-cli.rb:58: syntax error, unexpected '{', expecting kEND
@names { |f| lines = f.readlines
^
namePicker-cli.rb:60: syntax error, unexpected '}', expecting kEND
-----------------------------------------------------------|
What does this mean? Here is my code:

-----------------------------------------------------------|
CODE
-----------------------------------------------------------|
###########################################
# Tally ho, I'm going to write this
# as best I can. Store the picked names
# to a file, and then that will be the checker.
###########################################

require 'ftools'
require 'rubygems'
require 'commandline/optionparser'
include CommandLine

##############################
# Set us up for CL options
##############################
od = OptionParser.new { |o|
o << Option.new(:flag,
:names => %w[--help -h],
:opt_description => "Prints this page."
)
o << Option.new(:names => %w[--file -f],
:arg_arity => [1,1],
:arg_description => "file",
:opt_found => OptionParser::GET_ARGS
)
}

op = od.parse

######################
# Set up some rules
######################
if op['--help']
puts od
exit()
end

#################
# Variables...
#################
FILE = op['--file']

#####################
# NamePicker Class
#####################
class NamePicker

def initialize
# names = open(FILE)
File.syscopy(FILE, '.names.txt')
@names = open('.names.txt', 'w+')
end

def random
selected = nil
@names.inject { |choice, line| selected < 1/quiz.lineno.to_f ?
line : choice }
if selected
puts selected.chomp
@names { |f| lines = f.readlines
### LINE 58
lines.puts unless $. == selected.chomp
}
### LINE 60
self.rewind
end

end
end

randomName = NamePicker.new
randomName.random
#end


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


4 Answers

Alex Gutteridge

7/3/2007 8:21:00 AM

0


On 3 Jul 2007, at 16:07, Ari Brown wrote:

> Hey all.
> I seem to get this error whenever I run my program.
>
> -----------------------------------------------------------|
> ERROR
> -----------------------------------------------------------|
> namePicker-cli.rb:58: syntax error, unexpected '{', expecting kEND
> @names { |f| lines = f.readlines
> ^
> namePicker-cli.rb:60: syntax error, unexpected '}', expecting kEND
> -----------------------------------------------------------|
> What does this mean? Here is my code:
>

snip

> @names { |f| lines =
> f.readlines ### LINE 58
> lines.puts unless $. == selected.chomp
> }
> ### LINE 60

Untested, but shouldn't this be

@names.each{ |f| lines =
f.readlines ### LINE 58
lines.puts unless $. == selected.chomp
}

Alex Gutteridge

Bioinformatics Center
Kyoto University



James Gray

7/3/2007 12:56:00 PM

0

On Jul 3, 2007, at 2:07 AM, Ari Brown wrote:

> I seem to get this error whenever I run my program.
>
> -----------------------------------------------------------|
> ERROR
> -----------------------------------------------------------|
> namePicker-cli.rb:58: syntax error, unexpected '{', expecting kEND
> @names { |f| lines = f.readlines
> ^
> namePicker-cli.rb:60: syntax error, unexpected '}', expecting kEND
> -----------------------------------------------------------|
> What does this mean?

It's Ruby's way of telling you that your code doesn't make sense.

Basically, you are defining a block for an instance variable here,
but only methods can take blocks in Ruby. I assume you meant to call
some iterator and forgot to include the method name.

Hope that helps.

James Edward Gray II


Ari Brown

7/3/2007 1:01:00 PM

0


On Jul 3, 2007, at 8:55 AM, James Edward Gray II wrote:
<snip>
>
> It's Ruby's way of telling you that your code doesn't make sense.
>
> Basically, you are defining a block for an instance variable here,
> but only methods can take blocks in Ruby. I assume you meant to
> call some iterator and forgot to include the method name.
I was trying this:
@names = open('.names.txt', 'w+')
in initialize, and then write to that file later on. Is there a
better way to do this?

> Hope that helps.
It did!
> James Edward Gray II

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



James Gray

7/3/2007 1:09:00 PM

0

On Jul 3, 2007, at 8:00 AM, Ari Brown wrote:

>
> On Jul 3, 2007, at 8:55 AM, James Edward Gray II wrote:
> <snip>
>>
>> It's Ruby's way of telling you that your code doesn't make sense.
>>
>> Basically, you are defining a block for an instance variable here,
>> but only methods can take blocks in Ruby. I assume you meant to
>> call some iterator and forgot to include the method name.
> I was trying this:
> @names = open('.names.txt', 'w+')
> in initialize, and then write to that file later on. Is there a
> better way to do this?

This is fine and in that case you don't need the block at all. You
can just write to the file with:

@names.puts "…"

James Edward Gray II