Rubyに投稿されたコード一覧

Ruby 書いてみた

chars = [*"a".."z"]
per=[12.31959369,0.8250647611,0,1.9310338607,6.7622950682,0.0191191647,1.8584790696,2.6301082644,11.740626672,0,5.9303670688,0,2.4648992608,7.4677425668,10.614067677,0.1907012631,0,4.6787983637,5.3219860638,6.6799356681,8.884030162,0.0078437652,2.140363965,0,3.7282335687,1.38148096]

MAX = (ARGV.shift || 100).to_i

out = ""
per.each_with_index do |p, i|
  out += chars[i]*(0.01*p*MAX).to_i
end

out = out.ljust(MAX, " ")

puts out.chars.shuffle.map{ |c| (Random.rand(2)==0)? c.upcase : c }.join

Ruby side_tana.rb

#-*- coding: utf-8 -*-

Plugin.create(:side_tana) do
  command(:side_tana,
          name: 'たなさんする',
          condition: lambda{ |opt| true },
          visible: true,
          role: :timeline) do |opt|
    opt.messages.each { |m|
      Service.primary.post(:message =>"かえるぴょこぴょこ#{" " * rand(10)}", :replyto => m)
      Service.primary.post(:message =>"あわせてぴょこぴょこ#{" " * rand(10)}", :replyto => m)
    }
  end
end

Ruby test_code

#! /usr/bin/ruby

t = Time.now
p t.year	#=> 2012
p t.month	#=> 3

Ruby ゆきうさぎ製造マシーン(Ruby)

def textread()
  pattern=Regexp.new(ARGV[0])
  file1=ARGV[1]
  file2=ARGV[2]
  file_in=open(file1)
  file_out=open(file2,"w")
  count=1
  while text=file_in.gets()
    if count%2!=0
      file_out.puts(text)
      if pattern=~text
        snow=file_in.gets()
        prabbit=snow.to_i/10
        print(text,":",prabbit)
      end
    else
      rabbit=text.to_i/10
      file_out.puts(rabbit)
    end
    count=count+1
  end
  file_in.close
  file_out.close
end

textread()

Ruby Rubyで完全数を配列にしたった??

def perfect_num n
  get_array(n, [])
end

def get_array(n, a)
  return a if n == 0
  x = 0

  (1..n-1).each {|i|
    x += i if n%i == 0
  }
  a.push n if n == x

  get_array(n-1, a)
end

Ruby Rubyで配列を逆順にして出力する。

def my_reverse(x)
  my_reverse_rec(x, [])
end

def my_reverse_rec(x, y)
  if x.length == 0
    y
  else
    my_reverse_rec(x, y.push(x.pop))
  end
end

Ruby 配列の要素を逆順にする処理を再帰でムリヤリ書いた

$hai = [1,2,3,4,5]

$s = 0
$t = $hai.length - 1

class Tostatic
  def self.rev
     if $t < $s then
       return
     end
     $hai[$s], $hai[$t] = $hai[$t], $hai[$s]
     $s += 1
     $t -= 1
     self.rev
  end
end

Tostatic.rev

puts $hai[0]
puts $hai[1]
puts $hai[2]
puts $hai[3]
puts $hai[4]

Ruby DDSパラメータを求める

Base = 50000000
Goal = 1789772.5

div = (Base / Goal).round

best_add = 0
best_max = 0
best_gosa = Base.to_f
for add in 1..100
	gosa_min = Base.to_f

	hosei = -add

	loop{
		max = div * add + hosei

		hz = 0
		count = 0
		amari = 0

		for i in 0..Base
			if count > max
				count -= max
				amari += count
				hz += 1
			else
				count += add
			end
		end

		hzo = hz + count / max.to_f
		h = hzo - Goal

		if h.abs < gosa_min
			print "加算値 ", add, "  しきい値 ", max, "  補正 ", hosei, "\n"
			gosa_min = h.abs
			if gosa_min < best_gosa
				best_add = add
				best_max = max
				best_gosa = gosa_min
				print "更新 誤差 ", best_gosa, "\n"
			end
		else
			break
		end

		if h > 0.0
			hosei += 1
		else
			hosei -= 1
		end
	}
end

print "加算値 ", best_add, "  しきい値 ", best_max, "  誤差 ", best_gosa, "\n"

Ruby verilog_generate_label.rb

#encoding : sjis
#Verilog HDLのラベルがないgenerate for文にラベルを付ける
require 'fileutils'

f	= File.open(ARGV[0],"r")
FileUtils.cp(f,File.new(ARGV[0]+".bak","w"))	#バックアップ作成
s	= f.read
s2	= s.dup

#コメントを空白で置換
s.gsub!(/\/\*.*?\*\//m){|match|
	" "*match.size
}
s.gsub!(%r{//.*}){|match|
	" "*match.size
}

label_idx	= Array.new # ラベルを挿入する位置を保存
i			= 0
endgen_idx 	= 0
idx			= 0
while (idx = s.index(/(\s|\n|\t)generate(\s|\n|\t)/,endgen_idx))	#serch generate
	endgen_idx = s.index(/(\s|\n|\t)endgenerate(\s|\n|\t)/,idx)		#serch endgenerate
	while (idx = s.index(/(\s|\n|\t)for(\s|\n|\t)/,idx)) 			#serch for
		if (idx>endgen_idx)
			break
		end
		idx  = s.index(/(\s|\n|\t)begin(\s|\n|\t)/,idx+4)+6			#serch begin

		#既にラベルが付いているかどうかチェック
		j = idx+1
		while s[j] =~ /(\s|\n|\t)/
			j += 1
		end

		if (s[j] != ':')
			label_idx[i] = idx
			i+=1
		end
	end
end

print "Result >> Added "+i.to_s+" labels\n"
#ラベルを挿入していく
(label_idx.size - 1).downto(0){|i|
	s2.insert(label_idx[i]," : _label_"+i.to_s)
}

File.open(ARGV[0],"w").print(s2)

Ruby verilog_generate_label.rb

#encoding : sjis
#Verilog HDLのラベルがないgenerate for文にラベルを付ける
require 'fileutils'

f	= File.open(ARGV[0],"r+")
FileUtils.cp(f,File.new(ARGV[0].delete(".v")+".bk.v","w"))	#バックアップ作成
s	= f.read
s2	= s.dup

#コメントを空白で置換
s.gsub!(/\/\*.*?\*\//m){|match|
	" "*match.size
}
s.gsub!(%r{//.*}){|match|
	" "*match.size
}

label_idx	= Array.new # ラベルを挿入する位置を保存
i			= 0
endgen_idx 	= 0
idx			= 0
while (idx = s.index(/(\s|\n|\t)generate(\s|\n|\t)/,endgen_idx))	#serch generate
	endgen_idx = s.index(/(\s|\n|\t)endgenerate(\s|\n|\t)/,idx)		#serch endgenerate
	while (idx = s.index(/(\s|\n|\t)for(\s|\n|\t)/,idx)) 			#serch for
		if (idx>endgen_idx)
			break
		end
		idx  = s.index(/(\s|\n|\t)begin(\s|\n|\t)/,idx+4)+6			#serch begin

		#既にラベルが付いているかどうかチェック
		j = idx+1
		while s[j] =~ /(\s|\n|\t)/
			j += 1
		end

		if (s[j] != ':')
			label_idx[i] = idx
			i+=1
		end
	end
end

print "Result >> Added "+i.to_s+" labels\n"
#ラベルを挿入していく
(label_idx.size - 1).downto(0){|i|
	s2.insert(label_idx[i]," : _label_"+i.to_s)
}

f.print(s2)
f.close
Total Pages: 1 / 212

よく投稿されているコード

タグ

最近投稿されたコード