Skip to main content
deleted 6 characters in body
Source Link
TenJack
  • 141
  • 4

I've implemented a working version of Array#uniq in Ruby to answer the question here: http://www.rubeque.com/problems/like-a-snowflake, but I'd like to see if there's ways to clean up my code or some better practices I should follow? This is my version:

class Array
  def uniq(&block)
    @objsobjs = []
    @yield_valsyield_vals = []
    self.each do |obj|
      yield_val = yield obj
      unless @yield_valsyield_vals.include?(yield_val)
        @yield_valsyield_vals.push(yield_val)
        @objsobjs.push(obj)
      end
    end
    @objsobjs
  end
end

I've implemented a working version of Array#uniq in Ruby to answer the question here: http://www.rubeque.com/problems/like-a-snowflake, but I'd like to see if there's ways to clean up my code or some better practices I should follow? This is my version:

class Array
  def uniq(&block)
    @objs = []
    @yield_vals = []
    self.each do |obj|
      yield_val = yield obj
      unless @yield_vals.include?(yield_val)
        @yield_vals.push(yield_val)
        @objs.push(obj)
      end
    end
    @objs
  end
end

I've implemented a working version of Array#uniq in Ruby to answer the question here: http://www.rubeque.com/problems/like-a-snowflake, but I'd like to see if there's ways to clean up my code or some better practices I should follow? This is my version:

class Array
  def uniq(&block)
    objs = []
    yield_vals = []
    self.each do |obj|
      yield_val = yield obj
      unless yield_vals.include?(yield_val)
        yield_vals.push(yield_val)
        objs.push(obj)
      end
    end
    objs
  end
end
Source Link
TenJack
  • 141
  • 4

Implementing Array#uniq in Ruby

I've implemented a working version of Array#uniq in Ruby to answer the question here: http://www.rubeque.com/problems/like-a-snowflake, but I'd like to see if there's ways to clean up my code or some better practices I should follow? This is my version:

class Array
  def uniq(&block)
    @objs = []
    @yield_vals = []
    self.each do |obj|
      yield_val = yield obj
      unless @yield_vals.include?(yield_val)
        @yield_vals.push(yield_val)
        @objs.push(obj)
      end
    end
    @objs
  end
end