0

I'm trying to write a function that will remove an array from a 3d array where an array has a matching String name.

before(:each) do
  @topic1 = Topic.new 4,'topic 1'
  @topic2 = Topic.new 7,'topic 2'
  @topic3 = Topic.new 5,'topic 3'
  @subject = Subject.new 'Module 1',2,5
end

parameters are No_of_Lectures and Name

@topics = [5,'Topic1'], [3,'Topic2'], [5,'Topic3']  

Basically I'd like to remove the array where Name = 'Topic1' or return null if it's not in the list.

What I have so far is

def findTopic name
  @topics.find {|topic| topic.name == name }
end


def removeTopic name_in
  if @topics.findTopic(name)
    @topics.delete_if {|key, name| name == name_in } 
    topic
  else
    null
  end
end
6
  • 1
    What's the specific issue? That's how you delete an item from an array. Commented Nov 11, 2015 at 22:03
  • What about everything else? Would this function return the topic removed if it is removed and return null if its not in the list? Commented Nov 11, 2015 at 22:18
  • topic isn't defined in the method, so it wouldn't even run (unless it's a method as well). Commented Nov 11, 2015 at 22:53
  • Sorry didnt include that part. So are we good to go with rspec testing now? The function seems like its ok? Commented Nov 11, 2015 at 22:59
  • Probably should have been worrying the test first. But sure-if you're testing correctly you'll know if it's OK-that's kind of the point. Commented Nov 11, 2015 at 23:48

1 Answer 1

3

a 3d array

Your arrays are two dimensional.

return null if its not in the list.

null doesn't exist in ruby--there is nil, though.

def remove(target, array2D)

  results = array2D.reject do |arr|
    arr.last == target
  end

  results.size == array2D.size ? nil : results
end

test_arrays = [
  [[5,'Topic1'], [3,'Topic2'], [5,'Topic3']],
  [[5,'Topic4'], [3,'Topic5'], [5,'Topic6']]
]

test_arrays.each do |array2D|
  p remove('Topic2', array2D)
end

--output:--
[[5, "Topic1"], [5, "Topic3"]]
nil

On the other hand, a solution for this question:

remove a topic from the list. Returns the topic removed; otherwise, return null when topic is not currently in the list.

is:

def remove(target, array2D)

  array2D.each_with_index do |arr, index|
    if arr.last == target
      array2D.delete_at(index)
      return arr
    end
  end

  return nil

end

test_arrays = [
  [[5,'Topic1'], [3,'Topic2'], [5,'Topic3']],
  [[5,'Topic4'], [3,'Topic5'], [5,'Topic6']]
]

test_arrays.each do |array2D|
  p remove('Topic2', array2D)
end

--output:--
[3, "Topic2"]
nil
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks can you explain what the results = line does?
While true, this doesn't really address the original issue; using delete should work properly all other things being equal. Whether or not it's a good idea is a separate discussion!
@ChrisMaher, That assigns the return value of the method to the variable results.
@Dave. Thanks @ 7Stud Strange hearing null doesnt exist. Im following a tutorial and this is the exercise at the end. Heres the instructions. pastebin.com/g49YWGnL
@ChrisMaher It's sort of the same thing; Ruby's equivalent. One difference is that nil is a class in Ruby, NilClass, which is useful. Is this really a Ruby exercise? If so, it's too bad they didn't follow Ruby naming conventions (snake_case, not camelCase). I'd recommend changing the names to more Ruby-like ones.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.