動作環境はMacとなります。
主に自分の勉強用メモとして残しています。
オブジェクト指向プログラミング言語
- コンピュータ・プログラミングの概念の一つ。
- オブジェクト指向の概念や手法を取り入れたもの。
- プログラムを、データとその振る舞いが結びつけられたオブジェクトの集まりとして構成する。
オブジェクトとは?
データと処理の集まりのこと。文字列、配列、数値、nilなど全てオブジェクト。
オブジェクト指向とは?
オブジェクト同士が相互に関係しあうことで、システムの振る舞いを捉える考え方。
クラス
オブジェクトの設計図
インスタンス
クラス(設計図)から作成した、実態。
メソッド
- クラスの中に定義されていて、複数の処理を1つにまとめて、扱いやすくしたもの
- クラスに所属する関数のようなもの
数値(Numeric)
Numericは親クラス
- Numeric ・・・ 数値クラス
- Integer ・・・ 整数クラス(整数を扱うクラス)
- Float ・・・浮動小数点クラス(小数点以下を扱うクラス)
動的型付け
変数に格納した時に自動的に型が設定されるため、明示的にクラスを書く必要はない。
例)
i = 1はOK
int i = 0はNG
四則演算
irb(main):045:0> 1 + 1
=> 2
irb(main):046:0> 10 - 1
=> 9
irb(main):047:0> 13 * 2 #掛け算
=> 26
irb(main):048:0> 100 / 5 #割り算
=> 20
irb(main):049:0> x = 1 #xに1を代入
=> 1
irb(main):050:0> - x #入れた変数に-をつけると符号を反転させることができる
=> -1
割り算の注意点
irb(main):053:0* 3 / 2 #小数点の計算がされない
=> 1
#どちらかまたはどちらの数値にも小数点をつける必要がある
irb(main):054:0> 3.0 / 2
=> 1.5
irb(main):055:0> 3 / 2.0
=> 1.5
irb(main):056:0> 3.0 / 2.0
=> 1.5
irb(main):001:0> 10 % 3 #剰余
=> 1
irb(main):002:0> 10 ** 3 #べき乗
=> 1000
irb(main):004:0> x = 5 #5を代入
=> 5
irb(main):005:0> x.to_f #小数に変換
=> 5.0
irb(main):006:0> x.to_f / 7 #5を小数に変換し、7で割る
=> 0.7142857142857143
irb(main):007:0> x / 7 #5 / 7の出力結果 ※小数点は出力されない
=> 0
#クラスの確認
irb(main):008:0> 1.class
=> Integer
irb(main):009:0> 1.1.class
=> Float
#クラスのメソッドの確認
irb(main):010:0> 1.methods
=> [:%, :&, :*, :+, :-, :/, :<, :>, :^, :|, :~, :-@, :**, :<=>, :<<, :>>, :<=, :>=, :==, :===, :[], :inspect, :size, :succ, :to_int, :to_s, :to_i, :to_f, :next, :div, :upto, :chr, :ord, :coerce, :divmod, :fdiv, :modulo, :remainder, :abs, :magnitude, :integer?, :floor, :ceil, :round, :truncate, :odd?, :even?, :downto, :times, :pred, :bit_length, :digits, :to_r, :numerator, :denominator, :rationalize, :gcd, :lcm, :gcdlcm, :+@, :eql?, :singleton_method_added, :i, :real?, :zero?, :nonzero?, :finite?, :infinite?, :step, :positive?, :negative?, :quo, :arg, :rectangular, :rect, :polar, :real, :imaginary, :imag, :abs2, :angle, :phase, :conjugate, :conj, :to_c, :between?, :clamp, :instance_of?, :kind_of?, :is_a?, :tap, :public_send, :public_method, :singleton_method, :remove_instance_variable, :define_singleton_method, :method, :instance_variable_set, :extend, :to_enum, :enum_for, :=~, :!~, :respond_to?, :freeze, :object_id, :send, :display, :nil?, :hash, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variable_get, :instance_variables, :instance_variable_defined?, :!, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__]
文字列(String)
文字列はシングルクオートかダブルクオートで囲む。
挙動の違い
irb(main):006:0> "abcde"
=> "abcde"
irb(main):007:0> 'abcde'
=> "abcde"
#この時点での違いはない。
irb(main):001:0> puts "Ruby\nProgramming"
Ruby
Programming
=> nil
irb(main):002:0> puts 'Ruby\nProgramming'
Ruby\nProgramming
=> nil
#ダブルクオートで囲むと、文字列の途中で改行される
#シングルクオートで囲むと、改行されない
※バックスラッシュはoptionキー+¥キー
irb(main):003:0> first_name = "Yuta"
=> "Yuta"
irb(main):004:0> last_name = "Nakamura"
=> "Nakamura"
irb(main):006:0> "My name is #{first_name} #{last_name}"
=> "My name is Yuta Nakamura"
irb(main):007:0> 'My name is #{first_name} #{last_name}'
=> "My name is \#{first_name} \#{last_name}"
#ダブクオートで囲むと#{}で囲んだ文字列が式展開される
#シングルクオートではそのまま表示される
まとめ
ダブルクオートを使う場合・・・式展開、改行したい時
シングルクオートを使う場合・・・それ以外
その他文字列の使い方
irb(main):010:0> puts "kengo" + "kaneki"
kengokaneki
=> nil
irb(main):011:0> puts "kengo" + " " + "kaneki"
kengo kaneki
#スペースを開けたい場合、" "を連結
破壊的メソッド メソッド名の最後に!を記述
irb(main):017:0> name = 'ken'
=> "ken"
irb(main):018:0> puts name
ken
=> nil
#upcaseメソッドの破壊的メソッド
irb(main):019:0> name.upcase
=> "KEN"
irb(main):020:0> name
=> "ken"
#変数の中身に変化はない
irb(main):021:0> name.upcase!
=> "KEN"
irb(main):022:0> name
=> "KEN"
#変数の中身自体が大文字に書き換わる
クラスの確認
irb(main):023:0> "ken".class
=> String
メソッドの確認
irb(main):024:0> "ken".methods
=> [:include?, :%, :*, :+, :to_c, :unicode_normalize, :unicode_normalize!, :unicode_normalized?, :count, :partition, :unpack, :unpack1, :sum, :next, :casecmp, :casecmp?, :insert, :bytesize, :match, :match?, :succ!, :+@, :-@, :index, :rindex, :<=>, :replace, :clear, :upto, :getbyte, :==, :===, :setbyte, :=~, :scrub, :[], :[]=, :chr, :scrub!, :dump, :byteslice, :upcase, :next!, :empty?, :eql?, :downcase, :capitalize, :swapcase, :upcase!, :downcase!, :capitalize!, :swapcase!, :hex, :oct, :split, :lines, :reverse, :chars, :codepoints, :prepend, :bytes, :concat, :<<, :freeze, :inspect, :intern, :end_with?, :crypt, :ljust, :reverse!, :chop, :scan, :gsub, :ord, :start_with?, :length, :size, :rstrip, :succ, :center, :sub, :chomp!, :sub!, :chomp, :rjust, :lstrip!, :gsub!, :chop!, :strip, :to_str, :to_sym, :rstrip!, :tr, :tr_s, :delete, :to_s, :to_i, :tr_s!, :delete!, :squeeze!, :each_line, :squeeze, :strip!, :each_codepoint, :lstrip, :slice!, :rpartition, :each_byte, :each_char, :to_f, :slice, :ascii_only?, :encoding, :force_encoding, :b, :valid_encoding?, :tr!, :encode, :encode!, :hash, :to_r, :<, :>, :<=, :>=, :between?, :clamp, :instance_of?, :kind_of?, :is_a?, :tap, :public_send, :public_method, :singleton_method, :remove_instance_variable, :define_singleton_method, :method, :instance_variable_set, :extend, :to_enum, :enum_for, :!~, :respond_to?, :object_id, :send, :display, :nil?, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variable_get, :instance_variables, :instance_variable_defined?, :!, :!=, :__send__, :
空白文字の使い方
irb(main):025:0> 1 + 2
=> 3
irb(main):026:0> 1+2
=> 3
irb(main):027:0> 1 +2 + 3
=> 6
#スペースはいくら開けても開けなくても挙動に違いはない。
基本的にはスペースを一文字ずつ空けるのが定説