ためすう
private なクラスメソッドを定義する (Ruby)
2019-10-06やったこと
private なクラスメソッドを定義する方法を調べました。
確認環境
$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]
調査
private.rb
class Sample
private
def self.hoge1
'hoge1'
end
def self.hoge2
'hoge2'
end
private_class_method :hoge2
class << self
private
def hoge3
'hoge3'
end
end
end
# 呼び出せる
p Sample.hoge1
# 例外発生
# p Sample.hoge2
# 例外発生
# p Sample.hoge3
出力結果
$ ruby private.rb
"hoge1"
参考
- プロを目指す人のためのRuby入門
splat 展開 (Ruby)
2019-10-06やったこと
splat 展開を使ってみます。
確認環境
$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]
調査
$ irb
irb(main):001:0> [*3..8]
=> [3, 4, 5, 6, 7, 8]
irb(main):002:0> [*'bx'..'cg']
=> ["bx", "by", "bz", "ca", "cb", "cc", "cd", "ce", "cf", "cg"]
参考
AWS CLI の設定をする
2019-10-06やったこと
AWS CLI (Command Line Interface) の設定をします。
調査
設定する
$ aws configure
AWS Access Key ID [None]: xxx
AWS Secret Access Key [None]: xxx
Default region name [None]: xxx
Default output format [None]: xxx
設定した値を確認する
$ less ~/.aws/config
[default]
region = xxx
output = xxx
$ less ~/.aws/credentials
[default]
aws_access_key_id = xxx
aws_secret_access_key = xxx
参考
AWS CLI をインストールする
2019-10-05やったこと
AWS CLI (Command Line Interface) を Mac にインストールします。
調査
pip3 でインストール
$ pip3 install awscli --upgrade --user
path を通す
~/.bash_profile
export PATH=~/.local/bin:$PATH
再読み込み
$ source ~/.bash_profile
バージョン確認
$ aws --version
aws-cli/1.16.253 Python/3.6.2 Darwin/17.7.0 botocore/1.12.243
参考
numpy.transpose を使う
2019-10-05やったこと
numpy.transpose を使ってみます。
確認環境
$ python
Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 21 2017, 18:29:43)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.__version__
'1.16.4'
調査
>>> x = np.arange(6)
>>> x = x.reshape(3, 2)
>>> x
array([[0, 1],
[2, 3],
[4, 5]])
>>> x.transpose()
array([[0, 2, 4],
[1, 3, 5]])
>>> x.transpose(1, 0)
array([[0, 2, 4],
[1, 3, 5]])
>>> x.transpose(0, 1)
array([[0, 1],
[2, 3],
[4, 5]])
次元を入れ替えます。
参考
numpy.clip を使う
2019-10-05やったこと
numpy.transpose を使ってみます。
確認環境
$ python
Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 21 2017, 18:29:43)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.__version__
'1.16.4'
調査
最小値、最大値の範囲に値をおさめます。
>>> a = np.array([0.5, -7, 0.9, 1.2])
>>> np.clip(a, 0, 1)
array([0.5, 0. , 0.9, 1. ])
参考
コンソールで名前付きルーティングを呼び出す (Rails)
2019-10-04やったこと
開発コンソールで名前付きルーティングを呼び出せるようにします。
確認環境
$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]
$ rails --version
Rails 5.2.3
調査
このような routes が設定されていたとします。
hello_index GET /hello/:hello_id/index(.:format) hello#index
コンソールで呼び出します。
$ rails c
...
irb(main):007:0> Rails.application.routes.url_helpers.hello_index_path(1)
=> "/hello/1/index"
参考
vector を使ってみる (C++)
2019-10-04やったこと
vector を使ってみます。
確認環境
$ g++ --version
g++ (Homebrew GCC 9.2.0) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
調査
test.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v { 20, 30, 50 };
v.push_back(90);
v[0] = 1;
for (int v2: v) {
printf("%d\n", v2);
}
printf("---\n");
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
printf("%d\n", *it);
}
}
出力結果
1
30
50
90
---
1
30
50
90
参考
set を使ってみる (C++)
2019-10-04やったこと
set を使ってみます。
確認環境
$ g++ --version
g++ (Homebrew GCC 9.2.0) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
調査
test.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
set<int> m;
m.insert(10);
m.insert(5);
m.insert(5);
for (int v: m) {
printf("%d\n", v);
}
}
出力結果
5
10
参考
標準入力を受け取る (Ruby)
2019-10-03やったこと
標準入力を受け取る方法を調べました。
確認環境
$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]
調査
ruby-gets.rb
a = gets
b = gets.chomp
p a
p b
出力結果
$ ruby ruby-get.rb
aaa
bbbbb
"aaa\n"
"bbbbb"
chomp で改行が削除されます。