Newbie needs help with first project

D

Daniel Dale

I was told about Ruby several weeks ago and started my journey lol. I've
read several tutorials but reading the tutorials I still couldn't grasp
most of it and I thought finding a project I wanted to work on and
just diving in might be the best bet.

I want to create a program that I can enter my game collection into.
Ideally I want it to have a simple gui so others can use it and input
their games as well.

Here's the basic code I have I keep getting an error stating
uninitialized constant Game (NameError)

code:

class Games
def
initialize(title,platform,genre,rating,published_by,developed_by,year,condition)
@title=title
@platform=platform
@genre=genre
@rating=rating
@published_by=published_by
@developed_by=developed_by
@year=year
@condition=condition
end
end



class Games
def to_s
"Games:
#{@title}--#{@platform}--#{@genre}--#{@rating}--#{@published_by}--#(@developed_by)--#(@year)--#(@condition)"
end
end

game1=Game.new("FireProReturns","Playstation_2","Sports","Teen","Agetec_Inc.","Very_Good")
game1.to_s

Any help tips and etc on how to fix my problem and proceed will be
greatly appreciated.
 
V

Vikhyat Korrapati

I think your problem is that the class is named 'Games' but you are
initializing it by calling Game.new. Try changing 'Games' in the class
definition to 'Game', it should work then.
 
S

Stefan Codrescu

[Note: parts of this message were removed to make it a legal post.]

I was told about Ruby several weeks ago and started my journey lol. I've
read several tutorials but reading the tutorials I still couldn't grasp
most of it and I thought finding a project I wanted to work on and
just diving in might be the best bet.

I want to create a program that I can enter my game collection into.
Ideally I want it to have a simple gui so others can use it and input
their games as well.

Here's the basic code I have I keep getting an error stating
uninitialized constant Game (NameError)

code:

class Game
def

initialize(title,platform,genre,rating,published_by,developed_by,year,condition)
@title=title
@platform=platform
@genre=genre
@rating=rating
@published_by=published_by
@developed_by=developed_by
@year=year
@condition=condition
end

def to_s
"Games:

#{@title}--#{@platform}--#{@genre}--#{@rating}--#{@published_by}--#(@developed_by)--#(@year)--#(@condition)"
end
end


game1=Game.new("FireProReturns","Playstation_2","Sports","Teen","Agetec_Inc.","Very_Good")
game1.to_s

Any help tips and etc on how to fix my problem and proceed will be
greatly appreciated.
def
initialize(title,platform,genre,rating,published_by,developed_by,year,condition)
@title=title
@platform=platform
@genre=genre
@rating=rating
@published_by=published_by
@developed_by=developed_by
@year=year
@condition=condition
end

def to_s
puts "Games:
#{@title}--#{@platform}--#{@genre}--#{@rating}--#{@published_by}--#{@developed_by}--#{@year}--#{@condition}"
end
end

game1=Game.new("FireProReturns","Playstation_2","Sports","Teen",
"Good","Agetec_Inc.","2000","Very_Good")
game1.to_s


Is what I'm guessing you should have.
You can put both of the methods in the same class.
You forgot the 'puts' before the Games: in the to_s method
Also uh to print a variable you have to have #{var} not #(var)
and when you had the game1=Game.new you were missing two of your arguments
hehe I just made them up.
And as Vikhyat said you needed to name the class Game for your
initialization to work or you can keep the class name Games but put the
other one to Games too.
Running the code above should give you

Games:
FireProReturns--Playstation_2--Sports--Teen--Good--Agetec_Inc.--2000--Very_Good
 
T

Tim Hunter

Stefan said:
def to_s
puts "Games:
#{@title}--#{@platform}--#{@genre}--#{@rating}--#{@published_by}--#{@developed_by}--#{@year}--#{@condition}"
end
end

game1=Game.new("FireProReturns","Playstation_2","Sports","Teen",
"Good","Agetec_Inc.","2000","Very_Good")
game1.to_s


Is what I'm guessing you should have.
You can put both of the methods in the same class.
You forgot the 'puts' before the Games: in the to_s method

No, Daniel had it right. A class's #to_s method is expected to _return_
a string, not print a string. If you want to print the string
representation of a Game object, you would use

game1 = Game.new(....)
puts game1.to_s
 
D

Daniel Dale

Thanks for all the help. I appreciate it. just had a thought though.
continuing the way I'm doing it now all the games I enter will be lost
when I close the program right ?

is there a way i can code it so the user is asked for the
title,rating,condition and etc of a game. when they put it in is
stored in a file with the rest of the games they input.
then they can sort them or list them by rating,platform,or genre.
 
S

Stefan Codrescu

[Note: parts of this message were removed to make it a legal post.]

Yes there is a way to do that...

class Game
def
initialize(title,platform,genre,rating,published_by,developed_by,year,condition)
@title=title
@platform=platform
@genre=genre
@rating=rating
@published_by=published_by
@developed_by=developed_by
@year=year
@condition=condition
@my_file =
File.new("/Users/Stefan/Ruby/yourfileorthenameofthefileyouwanttocreate.txt",
'a+')
end

def to_s
puts "Games:

#{@title}--#{@platform}--#{@genre}--#{@rating}--#{@published_by}--#{@developed_by}--#{@year}--#{@condition}"
end
def savedata
@my_file.puts "Games:

#{@title}--#{@platform}--#{@genre}--#{@rating}--#{@published_by}--#{@developed_by}--#{@year}--#{@condition}"
end
def printdata
file =
File.open("/Users/Stefan/Ruby/yourfileorthenameofthefileyouwanttocreate.txt")
file.each {|info| print info}
end
end

game1=Game.new("
FireProReturns","Playstation_2","Sports","Teen",
"Good","Agetec_Inc.","2000","Very_Good")
game1.to_s
game1.savedata
game1.printdata



the a+ means that it will create a new file and write to it or it will add
on to the end of that file if it already exists.
and I also added a method that will print the file that you made.
also note that since the file is in the a+ mode if you run the program 5
times for example you will have five entries even if they are all the same.
 
P

Pierre Pierre

You could eventually use YAML or XML to store your data and get it back
later the way you want.
 
D

Daniel Dale

thanks for the example Stefan. it doesn't seem to ask the user to
input the information though but I'll be sitting down looking through
each line . great example to learn form so thanks for that.

Pierre , I've seen YAML mentioned else where but I haven't been able
to find any good documentation or tutorials on how to use it with ruby.
 
P

Phlip

Daniel said:
Pierre , I've seen YAML mentioned else where but I haven't been able
to find any good documentation or tutorials on how to use it with ruby.

That's syck!
 
E

Eric Jacoboni

Daniel Dale said:
Pierre , I've seen YAML mentioned else where but I haven't been able
to find any good documentation or tutorials on how to use it with ruby.


What's wrong with "ri YAML" ?
 
S

Stefano Crocco

Alle Monday 23 March 2009, Eric Jacoboni ha scritto:
What's wrong with "ri YAML" ?

In my opinion, it's not very informative. True, it does have a (very minimal)
example, but most of it is either very generic (the first part) or mostly
suited for people who already know how YAML works (the second part).

As for tutorials on YAML and ruby, you can look at the YAML for ruby cookbook
(http://www.yaml.org/YAML_for_ruby.html). I found it extremely useful while
learning YAML. Also, YAML in Five Minutes
(http://yaml.kwiki.org/index.cgi?YamlInFiveMinutesMinuteOne), which is
mentioned in ri YAML, can be useful.

Stefano
 
B

Brian Candler

Daniel said:
Thanks for all the help. I appreciate it. just had a thought though.
continuing the way I'm doing it now all the games I enter will be lost
when I close the program right ?

Yep. In addition to the options already listed:

- Marshal.dump and Marshal.load will store an arbitrary graph of Ruby
objects (that is, the object plus all the objects it refers to) to an
opaque binary file. Some objects cannot be Marshaled - e.g. File/IO
objects, procs, and any object with singleton methods

- There are plenty of libraries which will store your objects in a SQL
database, the most popular being ActiveRecord, but there are many other
options including Sequel and DataMapper.

In this case, you need not define attributes in your class at all,
because they are added at runtime based on the columns in the database.
That is,

ActiveRecord::Base.establish_connection(
... database connection params ...
)
class Game < ActiveRecord::Base; end

is all you need to access a table called 'games'.

Of course, sorting and searching are bread-and-butter to a SQL database.

HTH,

Brian.
 
S

Stefan Codrescu

[Note: parts of this message were removed to make it a legal post.]

OH... If you want the user to enter something you can have something like

puts "Enter the name of the game:"
@name = gets

and so on for each variable
 
D

Daniel Dale

thanks again . I'll look into those links about YAML

here's what I got so far. the input works correctly but I still can't
get it to save the info that I input into a file

class Game
def
initialize(title,platform,genre,rating,published_by,developed_by,year,condition)
@title=title
@platform=platform
@genre=genre
@rating=rating
@published_by=published_by
@developed_by=developed_by
@year=year
@condition=condition
@my_file= File.new("C:\ruby\gamesdb.txt",'a+')
end


puts "Enter the name of the game:"
@title = gets
@title.to_s


puts "Enter the platform:"
@platform = gets
@platform.to_s


puts "Enter the genre:"
@genre = gets
@genre.to_s

puts "Enter Rating:"
@rating=gets
@rating.to_s

puts "Published by:"
@published_by = gets
@published_by.to_s

puts "Developed by:"
@developed_by= gets
@developed_by.to_s

puts "What Year was it released?:"
@year= gets
@year.to_s

puts "What condition is it in?:"
@condition= gets
@condition.to_s

def to_s
puts
"Games:#{@title}--#{@platform}--#{@genre}--#{@rating}--#{@published_by}--#{@developed_by}--#{@year}--#{@condition}"
end
def savedata
@my_file.puts
"Games:#{@title}--#{@platform}--#{@genre}--#{@rating}--#{@published_by}--#{@developed_by}--#{@year}--#{@condition}"
end
def printdata
file = File.open("C:\ruby\gamesdb.txt")
file.each {|info| print info}
end

end
 
H

Heesob Park

2009/3/24 Daniel Dale said:
thanks again . I'll look into those links about YAML

here's what I got so far. the input works correctly but =C2=A0I still can= 't
get it to save the info that I input into a file

class Game
=C2=A0 =C2=A0def
=C2=A0initialize(title,platform,genre,rating,published_by,developed_by,ye= ar,condition)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0@title=3Dtitle
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0@platform=3Dplatform
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0@genre=3Dgenre
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0@rating=3Drating
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0@published_by=3Dpublished_by
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0@developed_by=3Ddeveloped_by
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0@year=3Dyear
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0@condition=3Dcondition
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0@my_file=3D File.new("C:\ruby\ga= mesdb.txt",'a+')
end


puts "Enter the name of the game:"
=C2=A0@title =3D gets
[email protected]_s


puts "Enter the platform:"
=C2=A0@platform =3D gets
[email protected]_s


puts "Enter the genre:"
=C2=A0@genre =3D gets
[email protected]_s

puts "Enter Rating:"
=C2=A0@rating=3Dgets
[email protected]_s

puts "Published by:"
=C2=A0@published_by =3D gets
=C2=A0@published_by.to_s

puts "Developed by:"
=C2=A0@developed_by=3D gets
=C2=A0@developed_by.to_s

puts "What Year was it released?:"
=C2=A0@year=3D gets
[email protected]_s

puts "What condition is it in?:"
=C2=A0@condition=3D gets
[email protected]_s

def to_s
=C2=A0 =C2=A0 =C2=A0 =C2=A0 puts
"Games:#{@title}--#{@platform}--#{@genre}--#{@rating}--#{@published_by}--= #{@developed_by}--#{@year}--#{@condition}"
=C2=A0 =C2=A0 end
=C2=A0 =C2=A0def savedata
=C2=A0 =C2=A0 =C2=A0 =C2=A0@my_file.puts
"Games:#{@title}--#{@platform}--#{@genre}--#{@rating}--#{@published_by}--= #{@developed_by}--#{@year}--#{@condition}"
=C2=A0 =C2=A0end
=C2=A0 =C2=A0def printdata
=C2=A0 =C2=A0 =C2=A0 =C2=A0file =3D File.open("C:\ruby\gamesdb.txt")
=C2=A0 =C2=A0 =C2=A0 =C2=A0file.each {|info| print info}
=C2=A0 =C2=A0end

end
--
The pathname "C:\ruby\gamesdb.txt" should be
"C:\\ruby\\gamesdb.txt" or "C:/ruby/gamesdb.txt"

Regards,

Park Heesob
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top