Skip to content

Instantly share code, notes, and snippets.

@michalvalasek
Created June 30, 2016 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michalvalasek/62c33be18f7a639ac2d4ae88d80cff8f to your computer and use it in GitHub Desktop.
Save michalvalasek/62c33be18f7a639ac2d4ae88d80cff8f to your computer and use it in GitHub Desktop.
Weird crystal error
f = File.new("buffer_i_gen_batt.txt")
6.times do
_ = f.gets
end
interval = f.gets
def real_val(raw_val)
(raw_val - 520) * 0.191
end
def normalize
end
loop do
break unless n = f.gets
unless n.to_s == ""
puts n.to_s.to_i
end
# puts n.to_i
end
@akwiatkowski
Copy link

f = File.new("buffer_i_gen_batt.txt")

6.times do
  _ = f.gets
end

interval = f.gets

def real_val(raw_val)
  (raw_val - 520) * 0.191
end

def normalize
end

loop do
  break unless n = f.gets

  unless n.to_s.strip == ""
    puts n.to_s.strip.to_i
  end

  # puts n.to_i
end

@pfertyk
Copy link

pfertyk commented Jun 30, 2016

file = File.open("buffer_i_gen_batt.txt")

values = Array(Float64).new

meas_offset = -512
linear_coefficient = 0.191

6.times do
    file.gets
end

interval = file.gets.to_s.to_i
puts interval

file.each_line do |raw_line|
    line = raw_line.to_s.strip
    puts line.inspect
    if line == ""
        raw_value = line.to_i
        real_value = (raw_value + meas_offset) * linear_coefficient
        values << real_value
        puts real_value
    end
end

file.close

min_value = values.min

values.each do |value|
    value -= min_value
end

Stacktrace:

1008
""
invalid Int32:  (ArgumentError)
[4422535] *CallStack::unwind:Array(Pointer(Void)) +87
[4422426] *CallStack#initialize:Array(Pointer(Void)) +10
[4422378] *CallStack::new:CallStack +42
[4390968] *raise<ArgumentError>:NoReturn +24
[4437093] *String#to_i32<Int32, Bool, Bool, Bool, Bool>:Int32 +245
[4436828] *String#to_i:Int32 +92
[4348897] ???
[4400473] main +41
[139764846427973] __libc_start_main +245
[4346921] ???
[0] ???

@akwiatkowski
Copy link

unless line == "" instead of if

file = File.open("buffer_i_gen_batt.txt")

values = Array(Float64).new

meas_offset = -512
linear_coefficient = 0.191

6.times do
    file.gets
end

interval = file.gets.to_s.to_i
puts interval

file.each_line do |raw_line|
    line = raw_line.to_s.strip
    puts line.inspect
    unless line == ""
        raw_value = line.to_i
        # real_value = (raw_value + meas_offset) * linear_coefficient
        # values << real_value
        # puts real_value
    end
end

file.close

min_value = values.min

values.each do |value|
    value -= min_value
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment