ruby-gsl Sample: vector.rb


#!/usr/local/bin/ruby


require "GSL"
include GSL

STDERR.puts "Running tests for Vector..."

puts "the C way"
v = Vector.new 4
0.upto(3) do |i|
  v.set i, 1.23 + i
end
0.upto(3) do |i|
  printf "v[%d] = %g\n", i, v.get(i)
end

puts "the Ruby way"
a = [1,2,3,4]
i = 0
v = Vector.new a
v.each do |x|
  printf "v[%d] = %g\n", i, v.get(i)
  i += 1
end

v = Vector.new((0..99).to_a)
f = File.open "vector.dat", "w"
v.fprintf f, "%.5g"
f.close

STDERR.puts "\ndone."


Back