3
3

More than 5 years have passed since last update.

Ruby初心者から上級者まで!! 知っても使えないRuby知識

Last updated at Posted at 2014-02-14

この記事の要約: 役に立ちません

1. %記法ではどの文字が使える?

英数字以外のASCIIなら、なんでもありです。

%{open{  # => "open"  
%%percent% # => "percent"
%\back slash\ # => "back slash"
% space  # => "space"
%
LF
# => "LF"
% null % # => "null"

Rubyのパワーにはシンタックスハイライトもついてこれません。

2. ?記法ではどの文字が使える?

文字を表すのに便利な?xも結構なんでもありです。

?, # => ","
(?ま..?も).count # => 5
.. # => ?̀..?ͯ
? # => "\b"

3. 変数宣言してみよう

代入=を使うと左辺のローカル変数は宣言されます。

if false
 orange = mikan = 100.yen # 通らないはず
end
orange # => nil
mikan # => nil

defined? code = exit
code # => nil

for文でも宣言されます。

for banana in [];end
banana # => nil

4. 変数/メソッド名ではどの文字が使える?

変数/メソッド名等には英数字およびアンダースコア('_')しか使えない、と多くのドキュメントにあります。
ex. リファレンス

実際には非ASCII文字も使えます。

うどん = "そば"; うどん # => "そば"
Tシャツ = ""; Object.constants.grep(/Tシャツ/) # => [:Tシャツ]
def メソッド
  "method"
end
メソッド # => "method"

実はリファレンスにも、利用できる(が非推奨)と書かれています。

3
3
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
3
3