lundi 24 octobre 2016

Can I call a method inside another method

I am trying to add a tractor_beam instance method that takes a string description of an item as a parameter (e.g., "cow"). When called, the method should disable the shield, add the item to the inventory along with the ship's current location. I keep getting an error message when I run the tractor beam, is it possible to run the disable_shield inside the tractor_beam?

Here is the class I created:

class Spaceship
attr_accessor :name, :location, :item, :inventory
attr_reader :max_speed
  def initialize (name, max_speed, location, item)
    puts "Initializing new Spaceship"
    @name = name
    @max_speed = max_speed
    @location = location
    @item = item
    @inventory = {}
   end

  def disable_shield
    puts "Shield is off!"
   end

  def enable_shield
    puts "Shield is on!"
   end

  def warp_to(location)
     puts "Traveling at #{max_speed} to #{location}!"
     @location = location
   end

  def tractor_beam(item)
    disable_shield
    @inventory[@location] = item
   end

 end

Aucun commentaire:

Enregistrer un commentaire