5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

さいきんchefで調べたり使ったrubyのメソッド

Last updated at Posted at 2015-06-23

初心者の自分用のメモです。

・subで置換
 gsubだと正規表現で置換
・改行を削除とか
http://hono-auto.seesaa.net/article/406385660.html
・行の末尾
$でいいもよう
http://www.rubylife.jp/regexp/anchor/index2.html
・linesで改行を区切りにして配列処理できる
http://ref.xaio.jp/ruby/classes/string/lines

   "#{hosts_mng_data}".lines(separator = "\n") do |line|
    if "#{line}" =~ /^#{localip}.*/ 
     newline = line.sub(/$/,' localhost')
     mng_local_data = mng_local_data + newline
    else
     mng_local_data = mng_local_data + line
    end

localのipと一致したら行末にlocalhost追加したかっただけです。
sedかperlならワンライナーだけどchef的にtemplateリソースでデータを差し込みたかった。

・文字列の連結

a = 'aaaa'
b = 'bbbb'
hoge = a + b
puts hoge
# => aaaabbbb

方法別速度の比較↓
http://qiita.com/Kta-M/items/c7c2fb0b61b11d3a2c48

・uuidの求め方
http://qiita.com/smallpalace/items/8d222447253789436609

・pとputsのちがい
osコマンドだとechoのこと。
pだと色々でる検証向き。
putsだとクォートとかでない。

・pushで配列の末尾に要素を追加する
http://ref.xaio.jp/ruby/classes/array/push

・クラス
http://ref.xaio.jp/ruby/classes/class

・四捨五入はroundでいける
http://www.rubylife.jp/ini/numeric_class/index3.html

ohaiで搭載メモリの50%をGBになおしたもんをroundで四捨五入。

# Heap Size (defaults to 256m min, 1g max)
# ES_HEAP_SIZE=2g
ES_HEAP_SIZE=<%= ("#{node[:memory][:total]}"[/\d+/].to_f / 1024 / 1024 * 0.5 ).round %>g

インスタンスタイプのメタデータとってくるよりはohaiにがんばってもらったほうが。

・variables.merge!
ハッシュを連結してくれる。
http://ref.xaio.jp/ruby/classes/hash/merge

・if var
varに中身が入ってたら真

・include?
配列のなかに特定の文字列が含まれるかどうかを探す
https://hydrocul.github.io/wiki/programming_languages_diff/list/contains.html

mng_roles = [
    "change-password",
    "jenkins",
    "log",
    "monitor"
]

hoge = 'ldap'
hoge2 = 'aaaa'

p mng_roles.include?(hoge)

p mng_roles.include?(hoge2)

含まれればtrue、なければfalseがかえる

・セカンダリのIPをohaiで出す
http://qiita.com/ezaqiita/items/ac108ec887a665962b5c
http://aikotobaha.blogspot.jp/2013/07/chefohai.html

require 'rubygems'
require 'ohai'
oh = Ohai::System.new
oh.all_plugins

# p oh["network"]["interfaces"]["eth1"]["addresses"].select { |address, data| data["family"] == "inet" }[0][0]
host_address_val = oh["network"]["interfaces"]["eth1"]["addresses"].select { |address_key, address_val| address_val["family"] == "inet" }
 host_address_array = host_address_val.keys
puts host_address_array

・case分岐
特定の文字列が先頭からマッチしたら処理をわけたいことがあったので以下のように適当に動作確認を。

# cat /tmp/casetest.rb
hoge='aaa-mng'
fuga='aaa-dev1'
booo='dev99'

environments=[hoge,fuga,booo]

environments.each do |environment|
case environment
when /^aaa/
 puts "environment is aaa."
else 
 puts "environment is not aaa."
end
end

とりあえずこんなところで。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?