rubyupgradejruby

How to use csv converter encoding in ruby?


require 'csv'
module Expertus
 class Dataset < Array
  attr_accessor :data_type, :udc_id, :filename_suffix

# knows its common_udc_id and data type

  def initialize *args
    CSV::Converters[:non_octal_integer] = lambda { |f|
     begin
      e = f.encode(CSV::ConverterEncoding)
      unless e == "0"
        if e.starts_with? "0"
          return f
        end
      end
    rescue
      return f
    end
    Integer(f.encode(CSV::ConverterEncoding)) rescue f
  }

  CSV::Converters[:non_octal_float] = lambda { |f|
    begin
      e = f.encode(CSV::ConverterEncoding)
      unless e.starts_with? "0."
        if e.starts_with? "0"
          return f
        end
      end
    rescue
      return f
    end
    Float(f.encode(CSV::ConverterEncoding)) rescue f
  }

  CSV::Converters[:string_to_bool] =->(f) {
    ['true', 'false'].include?(f) ? f=='true' : f
  }
  @csv_options = {:converters => [:non_octal_integer, :non_octal_float, :string_to_bool]}
  @data_type = nil
  @udc_id = nil
  @filename_suffix = nil
  super
end
def expected_output_filename
    return "#{@udc_id}_#{@data_type}#{@filename_suffix}.csv"
end
def read_expected_output_file_from_directory! directory
  if directory.is_a? String
    p "Directory/File:::: #{File.join(directory, self.expected_output_filename)}"
    p "@csv_options::: #{@csv_options}"
    csv_data = CSV.read File.join(directory, self.expected_output_filename), @csv_options
  else
    csv_data = CSV.new(directory, @csv_options).read
  end
  csv_data[0].map! { |h| h.to_sym }
  self.clear
  self.replace csv_data
end

I am calling read_expected_output_file_from_directory method in my jRuby script like:

expected_dataset.read_expected_output_file_from_directory! File.join($session.script_dir, '..', 'expectedresults')

It is throwing an error:

["wrong number of arguments (given 2, expected 1) (ArgumentError)"]

/home/eip/jruby-9.4.5.0/lib/ruby/stdlib/csv.rb:1821:in `read'

Any help is appreciated ? Thanks

What worked in this as suggested by @Stefan & @Ilmari Karonen :

def read_expected_output_file_from_directory! directory
  if directory.is_a? String
    p "Directory/File:::: #{File.join(directory, self.expected_output_filename)}"
    p "@csv_options::: #{@csv_options}"
    csv_data = CSV.read File.join(directory, self.expected_output_filename), **@csv_options**
  else
    csv_data = CSV.new(directory, **@csv_options**).read
  end
  csv_data[0].map! { |h| h.to_sym }
  self.clear
  self.replace csv_data
end

Solution

  • As Stefan notes in the comments above, CSV.read and CSV.new expect the options to be passed as keyword arguments.

    You can convert a hash to keyword arguments via **, e.g.:

    CSV.new(directory, **@csv_options)
    

    In Ruby 2 (and JRuby 9.3) you could pass a hash as the last argument to a method expecting keyword arguments and it would get implicitly converted into keyword arguments. However, in Ruby 3 (and JRuby 9.4) this no longer happens unless you request it explicitly using **.