Skip to content

Instantly share code, notes, and snippets.

View mehdi-farsi's full-sized avatar

Mehdi FARSI mehdi-farsi

View GitHub Profile
class Array
def my_map
ary = []
self.each do |elem|
ary << yield(elem)
end
ary
end
def yield_with_return_value
hello_world = yield
puts hello_world
end
yield_with_return_value { "Hello World!" } # => Hello World!
def yield_with_arguments
hello = 'Hello'
world = 'World!'
yield(hello, world)
end
yield_with_arguments { |hello, world| puts "#{hello} #{world}" } # => Hello World!
def optional_block
yield if block_given?
end
optional_block # => nil
optional_block { puts 'optional block' } # => optional block
def yield_without_block
yield
end
yield_without_block # => LocalJumpError (no block given (yield))
def one_yield
yield
end
def multiple_yields
yield
yield
end
one_yield { puts "one yield" }
computer.cores = 2
computer[:screens] = 2
require 'ostruct'
computer = OpenStruct.new(ram: '4GB')
computer.instance_variables # => [:@table, :@modifiable]