1 DAY 2 THE FOUNDATION OF RUBY 2011926
1 DAY 2: THE FOUNDATION OF RUBY 2011/9/26 dblab-study ruby seminar
method modifier(1) Limit where you can call the method. public Methods can be called anywhere. private Methods can be called in class. If not declared, then become a public treatment class Acc. Test def pub puts "this is a public method. " puts "and can call private methods" priv end def priv puts "this is private method. " end public : pub private : priv end >> acc = Acc. Test. new >> acc. pub this is a public method. and can call private methods this is private method. >> acc. priv #=> raise error!
method modifier(2) protected Can be accessed from the same class. Rarely use.
Class Inheritance(1) By inheritance, you can create a new class partially to customize and add new features to existing classes without modifying Ruby all classes are subclasses of the object
Class Inheritance(2) class My. Array < Array def sum res = 0 self. each do |i| res += i end res end >> array = Array. new([1, 2, 3]) >> puts array. sum raise error >> my_array = My. Array(array) >> puts my_array. sum 6
Class Inheritance(3) “super" can call methods in the parent class. Unlike java, you can call anywhere in the method. class My. Array < Array def empty? bool = super !bool end >> array = Array. new >> puts array. empty? false >> my_array = My. Array. new >> puts my_array. empty? true
Exercise 4 1. Add methods “each_to_s" to My. Array. “each_to_s" change each element to a String. 2. make sure you can override private methods 3. Override methods “[i]" to My. Array. when you see a larger size by specifying the array index, the index calculation is performed backwards from the exposed top.
Exercise 4(Execution example) >> my_array = My. Array. new([1, 2, 3]) >> puts my_array [1, 2, 3] >> puts my_array. each_to_s ["1", "2", "3"] >> puts my_array[1] 2 >> puts my_array[4] 2
module(1) Module is the ability to manage a group of method. Module can't create instance. Module can't inheritance.
Module(2) module My. Module def square n n * n end >> My. Module. square(3) #=> railse error! >> include My. Module >> puts My. Module. square(3) 9 >> puts square(2) 4 class My. Class include My. Module end >> m = My. Class. new >> puts m. square(4) 16 module My. Module 2 def double n n + n end module_function : double end >> puts My. Module 2. double(3) 6
Exception The end of the program raises an exception. If you know what to do to understand the possibility of raising an exception and it can be cording to do if an exception occurs. same as try and catch in Java. >> begin >> open("hoge. txt") #存在しないファイル >> rescue => e >> puts "raise error!" >> puts e. messages >> end railse error! No such file or directory - hoge. txt
Fixnum(1) Fixnum class is a class dealing with integers. Integer is treated as an instance of Fixnum class. Unlike java, there is no limit on size. >> puts 10. class Fixnum >> puts 10 + 10 20 >> puts 10. +(10) 20 >> 10 ** 10000. . .
Fixnum(2) times upto downto step >> >> >> 20 21 22 23 >> >> >> 20 19 18 >> >> >> 0 2 4 20. upto(23) do |i| puts i end 20. downto(18) do |i| puts i end 0. step(5, 2) do |i| puts i end
Exercise 5 1. Rewrite the fizzbuzz. 2. make sure you "-" Or "*" are methods of Fixnum object.
Array (1) array[n. . m] array[n, m] array[-n] >> array = [1, 2, 3, 4, 5, 6, 7] >> p array[1. . 3] [2, 3, 4] >> p array[1, 3] [2, 3, 4] >> p array[-1] 7
Array (2) array 1 + array 2 array 1 - array 2 array 1 & array 2 array 1 | array 2 >> array 1 = [1, 2, 3, 4] >> array 2 = [2, 4, 6] >> p array 1 + array 2 [1, 2, 3, 4, 2, 4, 6] >> p array 1 – array 2 [1, 3] >> p array 1 & array 2 [2, 4] >> p array 1 | array 2 [1, 2, 3, 4, 6]
Array (3) array. push(n) array. pop array. last array << n array. unshift(n) array. shift array. first >> array = [1, 2, 3] >> array. push(4) >> p array [1, 2, 3, 4] >> p array. pop 4 >> p array. last 3 >> array << 5 >> p array [1, 2, 3, 5] >> array. unshift(0) >> p array [0, 1, 2, 3, 5] >> p array. shift 0 >> p array. first 1
Array (4) array. delete array. uniq array. map array. flatten array. reverse array. sort >> array = [1, 1, 2, 2, 3, 3] >> array. delete(2) >> p array [1, 1, 3, 3] >> p array. uniq [1, 3] >> p array [1, 1, 3, 3] >> p array. map do |i| >> i. to_s >> end ["1", "3", "3"] >> array = [[1, 2], [3, 4]] >> p array. flatten [1, 2, 3, 4] >> array = [1, 4, 3, 5, 2] >> p array. reverse [2, 5, 3, 4, 1] >> p array. sort [1, 2, 3, 4, 5]
Exercise 6 1. 2. 3. 4. Confirm array methods. "word_count. txt" count word. Exercise 3 replace using the class. Calculate formulas written in Polish notation.
String + << size upcase downcase captalize >> puts "#{123}" 123 >> puts '#{123}' #{123} >> str = "abc" >> puts str + "def" abcdef >> str << "def" >> puts str abcdef >> puts str. size 6 >> puts "abc". upcase ABC >> puts "ABC". downcase abc >> puts "abc". captalize Abc
Symbol object A symbol is a number that is used internally to identify, such as the Ruby method name. Symbol will save memory because it has only one object. Symbol does not run faster than a string Symbol is the hash key, is used to name the model the rails and action. >> puts "c" == "c" true >> puts "c". equal? ("c") false >> puts : c == : c true >> puts : c. equal? (: c) true
Naming rules(1) Camel. Case Class name Snake. Case variable name method name class My. Class attr_accessor : my_name def to_s end
Naming rules(2) The name of the method that return boolean add “? " to the end. The name of the method that changes the state of the Object add “? " to the end. class My. Class attr_accessor : delete def valid? !@delete end def delete! @delete = true end >> m = My. Class. new >> p m. valid? true >> m. delete! >> p m. valid? false >> str = "abc" >> p str. upcase ABC >> p str abc >> str. upcase! >> p str ABC
Multiple Return Values You can return mulitple values at methods in ruby. def all_double value 1, value 2 return value 1*2, value 2*2 end >> v 1, v 2 = all_double(1, 4) >> puts v 1 2 >> puts v 2 8 >> >> 1 >> B >> C a, b, c = 1, "B", : C puts a puts b puts c
Class Override You can add methods and variables in a class that already exists in ruby. class My. Class def test puts “test" end >> m = My. Class. new >> m. test class My. Class def test puts “change" end def add puts “add methods" end >> m. test change >> m. add methods
Exercise 7 Exercise 3 in Class override to rewrite.
How to find methods. 1. read documents 2. search internet use “methods" methods is a object method that returns all the methods name with the object. class My. Class def hoge end >> m = My. Class. new >> puts m. methods inspect tap clone public_methods ・・・ hoge ・・・
reference たのしいRuby 第 3版 [単行本] Ruby 1. 9. 2 reference manual ISBN-13: 978 -4797357400 http: //www. amazon. co. jp/dp/4797357401 http: //doc. ruby-lang. org/ja/1. 9. 2/doc/index. html The Ruby Toolbox(rubygem information) http: //ruby-toolbox. com/
- Slides: 28