How to run a ruby script

class Dungeonapp
attr_accessor :player

def initialize(player_name)
@player = Player.new(player_name)
@rooms = []
end

def add_room(reference, name, description, connections)
@rooms << Room.new(reference, name, description, connections)
end

def start(location)
@player. location = location
show_current_description
end

def show_current_description
puts find_room_in_dungeon(@player.location).full_description
end

def find_room_in_dungeon(reference)
@rooms.detect { |room| room.reference == reference }
end

def find_room_in_direction(direction)
find_room_in_dungeon(@player.location).connections[direction]
end

def go(direction)
puts "You go " + direction.to_s
@player.location = find_room_in_direction(direction)
show_current_description
end

class Player
attr_accessor :name, :location

def initialize(name)
  @name = name
end

end

class Room
attr_accessor :reference, :name, :description, :connections

def initialize(reference, name, description, connections)
  @reference = reference
  @name = name
  @description = description
  @connections = connections
end

def full_description
  @name + "\n\nYou are in " + @description
end

end
end

this is my dungeonapp.rb file.I am using Ubuntu.Now how to execute this
code :

my_dungeon = Dungeon.new(“Fred Bloggs”)
my_dungeon.add_room(:largecave, “Large Cave”, “a large cavernous cave”,
{
:west => :smallcave })
my_dungeon.add_room(:smallcave, “Small Cave”, “a small, claustrophobic
cave”, {
:east => :largecave })

Question:
1.How to run this code from out of the file,like we do in java.Do I need
to go to irb prompt or usual command prompt for firing this code out of
the file.
2.When i copy the code of this file and directly paste on the irb prompt
it automatically add prefix “dungeonapp.rb” in each line! what is the
reason behind this.

Thank you guys.I need urgent help.

Well I took you code and pasted it into a file and went

$ ruby dungeonapp.rb

It works for me and should work for you.

However the is a bug. The class is called Dungeonapp but you then do
this

my_dungeon = Dungeon.new(“Fred Bloggs”)

Use only one name or the other.

Also I updated the end bit to

my_dungeon.start(:largecave)
while true
print "go where? "
go_to = STDIN.gets.chomp
my_dungeon.go(go_to)
end

and had to change this

def find_room_in_direction(direction)
find_room_in_dungeon(@player.location).connections[direction.to_sym]
end

Note the to_sym on the end. You cannot type a symbol in interactively so
you need to convert it.

A good start, keep it up.

Peter H. wrote in post #1090473:

Well I took you code and pasted it into a file and went

$ ruby dungeonapp.rb

It works for me and should work for you.

However the is a bug. The class is called Dungeonapp but you then do
this

my_dungeon = Dungeon.new(“Fred Bloggs”)

Use only one name or the other.

Also I updated the end bit to

my_dungeon.start(:largecave)
while true
print "go where? "
go_to = STDIN.gets.chomp
my_dungeon.go(go_to)
end

and had to change this

def find_room_in_direction(direction)
find_room_in_dungeon(@player.location).connections[direction.to_sym]
end

Note the to_sym on the end. You cannot type a symbol in interactively so
you need to convert it.

A good start, keep it up.

Thank you for that.

but we do ruby dungeonapp.rb to execute the file but how to execute the
code out of the file? if you can online i want to chat with you.Thank
You

On Fri, Dec 28, 2012 at 10:17 AM, Peter H.
[email protected] wrote:

my_dungeon.start(:largecave)
It will load the Dungeon class from dungeon.rb and run it (providing it is
in the same directory as play.rb).

As was recommended to me previously, I’d suggest looking at GLI and
Methadone to create command line applications, which it looks like
your adventure game is. (RIght on! Good start!)

Likewise, the RSpec book, which is quite a bit about test/behaviour
driven development, uses a game application as a case study. Working
your way through that will be of double benefit.

Well you could just place all the code between

class Dungeon
… and …
end

into a file called dungeon.rb and create a new file called play.rb

– play.rb
#!/usr/bin/env ruby

require ‘dungeon’

my_dungeon = Dungeon.new(“Fred Bloggs”)
my_dungeon.add_room(:largecave, “Large Cave”, “a large cavernous cave”,
{
:west => :smallcave, :east => :kitchen })
my_dungeon.add_room(:smallcave, “Small Cave”, “a small, claustrophobic
cave”, { :east => :largecave })
my_dungeon.add_room(:kitchen, “Kitchen”, “a small kitchen”, {:west =>
:largecave})

my_dungeon.start(:largecave)
while true
print "go where? "
go_to = STDIN.gets.chomp
my_dungeon.go(go_to)
end

now all you need to run it is

$ ruby play.rb

It will load the Dungeon class from dungeon.rb and run it (providing it
is
in the same directory as play.rb).

Peter H. wrote in post #1090491:

Well you could just place all the code between

class Dungeon
… and …
end

into a file called dungeon.rb and create a new file called play.rb

– play.rb
#!/usr/bin/env ruby

require ‘dungeon’

my_dungeon = Dungeon.new(“Fred Bloggs”)
my_dungeon.add_room(:largecave, “Large Cave”, “a large cavernous cave”,
{

Thank You I got it!

tamouse mailing lists wrote in post #1090547:

On Fri, Dec 28, 2012 at 10:17 AM, Peter H.
[email protected] wrote:

my_dungeon.start(:largecave)
It will load the Dungeon class from dungeon.rb and run it (providing it is
in the same directory as play.rb).

As was recommended to me previously, I’d suggest looking at GLI and
Methadone to create command line applications, which it looks like
your adventure game is. (RIght on! Good start!)

Likewise, the RSpec book, which is quite a bit about test/behaviour
driven development, uses a game application as a case study. Working
your way through that will be of double benefit.

Ohk I got it Thank You !