module CustomEnumerable
    def my_each ()
        # your code here!
        for i in 0...self.length
            yield(self[i])
        end
    end
end
class Array
    include CustomEnumerable
end

[1,2,3,4].my_each { |i| puts i } # => 1 2 3 4
[1,2,3,4].my_each { |i| puts i * 10 } # => 10 20 30 40# yield calls a passed block (anonymous function) passed to the method

def some_method
  puts "Before caling the block"
  yield
  puts "After calling the block"
end

# if a method yields and there was no block
# an error is raised
some_method #=> no block given (yield) (LocalJumpError)

# Here is a shortened syntax of passing a block
some_method { puts "Hello from the block" } 
#=> Before caling the block
#=> Hello from the block
#=> After caling the block

# Here is the longer syntax of passing a block
some_method do
  puts "Hello from the block"
end

# You can use the `block_given?` method to
# determine whether a block is present
def method_with_optional_block
  puts "Before caling the block"
  yield if block_given?
  puts "After calling the block"
end

# No error is raised when no block has been passed
method_with_optional_block
#=> Before caling the block
#=> After caling the block

method_with_optional_block { puts "Hello" }
#=> Before caling the block
#=> Hello
#=> After caling the block

# You can also capture the block as a `Proc`
def method_with_proc_block(&block)
  # same thing as `yield if block_given?`
  block.call if block
  
  # You can forward the passed block to another method
  some_method(&block)
end

Ruby相关代码片段

how to run rake task with arguments

ruby create array with n elements

Ruby Multi-root workspace

ruby each cons

bundle add gem

ruby how to add bundle

focusout event in capybara

clone vs dup ruby

Hash merge, join hashes

Splat and double Splat operators

read input from console in ruby, gets.chomp

how to download ruby on mac

add into array ruby

ruby reduce(:|)

deep copy in ruby

dup method in ruby (shallow copy)

tailwind css ruby bin/dev not working

write a method in ruby to get letter frequency

install subl command

has_one and belongs_to

convert keys to camel case

ruby number generator

rubymine copy path shortcut

rubymin open and close terminal

ruby each_key

divide arrays, partition

rest countries

write this in ruby

enumerable in ruby

bash: ruby: command not found

Ruby: Delegation

Ruby: fetch method with default value

Ruby: Using dig method to retrieve nested values

A server is already running

what would raise this exception

remove ruby using rvm

ruby 1.9 hash syntax

Ruby lowercase

obj file that contain vertex of a normal triangle

active record query log

httpparty debug

ruby to change ruby version

change system ruby

active record logs in console

access nested hash data ruby

ruby faker url

pre_posting_code

Ruby language hello

attribute accessor ruby

ruby 2d to 1d

ruby take elements from array

yield ruby

next array element

unless ruby

POSTGRESS CONFIGURATION

ruby list all variables in scope

how to check ruby version in cmd

ruby swap var

ruby copy var

ruby queue

find out running process id

kill all local hosts

ruby read from stdin

yaml load file in hash ruby

sort ruby hash by key

format ruby hash online

how to sum two numbers in ruby

ruby array inject

array skip first element ruby

Enable CORS (Cross Origin Resource Sharing)

ruby mkdir recursive

convert float to int ruby

if else ruby

ruby call class constant

Ruby comments

ruby array push if not exists

Ruby times downto upto step

Iterating over Arrays ruby

ruby silce!

cancancan check action controller