SG17 SG17 - 1 month ago 12
Ruby Question

Wrong return value when calling a method

I need 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 if it isn't too heavy to pick up (see algorithm below), enable the shield again, and return true. If the item is too heavy to pick up, the method should skip the inventory update and return false.

Algorithm:


An item is too heavy to pick up if its letters add up to more than 500. using
.ord
(Not very scientific, i know.) For example, the letters of cow add up to 329, so our tractor beam can abduct a cow, no problem.

My problem is that it returns nil and an empty hash, how do i break down the item to add each together?

Code:

class Spaceship
attr_accessor :name, :location, :item, :inventory
attr_reader :max_speed

def initialize (name, max_speed, location)
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

item = item.split('')

item.each do |let|
let.ord
let + let.next
end

return item

if item > 500
enable_shield
@inventory[@location] = item
return true
else
return false
end
end
end


Driver Code:

uss_enterprise = Spaceship.new("USS Enterprise","200,000 mph", "China")

hms_anfromeda = Spaceship.new("HMS Andromeda", "108,277 mph", "China")

uss_enterprise.disable_shield
hms_anfromeda.enable_shield

p hms_anfromeda.location

hms_anfromeda.warp_to("Namibia")

p hms_anfromeda.location

hms_anfromeda.tractor_beam("cow")

p hms_anfromeda.item


Terminal:

Initializing new Spaceship
Initializing new Spaceship
Shield is off!
Shield is on!
"China"
Traveling at 108,277 mph to Namibia!
"Namibia"
Shield is off!
nil

Answer

Firstly, you have a return statement before your if conditional, so the conditional will never be ran. Remove that. Secondly, you get the weight of the item by using ord, but you aren't assigning the value to anything:

item.each do |let|
  let.ord
  let + let.next
end

return item

if item > 500

This should do the trick:

item = item.split('')
weight = 0

item.each do |let|
  weight += let.ord # add the ord of this letter to the weight
end

if weight > 500 # weight is now the ord of each letter of item 'cow'
  enable_shield
  @inventory[@location] = item
  return true
else
  return false
end
Comments