Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Selfを考える

120 views

Published on

第80回Ruby関西勉強会で発表した ruby の self についての話しです。

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

Selfを考える

  1. 1. self @ 80 ruby
  2. 2. • : • SNS: @yalab • : • : ( ) • ruby : since 1.6 • : ruby trunk mjit merge
  3. 3. 1 name = "Yoshida" # 2 name.size #
  4. 4. 1 name = "Yoshida" # 2 name.size #
  5. 5. 1 name = "Yoshida" # 2 name.size #
  6. 6. 1 public_methods
  7. 7. • ) • )
  8. 8. 1 self.public_methods
  9. 9. "self" Y^Y^Y^Y^Y^Y
  10. 10. self
  11. 11. 1 p self #=> main 2 3 class A 4 p self #=> A 5 def method 6 p self #=> #<A:0x007fdf1002cf28> 7 end 8 9 def self.classmethod 10 p self #=> A 11 end 12 end 13 14 A.module_eval do 15 p self #=> A 16 end
  12. 12. 1 class Animal 2 def type 3 :animal 4 end 5 end 6 7 class Human < Animal 8 def initialize(name) 9 @name = name 10 end 11 12 def name 13 "#{@name}(#{type})" #<- type 14 end 15 end
  13. 13. 1 class User < ApplicationRecord 2 validates :name, presence: true 3 end
  14. 14. 1 class User < ApplicationRecord 2 self.validates(:name, {presence: true}) 3 end
  15. 15. 1 class User < ApplicationRecord 2 validates :name, presence: true 3 end
  16. 16. )private self
  17. 17. self
  18. 18. 1 p self.class #=> 2 p self.class.ancestors #=> 3 p self.instance_variables #=> 4 p self.methods #=> 5 p self.methods.grep /write/ #=> 6 p self.method(:tap).source_location #=>
  19. 19. Object,Class,Module https://docs.ruby-lang.org/ja/2.5.0/class/Object.html https://docs.ruby-lang.org/ja/2.5.0/class/Class.html https://docs.ruby-lang.org/ja/2.5.0/class/Module.html
  20. 20. self ruby
  21. 21. self self

×