<?xml version="1.0" encoding="utf-8"?>
<!-- generator="FeedCreator 1.7.2-ppt DokuWiki" -->
<?xml-stylesheet href="https://tm.root-n.com/lib/exe/css.php?s=feed" type="text/css"?>
<rss version="2.0">
    <channel>
        <title>Tipsというかメモ programming:ruby:idiom</title>
        <description></description>
        <link>https://tm.root-n.com/</link>
        <lastBuildDate>Wed, 19 Nov 2025 03:03:44 +0900</lastBuildDate>
        <generator>FeedCreator 1.7.2-ppt DokuWiki</generator>
        <image>
            <url>https://tm.root-n.com/lib/images/favicon.ico</url>
            <title>Tipsというかメモ</title>
            <link>https://tm.root-n.com/</link>
        </image>
        <item>
            <title>Ruby :: イディオム / 例外処理＆トランザクション</title>
            <link>https://tm.root-n.com/programming:ruby:idiom:exception_transaction</link>
            <description>#!/usr/local/bin/ruby -Ke

print &quot;Content-type: text/html; charset=EUC-JP\n\n&quot;

sql1 = &quot;insert into teble_1 (num, str) values(123, 'abc')&quot;
sql2 = &quot;insert into table_2 (num, str) values(789, 'xyz')&quot;

# DBインスタンスが生成されることを想定
db = DB.get

begin
	f=0
	db.exec(&quot;begin&quot;)
	db.exec(sql1)
	f=1
	db.exec(sql2)
	db.exec(&quot;commit&quot;)
rescue =&gt; e
	print e.to_s
	print &quot;&lt;br&gt;rescue&lt;br&gt;&quot;
	print &quot;f：#{f}&quot;
ensure
	print &quot;&lt;br&gt;ensure&lt;br&gt;&quot;
	print &quot;f：#{f}&quot;
end</description>
        <category>programming:ruby:idiom</category>
            <pubDate>Sun, 16 Mar 2008 22:48:19 +0900</pubDate>
        </item>
        <item>
            <title>Ruby :: イディオム / 手軽にファイル出力する</title>
            <link>https://tm.root-n.com/programming:ruby:idiom:output_file</link>
            <description>実行前に /tmp/hoge.txt の空ファイルを作り chmod 777 しておく
% touch /tmp/hoge.txt; chmod 777 /tmp/hoge.txt





output = '出力文字列
改行もOK'

fp = File.open(&quot;/tmp/hoge.txt&quot;,'w')
fp.puts output
fp.close</description>
        <category>programming:ruby:idiom</category>
            <pubDate>Sun, 16 Mar 2008 22:48:19 +0900</pubDate>
        </item>
        <item>
            <title>Ruby :: イディオム / postgresql のエラーチェック</title>
            <link>https://tm.root-n.com/programming:ruby:idiom:pg_result_status</link>
            <description>参照系のエラーチェック (ストアドも含む)



res = conn.exec('SELECT * .....')
if res.status == PGresult::TUPLES_OK
    何かの処理
else
    エラー処理
end





更新系のエラーチェック 



res = conn.exec('INSERT / UPDATE / DELETE .....')
if res.status == PGresult::COMMAND_OK
    何かの処理
else
    エラー処理
end</description>
        <category>programming:ruby:idiom</category>
            <pubDate>Sun, 16 Mar 2008 22:48:19 +0900</pubDate>
        </item>
        <item>
            <title>Ruby :: イディオム / ファイルから1行ずつ読み込む</title>
            <link>https://tm.root-n.com/programming:ruby:idiom:read_file</link>
            <description>#!/usr/local/bin/ruby -Ks
#
# 第一引数(ARGV[0])にファイルを指定する
#

f = open(ARGV[0])
f.each { |line|
    print line unless line.chop == ''
}</description>
        <category>programming:ruby:idiom</category>
            <pubDate>Sun, 16 Mar 2008 22:48:19 +0900</pubDate>
        </item>
        <item>
            <title>Ruby :: イディオム / 手軽にメール送信する</title>
            <link>https://tm.root-n.com/programming:ruby:idiom:send_mail</link>
            <description>mailstr = 'Subject: test mail
From: hoge@example.com
To: fuga@example.com
Date: Thu, 21 Sep 2099 11:03:15 +0900 (JST)
Mime-version: 1.0

あいうえお'


SENDMAIL = '/usr/sbin/sendmail'
addr = 'hoge@example.com'
to   = 'fuga@example.com'

IO.popen(&quot;#{SENDMAIL} -i -f#{addr} #{to}&quot;, 'w') do |fp|
	fp.puts mailstr
end</description>
        <category>programming:ruby:idiom</category>
            <pubDate>Sun, 16 Mar 2008 22:48:19 +0900</pubDate>
        </item>
        <item>
            <title>Ruby :: イディオム / 今日、昨日、一ヶ月前</title>
            <link>https://tm.root-n.com/programming:ruby:idiom:today_yesterday_1month_ago</link>
            <description>require 'date'

# today
today = DateTime.now.strftime('%Y-%m-%d')

# yesterday
yesterday = (DateTime.now - 1).strftime('%Y-%m-%d')

# 1 month ago
monthago = (DateTime.now &lt;&lt; 1).strftime('%Y-%m-%d')

# 3 month ago
threemonthago = (DateTime.now &lt;&lt; 3).strftime('%Y-%m-%d')</description>
        <category>programming:ruby:idiom</category>
            <pubDate>Sun, 16 Mar 2008 22:48:19 +0900</pubDate>
        </item>
    </channel>
</rss>
