Railsの会 24日目 「%w[]/assert/正規表現/」
今回の進捗です
%w[]
配列を作る。 スペースと区切りで指定する。
例
>> %w[foo bar baz] => ["foo", "bar", "baz"] >> addresses = %w[USER@foo.COM THE_US-ER@foo.bar.org first.last@foo.jp] => ["USER@foo.COM", "THE_US-ER@foo.bar.org", "first.last@foo.jp"] >> addresses.each do |address| ?> puts address >> end USER@foo.COM THE_US-ER@foo.bar.org first.last@foo.jp
.inspect
オブジェクトをわかりやすい文字列に返す。
message = "hello, world" puts message.inspect number = 123 puts number.inspect class Cat def initialize(name, color) @name = name; @color = color end end cat = Cat.new("Tama", "white") puts cat.inspect
結果
"hello, world" 123 #<Cat:0x1a80500 @name="Tama", @color="white">
用途
変数や配列など文字列になる可能性のあるものを調べるときに使う。
inspect (Object) - Rubyリファレンス
assert
処理ができていれば trueを表示 それ以外はメッセージを表示
class AssertTest < ActionController::IntegrationTest def test_assert assert false, "falseは想定外です" assert nil, "何らかの値が設定されるべきです" end end
正規表現
いくつかの文字列を一つ文字列で表す方法。
曖昧検索が可能になる!
メタ文字
通常の文字a,b,c...etc と組み合わせて使います。
例 \d \D \w \W \s \S
正規表現 - Wikipedia
^t.*?[ao]{2,4}.*?@(gmail\.com|yahoo\.co\.jp)
解説
^t → 先頭に t がある。
.*? → 文字がいくつかあるかもしれないし、ないかもしれない。
[ao]{2,4} → a か o が2文字~4文字ある。
@ → @がある。
(gmail¥.com|yahoo¥.co¥.jp)
→ gmail.com か yahoo.co.jp のどちらかがある。