職案人

求職・歴史・仏教などについて掲載するつもりだが、自分の思いつきが多いブログだよ。適当に付き合って下さい。

簡単なアプリ

2022年08月21日 | ruby 3.1.2
簡単なアプリ


【開発環境】
OS:Win11(64ビット)
Ruby 3.1.2

■ファイルを読み書きするアプリを作ってみる
「file1.rb」フアイルにコードを書いてみる。
file_path = 'sample.txt'

#テキストファイルに書き込む
open(file_path,'w') do |f|
f.puts('Hello,World!')
end

#テキストファイルから読み込む
open(file_path,'r') do |f|
p f.read
end

・コマンド実行
Microsoft Windows [Version 10.0.22000.856]
(c) Microsoft Corporation. All rights reserved.

C:\Users\Owner>ruby D:\pg\Ruby31-x64\サンプルプログラム\file1.rb
"Hello,World!\n"

C:\Users\Owner>

・「sample.txt」を見てみる
「Hello,World!」が書き込めれている

データを追加する
「file2.rb」フアイルにコードを書いてみる。
file_path = 'sample.txt'

#テキストファイルに書き込む
open(file_path,'a') do |f|
f.puts('Hello,Hello,World!')
end

#テキストファイルから読み込む
open(file_path,'r') do |f|
p f.read
end

・コマンド実行
C:\Users\Owner>ruby D:\pg\Ruby31-x64\サンプルプログラム\file2.rb
"Hello,World!\nHello,Hello,World!\n"

C:\Users\Owner>

・「sample.txt」を見てみる
Hello,World!
Hello,Hello,World!
コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

標準ライブラリ

2022年08月19日 | ruby 3.1.2
標準ライブラリ


【開発環境】
OS:Win11(64ビット)
Ruby 3.1.2

【dateモジュール】
「library1.rb」ファイルにコードを書き込む

require 'date'←

today = Date.today

p today
p today.year

day_of_the_weeks = %w(日 月 火 水 木 金 土)
day_of_the_week = day_of_the_weeks[today.wday]

p today.strftime('%Y年%m月%d日') + day_of_the_week + '曜日'

C:\Users\Owner>ruby D:\pg\Ruby31-x64\サンプルプログラム\library1.rb
#<Date: 2022-08-19 ((2459811j,0s,0n),+0s,2299161j)>
2022
"2022年08月19日金曜日"

C:\Users\Owner>
コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

モジュール

2022年08月18日 | ruby 3.1.2
モジュール


【開発環境】
OS:Win11(64ビット)
Ruby 3.1.2

1.モジュールとは
モジュール→処理(メソッド)をひとまとまりにする仕組み。クラスと違って、インスタンスを生成できない

2.使用例--include
「module1.rb」ファイルのコードを書く
module SampleModule
def Sample_mothod
p 'call sample_method.'
end
end

class SampleClass
include SampleModule
end

instance = SampleClass.new ※インスタンを生成する
instance.Sample_mothod


コマンドを実行する
Microsoft Windows [Version 10.0.22000.856]
(c) Microsoft Corporation. All rights reserved.

C:\Users\Owner>ruby D:\pg\Ruby31-x64\サンプルプログラム\module1.rb
"call sample_method."

C:\Users\Owner>

3.使用例2--extend
「module2.rb」ファイルのコードを書く
module SampleModule
def Sample_mothod
p 'call sample_method.'
end
end

class SampleClass
extend SampleModule
end

SampleClass.Sample_mothod

コマンドを実行する
C:\Users\Owner>ruby D:\pg\Ruby31-x64\サンプルプログラム\module2.rb
"call sample_method."
コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

例外--raise

2022年08月17日 | ruby 3.1.2
例外を発生させる


【開発環境】
OS:Win11(64ビット)
Ruby 3.1.2

・例外を発生させるraise命令
例外を意図的に発生させるには、raise命令を使う

構成文
1.raise'エラーメッセージ'
2.raise 例外の種類、'エラーメッセージ'

・「raise.rb」ファイルにコードを書く
begin
# TypeError例外を発生
raise TypeError,'original error'
rescue => e
p e.message
# 例外処理後に例外を再発生
raise
end

・コマンドの実行
Microsoft Windows [Version 10.0.22000.856]
(c) Microsoft Corporation. All rights reserved.

C:\Users\Owner>ruby D:\pg\Ruby31-x64\サンプルプログラム\raise.rb
"original error"
D:/pg/Ruby31-x64/サンプルプログラム/raise.rb:3:in `
': original error (TypeError)

C:\Users\Owner>
コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

例外処理の構文

2022年08月16日 | ruby 3.1.2
例外処理の構文


【開発環境】
OS:Win11(64ビット)
Ruby 3.1.2

・構文
begin
 例外を補足する対象の処理
・・・・・・
rescue => e
例外発生時の処理
ensure
 例外発生有無に関わらず実行する処理
end

例文1
1.「例外2」ファイルにコードを書く
a
rescue => e
p 'this is rescue block.'
p e
ensure
p 'this is rescue block.'
end

2.コマンドを実行
C:\Users\Owner>ruby D:\pg\Ruby31-x64\サンプルプログラム\例外2.rb
"this is rescue block."
#<NameError: undefined local variable or method `a' for main:Object

p a
^>
"this is rescue block."

C:\Users\Owner>

「例外2」ファイルを変数aに数値を入れる。
a = 1
begin
p a
rescue => e
p 'this is rescue block.'
p e
ensure
p 'this is rescue block.'
end

コマンドを実行する
C:\Users\Owner>ruby D:\pg\Ruby31-x64\サンプルプログラム\例外2.rb
1
"this is rescue block."

C:\Users\Owner>
コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする