TypeError reading in multiline regex - ruby

In a file is apparently stored a multiline regex, saved by YAML::dump(b1) without a problem.
Question, how can I load it back when Syck seems to have issues with the multiline regular expression?
b2 = YAML::load(File.open("browserObj.yaml", 'r'))
Syck::TypeError: Invalid Regular expression: "/\\A\\s*\n ([a-zA-Z][\\-+.a-zA-Z\\d]*): (?# 1: scheme)\n (?:\n ((?:[\\-_.!~*'()a-zA-Z\\d;?:#&=+$,]|%[a-fA-F\\d]{2})(?:[\\-_.!~*'()a-zA-Z\\d;\\/?:#&=+$,\\[\\]]|%[a-fA-F\\d]{2})*) \\ (?# 2: opaque)\n |\n (?:(?:\n \\/\\/(?:\n \\ (?:(?:((?:[\\-_.!~*'()a-zA-Z\\d;:&=+$,]|%[a-fA-F\\d]{2})*)#)? \\ (?# 3: userinfo)\n (?:((?:(?:[a-zA-Z0-9\\-.]|%\\h\\h)+|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\[(?:(?:[a-fA-F\\d]{1,4}:)*(?:[a-fA-F\\d]{1,4}|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})|(?:(?:[a-fA-F\\d]{1,4}:)*[a-fA-F\\d]{1,4})?::(?:(?:[a-fA-F\\d]{1,4}:)*(?:[a-fA-F\\d]{1,4}|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}))?)\\]))(?::(\\d*))?))? (?# 4: host, 5: port)\n |\n ((?:[\\-_.!~*'()a-zA-Z\\d$,;:#&=+]|%[a-fA-F\\d]{2})+) \\ (?# 6: registry)\n )\n |\n (?!\\/\\/)) \\ (?# XXX: '\\/\\/' is the mark for hostport)\n \\ (\\/(?:[\\-_.!~*'()a-zA-Z\\d:#&=+$,]|%[a-fA-F\\d]{2})*(?:;(?:[\\-_.!~*'()a-zA-Z\\d:#&=+$,]|%[a-fA-F\\d]{2})*)*(?:\\/(?:[\\-_.!~*'()a-zA-Z\\d:#&=+$,]|%[a-fA-F\\d]{2})*(?:;(?:[\\-_.!~*'()a-zA-Z\\d:#&=+$,]|%[a-fA-F\\d]{2})*)*)*)? \\ (?# 7: path)\n )(?:\\?((?:[\\-_.!~*'()a-zA-Z\\d;\\/?:#&=+$,\\[\\]]|%[a-fA-F\\d]{2})*))? \\ (?# 8: query)\n )\n (?:\\#((?:[\\-_.!~*'()a-zA-Z\\d;\\/?:#&=+$,\\[\\]]|%[a-fA-F\\d]{2})*))? \\ (?# 9: fragment)\n \\s*\\z/x"
from /usr/lib/ruby/1.9.1/syck/rubytypes.rb:284:in `yaml_new'
from /usr/lib/ruby/1.9.1/syck.rb:135:in `transfer'
from /usr/lib/ruby/1.9.1/syck.rb:135:in `node_import'
from /usr/lib/ruby/1.9.1/syck.rb:135:in `load'
from /usr/lib/ruby/1.9.1/syck.rb:135:in `load'
from /usr/lib/ruby/1.9.1/syck.rb:146:in `block in load_file'
from /usr/lib/ruby/1.9.1/syck.rb:145:in `open'
from /usr/lib/ruby/1.9.1/syck.rb:145:in `load_file'
from (irb):428
from /usr/bin/irb:12:in `<main>'
I've seen discussion about a patch to rubytypes.rb but otherwise nothing.
I'd rather not switch to Psych since that opens up another can of worms:
YAML::ENGINE.yamler = "psych"
# attempt to load from file again
TypeError: can't convert Fixnum into String
What's going on? Is there any hope? I had no prior knowledge of Syck nor Psych nor the internals of YAML parsing until this error made it non-transparent.

You could store the regexp as string. When you read it back from the yaml file just convert the string back into a regexp. Regexp.new myregexp_str - This is of course just a workaround.
Update:
Or you could do something that really stupid and patch psych to handle multiline regexp.
Since the patched method is to long to post it grab it here.
But here is the explanation of what was really patched.
module Psych
module Visitors
class ToRuby < Psych::Visitors::Visitor
# ...
def deserialize
# ... L75
when "!ruby/regexp"
o.value =~ /^\/(.*)\/([mixn]*)$/m # <- notice the added "m" for multiline mode
# ...
end
end
end
end
using ruby-1.9.3-p194

For everybody lurking around, this looks like an old bug. I just tried with ruby 1.9.3 and I'm getting this:
[3] pry(main)> YAML.load(YAML.dump(/asd/m))
=> /asd/m

Related

How to put an image on a label using Prawn/labels?

I'm using Ruby for scripting, and Prawn-labels to print ISBN labels for books, using Barby to generate the EAN-13 bar codes.
The ISBNs come out of MySQL. That works fine. I can create a PDF of 30-up labels containing the titles of the books, using Prawn/labels. That works fine.
However, I am not having much joy putting a Barby-generated PNG image of the proper EAN-13 barcode on the label.
Here's getting the info out of the database and generating EAN-13, pretty straightforward:
query = "SELECT ISBN13, Title FROM s_library WHERE Has_barcode != 'y'"
result = client.query(query, symbolize_keys: true)
tags = []
result.each do |row|
tags << [row[:ISBN13], row[:Title], Barby::EAN13.new(row[:ISBN13].chop)]
end
tags.sort! {|a, b| a[1] <=> b[1]} # Sort by book title
So now I have a two-dimensional Array, with each row containing an ISBN String, a Title String, and a Barby holding the EAN-13 coding for the ISBN (#<Barby::EAN13:0x00007fc6da13ce98 #data="978140006215">
).
Now I'm following the example for Prawn::Labels, and if I leave out trying to #embed_image the PNG data, I can successfully generate 30-up labels with the title of each book:
Prawn::Labels.generate('EAN_tags.pdf', tags, :type => 'Avery5160') do |pdf, tag|
pdf.font '/Library/Fonts/Erasdemi.TTF'
pdf.text(tag[1], :align => :center)
png = Barby::PngOutputter.new(tag[2]).to_png
# pdf.embed_image(png, Prawn::Images::PNG.new(png), {})
end
`open EAN_tags.pdf`
When I un-comment the image embedding, I get a cryptic traceback that I don't quite understand:
25: from /Users/jan/bin/Print_ISBN13.rb:73:in `<main>'
24: from /usr/local/lib/ruby/gems/2.7.0/gems/prawn-labels-1.2.6/lib/prawn/labels.rb:18:in `generate'
23: from /usr/local/lib/ruby/gems/2.7.0/gems/prawn-2.3.0/lib/prawn/document.rb:408:in `render_file'
22: from /usr/local/lib/ruby/gems/2.7.0/gems/prawn-2.3.0/lib/prawn/document.rb:408:in `open'
21: from /usr/local/lib/ruby/gems/2.7.0/gems/prawn-2.3.0/lib/prawn/document.rb:408:in `block in render_file'
20: from /usr/local/lib/ruby/gems/2.7.0/gems/prawn-2.3.0/lib/prawn/document.rb:400:in `render'
19: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/renderer.rb:167:in `render'
18: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/renderer.rb:202:in `render_body'
17: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/document_state.rb:72:in `render_body'
16: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/object_store.rb:70:in `each'
15: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/object_store.rb:70:in `each'
14: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/object_store.rb:71:in `block in each'
13: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/document_state.rb:78:in `block in render_body'
12: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/reference.rb:26:in `object'
11: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:87:in `pdf_object'
10: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:87:in `each'
9: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:93:in `block in pdf_object'
8: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:87:in `pdf_object'
7: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:87:in `each'
6: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:93:in `block in pdf_object'
5: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:87:in `pdf_object'
4: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:87:in `each'
3: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:93:in `block in pdf_object'
2: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:75:in `pdf_object'
1: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:25:in `utf8_to_utf16'
/usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:25:in `encode': "\\x89" to UTF-8 in conversion from ASCII-8BIT to UTF-8 to UTF-16BE (Encoding::UndefinedConversionError)
I can dump the png variable to a file and see the barcode.
From the stack, it looks like it's trying to interpret the PNG data as a UTF-16 String. Ugh. Wasn't expecting that.
Any clues as to what I'm doing wrong?
Use pdf.image(png) instead of pdf.embed_image. This is a wrapper around pdf.embed_image that does the necessary work of getting the necessary data out of the PNG file.
The answer appears to be to use something called StringIO, which apparently provides a File interface to an in-memory (blob) image.
So, I added the middle line, below:
png = Barby::PngOutputter.new(tag[2]).to_png
str_png = StringIO.new(png)
pdf.image(str_png)
instead of trying to pass binary data via embed_image, and I am now getting the barcode in the final PDF output. (It still needs some tweaking, but the basic concept is now working.)
I had searched here many ways — really! But the word "blob" is what got the answer on this site.

How do I parse a tab-delimited line that contains a quote?

I'm using Ruby 2.4. How do I parse a tab-delimited line that contains a quote character? This is what's happening to me now ...
2.4.0 :003 > line = "11\tDave\tO\"malley"
=> "11\tDave\tO\"malley"
2.4.0 :004 > CSV.parse(line, col_sep: "\t")
CSV::MalformedCSVError: Illegal quoting in line 1.
from /Users/davea/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/csv.rb:1912:in `block (2 levels) in shift'
from /Users/davea/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/csv.rb:1868:in `each'
from /Users/davea/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/csv.rb:1868:in `block in shift'
from /Users/davea/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/csv.rb:1828:in `loop'
from /Users/davea/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/csv.rb:1828:in `shift'
from /Users/davea/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/csv.rb:1770:in `each'
from /Users/davea/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/csv.rb:1784:in `to_a'
from /Users/davea/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/csv.rb:1784:in `read'
from /Users/davea/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/csv.rb:1324:in `parse'
from (irb):4
from /Users/davea/.rvm/gems/ruby-2.4.0#global/gems/railties-5.0.1/lib/rails/commands/console.rb:65:in `start'
from /Users/davea/.rvm/gems/ruby-2.4.0#global/gems/railties-5.0.1/lib/rails/commands/console_helper.rb:9:in `start'
from /Users/davea/.rvm/gems/ruby-2.4.0#global/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
from /Users/davea/.rvm/gems/ruby-2.4.0#global/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /Users/davea/.rvm/gems/ruby-2.4.0#global/gems/railties-5.0.1/lib/rails/commands.rb:18:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
Although teh example illustrates my point, I can't easily control the input coming in. So, although an answer coudl be< "Remove all quotes from teh string before parsing," I want to preserve the data as closely as possible.
That's a malformed document if you're trying to adhere to the CSV standard. Instad you might just brute-force it and pray there's no tabs in the data itself:
line.split(/\t/)
The CSV parsing library comes in handy when you're dealing with data like this:
"1\t2\t\"3a\t3b\"\t4"
Update: If you're prepared to abuse the CSV library a little then you can do this:
CSV.parse("11\tDave\tO\"malley", col_sep: "\t", quote_char: "\0")
That basically kills quote detection, so if there is other data that depends on that being processed correctly this may not work out.
"11\tDave\tO\"malley" is not valid CSV data. Strangely enough, the answer is to use two double-quotes, and to double quote each element
2.3.1 :001 > require 'csv'
=> true
2.3.1 :002 > line = "\"11\"\t\"Dave\"\t\"O\"\"malley\""
=> "\"11\"\t\"Dave\"\t\"O\"\"malley\""
2.3.1 :003 > puts line # for clarity
"11" "Dave" "O""malley"
=> nil
2.3.1 :004 > CSV.parse(line, col_sep: "\t")
=> [["11", "Dave", "O\"malley"]]

Nokogiri method error

I am using Nokogiri as a tool to help me with a project I'm working on for school. Right now the idea of the project is to search an HTML document for a keyword that the user provides. The function will find all occurrences of the keyword in the HTML string after the <body> element unless the keyword appears within an HTML tag, then surround the string found with tags to ``highlight’’ the keyword. Right now my code is failing with a pretty hefty error call stack. The main error is this:
Nokogiri::CSS::SyntaxError:unexpected'$'after''[:substring_match,"simple"]'(Nokogiri::CSS::SyntaxError)
My Current Method:
require 'nokogiri'
file = File.open ("desktop/Personal/code.HTML")
#doc = Nokogiri::HTML(file)
puts #doc
puts "Welcome to the HTML keyword highlighter!"
puts "Please enter a keyword"
keyword = gets.chomp
highlight = "<span style='background-color: yellow; color: black'>#{keyword}</span>"
search = #doc.xpath("//body").search("[text()*=#{keyword}")
search.each do |node|
nodeN = node.name
nodeH = node.content.gsub(keyword, highlight)
node.replace(nodeH)
end
file.close
The error call stack:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2. 0.0/gems/nokogiri-1.5.6/lib/nokogiri/css/parser_extras.rb:87:in `on_error': unexpected '$' after '[:substring_match, "simple"]' (Nokogiri::CSS::SyntaxError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/racc/parser.rb:258:in `_racc_do_parse_c'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/racc/parser.rb:258:in `do_parse'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/css/parser_extras.rb:62:in `parse'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/css/parser_extras.rb:79:in `xpath_for'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/css.rb:23:in `xpath_for'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:111:in `block (2 levels) in css'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:109:in `map'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:109:in `block in css'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:239:in `block in each'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:238:in `upto'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:238:in `each'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:105:in `css'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:81:in `block in search'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:80:in `each'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:80:in `search'
from /Users/Eva/Desktop/Personal/Project_Nokogiri.rb:21:in `<main>'
Any advice on how to go about preventing this would be truly appreciated! I'm new to programming, Ruby, and Nokogiri so I have very little understanding of how to fix this on my own. Sorry if formatting is bad. Thank you for any help!
You forgot to close the square bracket:
search = #doc.xpath("//body").search("[text()*=#{keyword}]")
I'd add quotes just in case:
search = #doc.xpath("//body").search("[text()*=\"#{keyword}\"]")
(You might expand that by also ensuring the double quote is escaped if it is in keyword to make it bulletproof)

Why is YAML throwing float ArgumentErrors on strings?

I have some nested strings in a complex hash that triggers "ArgumentError" exceptions. What's the best practiced way in dealing with this?
require 'yaml'
{
a: 'hello',
b: [{f:'hello',g:Hash.new,i:{a:'hello'}}],
c: {e:"+."}
}.to_yaml #=> `Float': invalid value for Float(): "+" (ArgumentError)
Full error dump:
/Users/XXX/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/psych/scalar_scanner.rb:99:in `Float': invalid value for Float(): "+" (ArgumentError)
from /Users/XXX/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/psych/scalar_scanner.rb:99:in `tokenize'
from /Users/XXX/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:272:in `visit_String'
from /Users/XXX/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:128:in `accept'
from /Users/XXX/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:324:in `block in visit_Hash'
from /Users/XXX/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:322:in `each'
from /Users/XXX/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:322:in `visit_Hash'
from /Users/XXX/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:128:in `accept'
from /Users/XXX/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:324:in `block in visit_Hash'
from /Users/XXX/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:322:in `each'
from /Users/XXX/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:322:in `visit_Hash'
from /Users/XXX/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:128:in `accept'
from /Users/XXX/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:92:in `push'
from /Users/XXX/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/psych.rb:244:in `dump'
from /Users/XXX/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/psych/core_ext.rb:14:in `psych_to_yaml'
This appears to be a bug in the bundled psych. Patching ~/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/psych/scalar_scanner.rb at line 99 from:
Float(string.gsub(/[,_]|\.$/, ''))
to:
Float(string.gsub(/[,_]|\.$/, '')) rescue ArgumentError
fixes the issue. This is essentially what's in the psych gem as well as the Ruby 1.9 bundled version.
If you'd rather not patch your Ruby, using the psych-1.3.4 gem is another option; just be sure to require 'psych' rather than 'yaml':
gem 'psych', '=1.3.4'
require 'psych'
{a: 'hello', b: [{f:'hello',g:Hash.new,i:{a:'hello'}}], c: {e:"0+."}}.to_yaml
# => "---\n:a: hello\n:b:\n- :f: hello\n :g: {}\n :i:\n :a: hello\n:c:\n :e: 0+.\n"
This can be reproduced with a simpler example:
"+.".to_yaml
This appears to be a bug in the version of psych bundled with ruby 2.0.0 (and other versions, I'm sure):
when FLOAT
if string == '.'
#string_cache[string] = true
string
else
Float(string.gsub(/[,_]|\.$/, ''))
end
The problem is that that "+." looks like a valid floating point number like +.5.
This is fixed in Ruby 2.2.1 (or probably an earlier version), which specifically checks for the case where there may be a leading sign (+ or -):
when FLOAT
if string =~ /\A[-+]?\.\Z/
#string_cache[string] = true
string
else
Float(string.gsub(/[,_]|\.$/, ''))
end

ruby: `read': Invalid argument -(Errno::EINVAL) at File.read

I'm doing a simple script to check crc of all files...
require "zlib"
exit if Object.const_defined?(:Ocra)
files = Dir.glob("*")
File.open('dir.txt', 'a+') do |file|
file.puts files
end
File.read('dir.txt').each_line { |line|
file = File.read(line) ; nil
file_crc = Zlib.crc32(file,0).to_s(16)
puts line, file_crc
}
The problem is at the line File.read('dir.txt').each_line { |line|
I get this error:
test.rb:13:in `read': Invalid argument - 1.exe (Errno::EINVAL)
from C:/Users/Administrador/Desktop/1.rb:13:in `block in <main>'
from C:/Users/Administrador/Desktop/1.rb:12:in `each_line'
from C:/Users/Administrador/Desktop/1.rb:12:in `<main>'
PD: 1.exe is a file listed in the "dir.txt".
Have you checked that the line doesn't contain extra characters? p line.
IIRC line will contain the newline character, use line.chomp.

Resources