Upgrade to Pro — share decks privately, control downloads, hide ads and more …

生成AIの使いどころ

 生成AIの使いどころ

kanayannet

March 15, 2025
Tweet

More Decks by kanayannet

Other Decks in Programming

Transcript

  1. def prime?(n) return false if n < 2 (2..Math.sqrt(n)).none? {

    |i| n % i == 0 } end primes = (1..100).select { |num| prime?(num) } puts primes.join(", ")
  2. require 'sinatra' require 'sinatra/reloader' require 'json' set :messages, [] get

    '/' do @messages = settings.messages erb :index end post '/post' do content = params[:content] unless content.strip.empty? settings.messages << { id: settings.messages.size + 1, content: content } end redirect '/' end __END__ @@ layout <!DOCTYPE html> <html> <head> <title>Simple BBS</title> </head> <body> <h1>掲示板</h1> <%= yield %> </body> </html> @@ index <form action="/post" method="post"> <input type="text" name="content" required> <button type="submit">投稿</button> </form> <ul> <% @messages.each do |msg| %> <li><%= msg[:content] %></li> <% end %> </ul>
  3. 重要なリプレイ post '/post' do content = params[:content] unless content.strip.empty? settings.messages

    << { id: settings.messages.size + 1, content: content } end redirect '/' end
  4. # 一部抜粋 FILE_PATH = 'messages.txt' # ファイルからメッセージを読み込む def load_messages return

    [] unless File.exist?(FILE_PATH) File.read(FILE_PATH).split("\n").map.with_index(1) { |msg, idx| { id: idx, content: msg } } end # ファイルにメッセージを保存 def save_message(content) File.open(FILE_PATH, 'a') { |file| file.puts(content) } end set :messages, load_messages get '/' do @messages = settings.messages erb :index end post '/post' do content = params[:content].strip unless content.empty? save_message(content) settings.messages = load_messages end redirect '/' end
  5. # ... # 投稿処理 post '/post' do content = params[:content].strip

    unless content.empty? new_message = { id: settings.messages.size + 1, content: content } settings.messages << new_message save_messages(settings.messages) end redirect '/' end # 編集ページ get '/edit/:id' do @message = settings.messages.find { |msg| msg[:id] == params[:id].to_i } halt 404, "Not Found" unless @message erb :edit end # 編集処理 post '/update/:id' do id = params[:id].to_i new_content = params[:content].strip message = settings.messages.find { |msg| msg[:id] == id } halt 404, "Not Found" unless message message[:content] = new_content save_messages(settings.messages) redirect '/' end
  6. 重要なリプレイ # 投稿処理 post '/post' do content = params[:content].strip unless

    content.empty? new_message = { id: settings.messages.size + 1, content: content } settings.messages << new_message save_messages(settings.messages) end redirect '/' end
  7. # 投稿処理 post '/post' do content = params[:content].strip unless content.empty?

    new_message = { id: SecureRandom.uuid, content: content } settings.messages << new_message save_messages(settings.messages) end redirect '/' end
  8. まずは新規データ書き込み require './lib/bbs' require 'minitest/autorun' require 'minitest/spec' require 'minitest-spec-context' require

    'minitest/reporters' Minitest::Reporters.use! describe '掲示板のデータ' do before do @bbs = BBS.new end context '記事を書き込む' do it '記事が追加される' do @bbs.name = 'test' @bbs.title = 'test' @bbs.article = 'test' res = @bbs.add assert_equal true, res end end end
  9. Product code class BBS def initialize @article_path = './data/article.dat' @id_path

    = './data/id.dat' @name = '' @title = '' @article = '' end attr_accessor :name, :title, :article # 記事を追加する(デリミタはタブ) # id は自動採番で削除されても重複しないuniqな値 end