enumerable_methods's People
enumerable_methods's Issues
multiply_els is wrong
Enumerable_methods/enumberable.rb
Lines 161 to 163 in f4b482c
Solution:
def multiply_els(array)
array.my_inject { |total,value| total*value}
end
Remove `puts`
Please remove puts from this line of test code.
"multiply_els" is not a module function
Enumerable_methods/enumberable.rb
Lines 159 to 161 in 740979c
This is a simple method. Not a module method so it should be outside of the module.
my_inject still not behave properly
Enumerable_methods/enumberable.rb
Lines 144 to 157 in f4b482c
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
Enumerable_methods/spec/enumerable_spec.rb
Lines 10 to 14 in 79c7c21
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
Enumerable_methods/enumberable.rb
Lines 144 to 157 in 740979c
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:
For more ...
my_count should also work without block
Enumerable_methods/enumberable.rb
Lines 98 to 114 in f4b482c
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_any?` not behave properly
`my_count` method not working properly
Enumerable_methods/enumberable.rb
Lines 98 to 114 in 740979c
This should also work without a block
Hint: You have to return the size/length of the array.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.