ruby基礎①出力、変数、定数、クラスインスタンス、文字列オブジェクト、数字オブジェクト、I?に関して、配列オブジェクト
テックエキスパート 最終課題を終え、基礎固めのためドットインストールのrubyを復習しています!
電車の中でさらっと確認できる様な内容にしていますので、是非とも見て見ください。
point
そもそも文字列とか数値にはクラスがあって、それぞれのクラスにメゾットが用意されていること。
最終課題でコントローラーの中で配列を用意して、orderメゾットを使ったらエラー吐いたけど、今思えば、配列クラスにはorderメゾットはないのよね。。。
オブジェクトがどのクラスにいるのかは常に意識しないと!!!
# ruby の出力方法 print "hallo world" #改行無し puts "hallo world" #改行あり p "hallo world" #デバック用 オブジェクトの種類がわかる # => # hallo worldhallo world # "hallo world" # ----------------------------------------------------------------- # 変数 # 英小文字 msg = "hallo world" puts msg msg = "hallo world again" puts msg # ----------------------------------------------------------------- # 定数 # 英大文字 MSG = "hallo world" puts MSG MSG = "hallo world again" puts MSG # エラーが出るが実際には書き換わってしまうので、エラー文を見逃さない様に # ----------------------------------------------------------------- # インスタンスとクラス # 全てのオブジェクトはクラスがある。 # またクラスのそれぞれのメゾットがある。 # String class "hello world" "hello world".reverse "hello world".length # Float class 1.1 1.1.round 1.1.floor #"hello world",1.1などは実際はインスタンスと呼ぶ # ----------------------------------------------------------------- # 数値クラスとそのメゾット p 4.5.class p 4.5.methods # => # Float # [:%, :*, :+, :-, :/, :<, :>, :-@, :**, :<=>, :<=, :>=, :==, :===, :eql?, :inspect, :to_int, :to_s, :to_i, :to_f, :hash, :coerce, :divmod, :fdiv, :modulo, :abs, :magnitude, :zero?, :floor, :ceil, :round, :truncate, :positive?, :negative?, :quo, :nan?, :infinite?, :finite?, :next_float, :prev_float, :to_r, :numerator, :denominator, :rationalize, :arg, :angle, :phase, :+@, :singleton_method_added, :div, :i, :remainder, :real?, :integer?, :nonzero?, :step, :rectangular, :rect, :polar, :real, :imaginary, :imag, :abs2, :conjugate, :conj, :to_c, :between?, :instance_of?, :public_send, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :private_methods, :kind_of?, :instance_variables, :tap, :define_singleton_method, :is_a?, :public_method, :extend, :singleton_method, :to_enum, :enum_for, :=~, :!~, :respond_to?, :freeze, :display, :object_id, :send, :method, :nil?, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :trust, :untrusted?, :methods, :protected_methods, :frozen?, :public_methods, :singleton_methods, :!, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__] # ----------------------------------------------------------------- # 文字列クラス # ""特殊文字 式展開ができる # '' puts "hel\nlo wor\tld" #改行スペースが適応 puts 'hel\nlo wor\tld' #改行スペースが適応されない puts "price #{3000 * 4}" puts 'price #{3000 * 4}' # => # price 12000 # price #{3000 * 4} name = "sakurai" puts "hello #{name}" puts "hallo" + "world" puts "hallo" * 10 # => # halloworld # hallohallohallohallohallohallohallohallohallohallo #string class method # [:include?, :%, :unicode_normalize, :*, :+, :unicode_normalize!, :to_c, :unicode_normalized?, :count, :partition, :unpack, :encode, :encode!, :next, :casecmp, :insert, :bytesize, :match, :succ!, :next!, :upto, :index, :rindex, :replace, :clear, :chr, :+@, :-@, :setbyte, :getbyte, :<=>, :<<, :scrub, :scrub!, :byteslice, :==, :===, :dump, :=~, :downcase, :[], :[]=, :upcase, :downcase!, :capitalize, :swapcase, :upcase!, :oct, :empty?, :eql?, :hex, :chars, :split, :capitalize!, :swapcase!, :concat, :codepoints, :reverse, :lines, :bytes, :prepend, :scan, :ord, :reverse!, :center, :sub, :freeze, :inspect, :intern, :end_with?, :gsub, :chop, :crypt, :gsub!, :start_with?, :rstrip, :sub!, :ljust, :length, :size, :strip!, :succ, :rstrip!, :chomp, :strip, :rjust, :lstrip!, :tr!, :chomp!, :squeeze, :lstrip, :tr_s!, :to_str, :to_sym, :chop!, :each_byte, :each_char, :each_codepoint, :to_s, :to_i, :tr_s, :delete, :encoding, :force_encoding, :sum, :delete!, :squeeze!, :tr, :to_f, :valid_encoding?, :slice, :slice!, :rpartition, :each_line, :b, :ascii_only?, :to_r, :hash, :<, :>, :<=, :>=, :between?, :instance_of?, :public_send, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :private_methods, :kind_of?, :instance_variables, :tap, :define_singleton_method, :is_a?, :public_method, :extend, :singleton_method, :to_enum, :enum_for, :!~, :respond_to?, :display, :object_id, :send, :method, :nil?, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :trust, :untrusted?, :methods, :protected_methods, :frozen?, :public_methods, :singleton_methods, :!, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__] # ----------------------------------------------------------------- # ?!に関して # ! # upcase 大文字にして返す # upcase! 大文字にして返す また元の文字列も大文字に書き換えてします。→元の文字列も書き換えるため破壊的メゾットと呼ばれる。 name = "sakurai" puts name.upcase puts name puts name.upcase! puts name # => # SAKURAI # sakurai # SAKURAI # SAKURAI #? 真偽値 name = "sakurai" p name.empty? p name.include?("s") # => # false # true # ----------------------------------------------------------------- # 配列クラス colors = ["red","blue","yellow"] p colors[0] p colors[-1] p colors[0..2] p colors[0...2] p colors[5] # => # "red" # "yellow" # ["red", "blue", "yellow"] # ["red", "blue"] # nil colors[0] = "pink" colors[1..2] = ["white","black"] p colors # => # ["pink", "white", "black"] colors.push("gold") colors << "silver" p colors # => # ["pink", "white", "black", "gold", "silver"] p colors.size p colors.sort # => # 5 # ["black", "gold", "pink", "silver", "white"]