Skip to content

Instantly share code, notes, and snippets.

@AlexB52
Created February 1, 2023 00:48
Show Gist options
  • Save AlexB52/9fe8329f5dadaed400c65f2e26a91ae8 to your computer and use it in GitHub Desktop.
Save AlexB52/9fe8329f5dadaed400c65f2e26a91ae8 to your computer and use it in GitHub Desktop.
Difference between Enumerable and Grizzly::Enumerable
class List
include Enumerable
attr_reader :items
def initialize(items)
@items = items
end
def a_thing_to_do
"calling a_thing_to_do"
end
def each
return to_enum unless block_given?
items.each { yield _1 }
end
end
list = List.new([1, 2, 3, 4, 5, 6, 7, 8, 9])
puts list.a_thing_to_do
begin
list.select(&:even?).a_thing_to_do
rescue NoMethodError => e
puts e.message
end
new_items = list.select(&:even?)
puts new_items.class
puts List.new(new_items).a_thing_to_do
# calling a_thing_to_do
# undefined method `a_thing_to_do' for [2, 4, 6, 8]:Array
# list.select(&:even?).a_thing_to_do
# ^^^^^^^^^^^^^^
# Array
# calling a_thing_to_do
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'grizzly-rb', require: 'grizzly'
end
class List
include Grizzly::Enumerable
attr_reader :items
def initialize(items)
@items = items
end
def a_thing_to_do
"calling a_thing_to_do"
end
def each
items.each
end
end
list = List.new([1, 2, 3, 4, 5, 6, 7, 8, 9])
puts list.a_thing_to_do
puts list.select(&:even?).a_thing_to_do
new_items = list.select(&:even?)
puts new_items.class
puts new_items.a_thing_to_do
puts new_items.select.reject.each(&:odd?).a_thing_to_do
# calling a_thing_to_do
# calling a_thing_to_do
# List
# calling a_thing_to_do
# calling a_thing_to_do
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment