Java Java File Input Stream fis new File

  • Slides: 30
Download presentation

Javaより簡潔 *Java File. Input. Stream fis = new File. Input. Stream("hello. txt"); Input. Stream.

Javaより簡潔 *Java File. Input. Stream fis = new File. Input. Stream("hello. txt"); Input. Stream. Reader ir = new Input. Stream. Reader(fis, "MS 932"); Buffered. Reader br = new Buffered. Reader(ir); うわっ *Ruby file = open("hello. txt", "r")

Rubyの特徴 – 強力な配列 *Java int []a = {1, 2, 3, 4, 5} for(i=0; i<a.

Rubyの特徴 – 強力な配列 *Java int []a = {1, 2, 3, 4, 5} for(i=0; i<a. length; i++) { a[i] = a[i] * 2 } *Ruby a = [1, 2, 3, 4, 5] a = a. map { |x| x * 2 } Cool!

Ruby – その他の強力な文法 • arrayのhash – a = {: taro => [1970, 4, 12],

Ruby – その他の強力な文法 • arrayのhash – a = {: taro => [1970, 4, 12], : hana => [1980, 1, 1]} • 文字列配列 – %w{taro hanako jiro}. each { |a| puts a} • Case文 – sex = case name when /ro$/: : male when /ko$/: : female end

Active. Record • • Railsの肝? DBのテーブル = 1クラスとしてアクセス データベースの関連をたどれる SQL無しで色々なことができる db. select_one("SELECT * FROM

Active. Record • • Railsの肝? DBのテーブル = 1クラスとしてアクセス データベースの関連をたどれる SQL無しで色々なことができる db. select_one("SELECT * FROM users, groups WHERE name = ? AND users. id = groups. user_id", "arai") User. find_by_name("arai"). group

Railsの動作 リクエスト (/foo/hoge. html) Action Controller (app/controllers/foo. rb) (メソッド hoge) データベース (app/model/foo. rb) Action

Railsの動作 リクエスト (/foo/hoge. html) Action Controller (app/controllers/foo. rb) (メソッド hoge) データベース (app/model/foo. rb) Action View (eruby) (app/views/foo/hoge. rhtml) レスポンス

Migrate (2) class Create. Articles < Active. Record: : Migration def self. up create_table

Migrate (2) class Create. Articles < Active. Record: : Migration def self. up create_table : articles do |t| t. column : title, : string t. column : title_j, : string t. column : body, : text t. column : body_j, : text t. column : created_at, : datetime end def self. down drop_table : articles end

script/console • irb (対話的Rubyコンソール)環境 • Active. RecordでDB操作可能 • DBの管理運営や検査集計に Ruby script/console Loading development environment.

script/console • irb (対話的Rubyコンソール)環境 • Active. RecordでDB操作可能 • DBの管理運営や検査集計に Ruby script/console Loading development environment. >> Product. find(1) => #<Product @attributes={“price”=>” 100”}>

Rails実例 • 37 signals – Backpack – Basecamp • 国内事例 – Podcastle. jp –

Rails実例 • 37 signals – Backpack – Basecamp • 国内事例 – Podcastle. jp – my. ITPro

バッチ処理の記述 • /lib/tasksに. rakeファイルを作成 • rake tasknameで実行される desc “processing data" task : process_data =>

バッチ処理の記述 • /lib/tasksに. rakeファイルを作成 • rake tasknameで実行される desc “processing data" task : process_data => : environment do establish_connection Hoge. Data. process end

ベンチマーク結果 (参照) Benchmark. bmbm { |bm| user system total real bm. report("find") { find

ベンチマーク結果 (参照) Benchmark. bmbm { |bm| user system total real bm. report("find") { find 0. 161000 0. 030000 0. 191000 ( 0. 331000) 100. times { find_by_sql 0. 180000 0. 031000 0. 211000 ( 0. 340000) Article. find(1) connection 0. 120000 0. 000000 0. 120000 ( 0. 291000) } } bm. report("find_by_sql") { 100. times { Article. find_by_sql(["select * from articles where id = 1"]) } } bm. report("connection") { 100. times { Article. connection. execute("select * from articles where id = 1") } } }

ベンチマーク結果 (作成) Benchmark. bmbm { |bm| bm. report("create") { 100. times { Article. create(:

ベンチマーク結果 (作成) Benchmark. bmbm { |bm| bm. report("create") { 100. times { Article. create(: title => "hoge") } } bm. report("connection") { 100. times { Article. connection. execute("insert into articles (title) values ('hoge')") } } } user system total real create 0. 461000 0. 010000 0. 471000 ( 6. 199000) connection 0. 100000 0. 000000 0. 100000 ( 6. 099000)