Learn to Code via Tutorials on Repl.it!

← Back to all posts
Basic Ruby
jimibue (1)

Ruby Block

Blocks are anonymous functions (i.e. not named) that are passed into methods.

Examples

  # If a block is a single line use {}
	5.times { |x| puts x}

  # If a block requires multiple lines enclose in do end
	5.upto(10) do |x|
    if x.odd?
        puts 'odd'
      end
      puts 'even' 
	end
Commentshotnewtop
sugarfi (489)

Good tutorial, but you should add some sort of introduction and possibly add other features of the language, like variables or functions.