Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. # パーフェクトRuby
  2.  
  3. # setter/getter
  4. class Ruler
  5. # setter
  6. def length=(val)
  7. @length = val
  8. end
  9.  
  10. # getter
  11. def length
  12. @length
  13. end end
  14.  
  15. ruler = Ruler.new
  16. ruler.length = 30
  17. ruler.length # 30
  18.  
  19. **equivalent:**
  20.  
  21. class Ruler
  22. attr_accessor :length
  23. end
  24. # self
  25. class Ruler
  26. attr_accessor :length
  27.  
  28. def display_length
  29. # equivalent to `self.length`
  30. puts length
  31. end
  32. end
  33.  
  34. **Can't omit** `**self**` **on assigning value**
  35.  
  36.  
  37. class Ruler
  38. attr_accessor :length
  39.  
  40. def set_default_value
  41. # interpreted as assigning value to local variable if `self` ommited
  42. self.length = 30
  43. end
  44. end
  45. # Proc / lambda
  46. ## Proc
  47. # called on `Proc#call`
  48. greeter = Proc.new { |name| puts "Hello, #{name}!" }
  49. greeter.call "Proc" #=> "Hello, Proc!"
  50. ## lambda
  51. # proc method
  52. by_proc = proc { |name| puts "Hello, #{name}!" }
  53. # lambda method
  54. by_lambda = lambda { |name| puts "Hello, #{name}!" }
  55. # syntax sugar of lambda
  56. by_literal = ->(name) { |name| puts "Hello, #{name}!" }
  57. # === / case,when
  58.  
  59. `===` **method changes behavior depends on the object**
  60.  
  61. | method object | behavior |
  62. | ------------- | -------------------------------------------------------------------- |
  63. | Range | returns true if the given argument is in range |
  64. | Regexp | returns true if the given argument(String) is matched |
  65. | Proc | invokes the block with the given argument as the proc’s parameter |
  66. | Module, Class | returns true if the given argument is self or its subclass’ instance |
  67.  
  68. `when` ‘s argument is compared using `===` under the hood
  69.  
  70.  
  71. stone = "ruby"
  72.  
  73. detected =
  74. case stone
  75. when /ruby/
  76. "7月"
  77. when /peridot|sardonyx/
  78. "8月"
  79. else
  80. "unknown"
  81. end
  82.  
  83. puts detected # "7月"
  84. # When to use `()` with methods
  85.  
  86. One criteria is use `()` when you want to use returned value, and not use for just a statement
  87.  
  88.  
  89. member = find_member_by(name)
  90. set_current member
  91.  
  92.  
  93. # Block
  94. ## yield
  95.  
  96. use `yield` to invoke parameter’s block
  97.  
  98. def block_sample
  99. puts "stand up"
  100. yield
  101. puts "sit down"
  102. end
  103.  
  104. block_sample do
  105. puts "walk"
  106. end
  107.  
  108. parameter of `yield` gets to be block’s parameter
  109.  
  110. def with_current_time
  111. yield Time.now
  112. end
  113.  
  114. # `now` is `Time.now`
  115. with_current_time do |now|
  116. puts now.year
  117. end
  118. ## &block
  119.  
  120. use `&block` to pass block as a parameter
  121. `block` is Proc object
  122.  
  123.  
  124. def block_sample(&block)
  125. puts "stand up"
  126. block.call if block
  127. puts "sit down"
  128. end
  129.  
  130. block_sample do
  131. puts "walk"
  132. end
  133. # when to use `do … end` and `{…}`
  134.  
  135. One criteria is:
  136.  
  137. - Us `do … end` for multi-line block
  138. - Use `{…}` for single-line block
  139.  
  140. And the other is:
  141.  
  142. - Use `do … end` for expression to perform the procedure
  143. - Use `{…}` for for expression to return value
  144. # Class method
  145. ## class << self
  146. class Ruler
  147. attr_accessor :length
  148.  
  149. # returns two Ruler objects array
  150. def self.pair
  151. [new, new]
  152. end
  153. end
  154.  
  155. # equivalent
  156. class Ruler
  157. attr_accessor :length
  158.  
  159. class << self
  160. def pair
  161. [new, new]
  162. end
  163.  
  164. def trio
  165. [new, new, new]
  166. end
  167. end
  168. end
  169. # Enumerable module
  170. class FriendList
  171. include Enumerable
  172.  
  173. def initialize(*friends)
  174. @friends = friends
  175. end
  176.  
  177. # returns undefined method `each` error unless defined
  178. def each
  179. for v in @friends
  180. yield v
  181. end
  182. end
  183. end
  184.  
  185. friend_list = FriendList.new("Alice", "Bob", "Charlie")
  186. upper_cased_friends = friend_list.map { |f| f.upcase }
  187. puts upper_cased_friends
  188. # extend
  189.  
  190. `extend` gives behavors to the specific object, like class method
  191.  
  192.  
  193. module Greetable
  194. def greet_to(name)
  195. puts "Hello, #{name}"
  196. end
  197. end
  198.  
  199. class Alice
  200. extend Greetable
  201. end
  202.  
  203. Alice.greet_to "Bob"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement