2
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

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

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}"')

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
2
Help us understand the problem. What are the problem?