Code Monkey home page Code Monkey logo

enumerable_methods's People

Contributors

danielmitiku avatar

Stargazers

 avatar

Watchers

 avatar

enumerable_methods's Issues

my_inject still not behave properly

def my_inject
total = 0
if self.class == Array
self.my_each do |value|
total = yield(total, value)
end
elsif self.class == Hash
self.my_each do |key, value|
total = yield(total, value)
end
else
end
total
end

Possible example solution:

def my_inject(arg = nil)
    arr = self.class == Array ? self.clone : self.class == Range ? self.to_a : self.my_map {|k, v| v }
    acc = arg == nil ? arr[0] : arg
    nxt = arg == nil ? arr[1] : arr[0]
    i = arg == nil ? 1 : 0
    while i < arr.size do
      acc = yield(acc,nxt)
      nxt = arr[i+1]
      i+=1
    end
    acc
  end

This test is not perfect

describe "#my_each_with_index" do
it "takes a block with element and index" do
expect([1,2,3,4].my_each_with_index{|x,i| i}).to eql((0...4))
end
end

First of all, you can't use a range like this (0...4) You have to convert it to an array using this to_a.
For testing index, it will be better if you take an empty array to store the array index and test that new fillup with index array. This test should like this

describe '#my_each_with_index' do
    it 'takes a block with element and index' do
      arr_index = []
      [1, 2, 3, 4].my_each_with_index { |_x, i| arr_index.push(i) }
      expect(arr_index).to eql((0...4).to_a)
    end
  end

#my_inject Method not working properly

def my_inject(x)
total = x
if self.class == Array
self.my_each do |value|
total = yield(total, value)
end
elsif self.class == Hash
self.my_each do |key, value|
total = yield(total, value)
end
else
end
total
end

This method only works if it calls like this puts ar.my_inject(0) { |acc, nxt| acc + nxt }. But it should also work if it calls puts ar.my_inject { |acc, nxt| acc + nxt }

Hint:
2019-04-30_224149
For more ...

my_count should also work without block

def my_count
count = 0
if self.class == Array
self.my_each do |value|
if yield(value)
count += 1
end
end
elsif self.class == Hash
self.my_each do |key, value|
if yield(key, value)
count += 1
end
end
end
count
end

Solution:

def my_count
      if block_given?
        count = 0
        if self.class == Array
          self.my_each do |value|
            if yield(value)
              count += 1
            end
          end
        elsif self.class == Hash
          self.my_each do |key, value|
            if yield(key, value)
              count += 1
            end
          end
        end
        count
      else
        self.size
      end
    end

This solution is based on your code.

`my_count` method not working properly

def my_count
count = 0
if self.class == Array
self.my_each do |value|
if yield(value)
count += 1
end
end
elsif self.class == Hash
self.my_each do |key, value|
if yield(key, value)
count += 1
end
end
end
count
end

This should also work without a block

Hint: You have to return the size/length of the array.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.