Home > Tags > 新規タグの追加
新規タグの追加
Ruby(Ruby on Rails)とxyzzyをデビューしました
windowsのコマンドで学んだこと
ls → dir
rm → del
mv → rename
xyzzyで学んだこと
C-x C-k f で文字コード変換
C-r 上に検索
C-s 下に検索
C-x C-f ファイルを開く
rubyで学んだこと
%q、%、Q%を使うと、二十引用符と一重引用符以外の好きな記号で文字列を囲むことができる
hello = %q/こんにちは/ hello = %Q(こんにちは) hello = %<こんにちは>
変数型変換
a = 4 b = '9' puts a.to_s + b # 49 数値を文字列に変換するメソッド to_s (to_stringの略でしょ) puts a + b.to_i # 13 文字列を整数に変換するには to_i (to_intの略でしょ) puts a * b.to_i # 36 浮動小数点に変換するには to_f (to_floatの略でしょ)
配列
colors = ['red', 'blue', 'yellow']
puts colors.join(', ') # red, blue, yellow
colors.push('pink', 'green')
puts colors.join(', ') # red, blue, pink, green
イテレータ
10.times do |i|
print i, ', ' # 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
end
# eachメソッドは、パラメータ(次の例ではitem)に配列の要素を代入しながら
# 配列の要素数だけ繰り返します。
arr = ['apple', 'orange', 'grape']
arr.each do |item|
print item + ', ' # apple, orange, grape,
end
# 配列の要素とともに要素の番号も扱い時は、each_with_indexメソッドを使用
arr = ['apple', 'orange' ,'grape']
arr.each_with_index do |item, i|
print "#{i}.#{item}";
print ', ' if i < arr.lentgh -1
end
例外処理
begin
f = File.open('notexist.txt')
print f.gets
f.close
rescue SystemCallError => e # SystemCallErrorによる例外をキャッチ
warn 'SystemCallError: ' + e.message
rescue Exception => e # その他の例外をキャッチ
warn 'Exception: ' + e.message
end
「?」「!」がつくメソッド
# 「?」が付くメソッドは、trueまたはfalseを返すことを表す。
if message.empty? .... # 空かどうかを調べる
if array.include?('apple') .... # appleが含まれるかどうか調べる
s1= 'hello'
puts s1.class # String
puts s1.kind_of?(String) # true
puts s1.kind_of?(Object) # true
puts s1.instance_of?(String) # true
puts s1.instance_of?(Object) # false
# 「!」が付くメソッドは、そのオブジェクトの中身を変更することを表す。
s1 = 'hello'
s2 = s1
puts s2 # hello
s1.upcase!
puts s2 # HELLO
今後やりたいこと
xyzzyの色づけ
- Comments: 0
- Trackbacks: 0
Home > Tags > 新規タグの追加
- Search
- Feeds
- Meta