r/learnruby • Beginner • Sep 16 '14

Trouble with scope in Ruby

Hi all,

I'm having some trouble understanding scope in ruby.

Here is a link to a repo if you'd like to download/run what I'm talking about to see for yourself:

https://github.com/minervadreaming/killshit

I have several .rb files present - specifically, I'm having an issue calling a class method from an instance. Seems like I'm not properly following scope, but I'm not sure how.

Here is a snippet from room.rb:

module KillShit
	class Room
		attr_reader :player, :monster, :game

		def initialize(player, monster, game)
			@player = player
			@monster = monster
			@game = game
		end

		def action
			outline
			Player.describe(player)
			vs
			Monster.describe(monster)
			outline

			#rolling a d20 to see who takes a turn
			turn = rand(1..100)

			if turn <= 20
				monster_attack
			else
				puts "What would you like to do?"
				puts "1. Attack!"
				puts "2. Defend!"
				puts "3. Run away!"
				#Give the player magic if they're at least level 2
				if player.maglevel >= 1
					puts "4. Cast spell"
				else
				end

				prompt; action = gets.chomp
		
				if action == "1"
					attack(player, monster)
				elsif action == "2"
					defend
				elsif action == "3"
					flee
				elsif action == "4" && player.maglevel >= 1
					magic
				else
					action
				end
			end
		end

		def magic
			puts "What magic would you like to cast?"
			if player.maglevel == 1
				puts "1. Heal"
				puts "2. Fireball"
				puts "3. Tremor"
				prompt; magic = gets.chomp

				if magic == "1"
					Spells.heal(player)
				elsif magic == "2"
					Spells.fireball(player, monster)
				elsif magic == "3"
					Spells.tremor(player, monster)
				else
					magic
				end
			elsif player.maglevel == 2
				puts "1. Greater Heal"
				puts "2. Firestorm"
				puts "3. Earthquake"
				prompt; magic = gets.chomp

				if magic == "1"
					Spells.greaterheal(player)
				elsif magic == "2"
					Spells.firestorm(player, monster)
				elsif magic == "3"
					Spells.earthquake(player, monster)
				else
					magic
				end
			else
			end
		end
	end
end

As you can see, when the player chooses to cast a spell it calls out to a Spells class, which I have in spells.rb. Here is a snippet from that code:

require_relative 'room'
require_relative 'player'
require_relative 'monster'

module KillShit
	class Spells
		attr_accessor :player, :monster

	#Checking if user has enough MP to cast spell
		def self.mp_check(req, player)
			req_mp = req
			if player.mp < req_mp
				puts "#{player.name} doesn't have enough MP!"
				action
			else
			end
		end

		def self.heal(player)
			req_mp = 3
			Spells.mp_check(req_mp, player)

			player.mp -= req_mp
			amt = rand(3..10)
			player.hp += amt
			puts "#{player.name} has been healed by #{amt} HP!"
			Room.action
		end
	end
end

The problem is in the "Room.action" call at the end of the self.heal method (or just "action" as was my first attempt, as can be seen in the self.mp_check method). When it is hit, the following error is received:

spells.rb:27:in `heal': undefined method `action' for KillShit::Room:Class (NoMethodError)

I thought that it might've been because I needed to define "action" as a class method with "self." but that isn't doing it.

Any ideas? What should I be reading up on to better understand the concept behind what's happening here?

Thanks!

3 Upvotes

6 comments sorted by

5

u/mCseq Sep 17 '14 edited Sep 17 '14

Copying "action" and defining it again as "self.action" does fix your issue, however, it's not a good way to go about it. Generally it's a good idea to try to avoid the use of class methods like this. You should really be instantiating an object of the class and using instance methods.

Edit: Also I'm seeing a lot of things like this in your code "Player.describe(player)" and "Monster.describe(monster)". These should look more like "@player.describe" and "@monster.describe".

1

u/MinervaDreaming Beginner Sep 17 '14 edited Sep 17 '14

Thank you for the feedback, just what I'm looking for.

Hmmmmm, interesting. Logically I understand what you're saying but I don't have a good understanding of how to go about refactoring in this manner - could you give me a resource to read up on what you're pointing out?

Edit: the "@player.describe" vs "Player.describe(player)" is making a bit more sense, now - these are instance variables and so I can use the single @ with them versus the way that I was doing them, yes?

But, the instantiating of the object of the class in my case is hard to wrap my head around.

The way I see it, the way I have it set up, is this:

  • run killshit.rb
  • player enters name, new object of class Player is instantiated (in cli.rb via player.rb)
  • new object of class Game is instantiated (in cli.rb via game.rb)

Then this is where it gets murky:

  • Now, I am not instantiating an object of the class Room - perhaps this is my mistake? The room, where most everything takes place, is instantiated with:

      	def new_room
      		Room.load_monster(player, self)
          end

Should it be more along the lines of

def new_room
     Room.new(player, self).load_monster
end

in order to actually instantiate a new object of the class Room to work from?

4

u/materialdesigner Sep 17 '14

Should it be more along the lines of

def new_room
  Room.new(player, self).load_monster
end

in order to actually instantiate a new object of the class Room to work from?

yep absolutely.

One of the really nice things about ruby is how well it reads as english, and you should be using your linguistic understanding to better dictate what the behavior of your various objects is.

For instance:

Spells.heal(player)

How does this read in a sentence? "Spells heal player" How about instead

player.heal

"Player heal". Imagine you extend this game to allow players to heal other players.

Spell.heal(from_player, to_player)

# vs

player.heal(other_player)

Which of these reads better?

In your code I see a lot of what has been termed Anemic Domain Model where your classes are nothing more than glorified bags of getters and setters. Your classes are big boxes with labeled shelves inside where you can grab and put down attributes, but the classes have little or no behavior.

A general guideline to follow is to minimize the number of class methods, e.g. Spells.heal, Room.load_monster, Spells.mp_check, Player.describe. Because generic Spells is not an object. a Spells is not a thing. Once you instantiate a spell, that is an object -- that is a spell.

You should be thinking of: "what objects does it make sense to have these behaviors?"

For instance, your mp_check stuff. How about instead, for instance

class Spells
   def initialize(name, req_level, mp_cost)
     @name = name
     @req_level = req_level
     @mp_cost = mp_cost
  end

  attr_reader :name, :req_level, :mp_cost
end

class Player
  # ... other methods, including initializer
  # def magic_level; end
  # def remaining_mp; end

  def can_cast?(spell)
    (magic_level >= spell.req_level) && (remaining_mp >= spell.mp_cost)
  end
end

1

u/MinervaDreaming Beginner Sep 17 '14

Thank you! You've been a great help - this perspective shift makes sense, and I'm going to work on refactoring to take it into account.

2

u/materialdesigner Sep 17 '14

No problem! Glad you found some use. I've got a list of really good Ruby and Dev resources on my website if you're looking for things to read.

1

u/MinervaDreaming Beginner Sep 18 '14

Bookmarked, thanks!