Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # パーフェクトRuby
- # setter/getter
- class Ruler
- # setter
- def length=(val)
- @length = val
- end
- # getter
- def length
- @length
- end end
- ruler = Ruler.new
- ruler.length = 30
- ruler.length # 30
- **equivalent:**
- class Ruler
- attr_accessor :length
- end
- # self
- class Ruler
- attr_accessor :length
- def display_length
- # equivalent to `self.length`
- puts length
- end
- end
- **Can't omit** `**self**` **on assigning value**
- class Ruler
- attr_accessor :length
- def set_default_value
- # interpreted as assigning value to local variable if `self` ommited
- self.length = 30
- end
- end
- # Proc / lambda
- ## Proc
- # called on `Proc#call`
- greeter = Proc.new { |name| puts "Hello, #{name}!" }
- greeter.call "Proc" #=> "Hello, Proc!"
- ## lambda
- # proc method
- by_proc = proc { |name| puts "Hello, #{name}!" }
- # lambda method
- by_lambda = lambda { |name| puts "Hello, #{name}!" }
- # syntax sugar of lambda
- by_literal = ->(name) { |name| puts "Hello, #{name}!" }
- # === / case,when
- `===` **method changes behavior depends on the object**
- | method object | behavior |
- | ------------- | -------------------------------------------------------------------- |
- | Range | returns true if the given argument is in range |
- | Regexp | returns true if the given argument(String) is matched |
- | Proc | invokes the block with the given argument as the proc’s parameter |
- | Module, Class | returns true if the given argument is self or its subclass’ instance |
- `when` ‘s argument is compared using `===` under the hood
- stone = "ruby"
- detected =
- case stone
- when /ruby/
- "7月"
- when /peridot|sardonyx/
- "8月"
- else
- "unknown"
- end
- puts detected # "7月"
- # When to use `()` with methods
- One criteria is use `()` when you want to use returned value, and not use for just a statement
- member = find_member_by(name)
- set_current member
- # Block
- ## yield
- use `yield` to invoke parameter’s block
- def block_sample
- puts "stand up"
- yield
- puts "sit down"
- end
- block_sample do
- puts "walk"
- end
- parameter of `yield` gets to be block’s parameter
- def with_current_time
- yield Time.now
- end
- # `now` is `Time.now`
- with_current_time do |now|
- puts now.year
- end
- ## &block
- use `&block` to pass block as a parameter
- `block` is Proc object
- def block_sample(&block)
- puts "stand up"
- block.call if block
- puts "sit down"
- end
- block_sample do
- puts "walk"
- end
- # when to use `do … end` and `{…}`
- One criteria is:
- - Us `do … end` for multi-line block
- - Use `{…}` for single-line block
- And the other is:
- - Use `do … end` for expression to perform the procedure
- - Use `{…}` for for expression to return value
- # Class method
- ## class << self
- class Ruler
- attr_accessor :length
- # returns two Ruler objects array
- def self.pair
- [new, new]
- end
- end
- # equivalent
- class Ruler
- attr_accessor :length
- class << self
- def pair
- [new, new]
- end
- def trio
- [new, new, new]
- end
- end
- end
- # Enumerable module
- class FriendList
- include Enumerable
- def initialize(*friends)
- @friends = friends
- end
- # returns undefined method `each` error unless defined
- def each
- for v in @friends
- yield v
- end
- end
- end
- friend_list = FriendList.new("Alice", "Bob", "Charlie")
- upper_cased_friends = friend_list.map { |f| f.upcase }
- puts upper_cased_friends
- # extend
- `extend` gives behavors to the specific object, like class method
- module Greetable
- def greet_to(name)
- puts "Hello, #{name}"
- end
- end
- class Alice
- extend Greetable
- end
- Alice.greet_to "Bob"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement