LoginSignup
2

More than 5 years have passed since last update.

CRubyでFizzBuzzを書いてみた。JRubyでは動かない

Last updated at Posted at 2012-03-16
require 'fileutils'

File.open("./numeric.c", "r") do |klass|
  File.open("./numeric.tmp", "w") do |tmpfile|
    while line = klass.gets
      tmpfile.write line
      if line =~ /fix_to_s\(int argc, VALUE \*argv, VALUE x\)/
        2.times {tmpfile.write klass.readline}
        tmpfile.write <<PATCH
    int val;
    val = FIX2INT(x);
    if (val % 15 == 0) return rb_str_new2("FizzBuzz");
    if (val %  3 == 0) return rb_str_new2("Fizz");
    if (val %  5 == 0) return rb_str_new2("Buzz");
PATCH
      end
    end
  end
end

FileUtils.mv("numeric.tmp", "numeric.c")

system('make')
system('./ruby -ve "(1..100).each{|i| puts i}"')

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2