Jump to content

The ultimate community for Ruby on Rails developers.


I'm trying to understand passing code blocks

ruby ruby on rails

  • Please log in to reply
No replies to this topic

#1 Vell



  • Members
  • 100 posts
  • LocationWashington, DC

Posted 18 November 2013 - 09:02 PM



I came across this small project I wanted to work on where am now starting to create a lot of excel files. I decided its time that I create my own class that I can call when I want and reusable etc. I did the same thing with exporting data to csv. Unfortunately, exporting to csv is a lot simpler (in my mind) then exporting to excel. Of course this all depends on the gem that I use.


I just to use simple_xlss_writer (https://github.com/h...ple_xlsx_writer). Its one of the easies gems that I have seen for creating a basic excel spreadsheet. As I started to build out my xlsx class, I started to split apart the code that was in the example.


Once I finished creating my class and was ready to test this on some data (wasn't sure how to write a unit test or anything for this), I gave it a shot. And BAM! 

>> XlsxFile.new("text_file",".",%w(header1 header1))
LocalJumpError: no block given

This naturally brought me to wondering how I could pass a block when needed so that I can keep things going. I had done it maybe once before months ago as a another developer was telling me what to do but that didn't stick in my head since I was stressing about the task at hand then. This task is not so much a stretch since I have way to generate it what I need. But at the promise of learning something new I starting searching the web.


I came across this blog where it sorta spells out everything and gives examples but for whatever reason its not syncing in. 




So I decided to ask you guys to see if you can put it in a way that makes sense to me.


Questions like, how would I write a method that would send a block of code? What would I need to write when defining method so that it accepts the code? Why is there &block and &code?


I think the class that I am working on is a good one to learn this but Im just having a hard time figure out out where to start. Below is my class:



Generic class for exporting to a xlsx file. Class expects the following:
filename: of the file
file_path: that you want the file created
headers: Column headers takes only an array data type
note: puts method only takes an array data type

class XlsxFile
  require 'simple_xlsx'
  def initialize(filename, file_path, headers)
    @file_name = filename
    @file_path = file_path
    @headers = headers
    @file = new_file
    @file_count = 1
  def puts(arr)
    @worksheet.add_row arr

  def new_file
    f = SimpleXlsx::Serializer.new("#{@file_path}/#{@file_name}.xlsx")
    puts f.inspect
    return f
  def worksheet
    @worksheet = @file.add_sheet('data')
  def check_path
    `mkdir #{@file_path}` unless File.exist?(@file_path)
  def check_file!
    if File.exist? "#{@file_path}/#{@file_name}.xlsx"
      puts "File exists. Do you want to delete this file? ('yes' or 'no')"
      remove_file if gets.chomp =~ /yes/i 
  def remove_file
    `rm #{@file_path}/#{@file_name}.xlsx`

The example for simple_xlsx_writer is:

require 'simple_xlsx'

serializer = SimpleXlsx::Serializer.new("test.xlsx") do |doc|
  doc.add_sheet("People") do |sheet|
   sheet.add_row(%w{DoB Name Occupation})
   sheet.add_row([Date.parse("July 31, 1912"),
                 "Milton Friedman",
                 "Economist / Statistician"])

Any thoughts?


Also tagged with one or more of these keywords: ruby, ruby on rails

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users