开发者

Need some nice explanation on these code snippets - Ruby

开发者 https://www.devze.com 2023-03-25 18:35 出处:网络
The First One module A include B def initialize ----- end def x --------开发者_如何学JAVA- self << Y.new# I need some explanation on this please

The First One

module A
  include B
  def initialize
     -----
  end
  def x
    --------开发者_如何学JAVA-
    self << Y.new     # I need some explanation on this please
    --------
  end
end

The Second One is

class H
  include G
  include F

  EE = [.,.,.,]
  def << k     # I need some explanation here
    k.id?
    -------
  end
end

Can some one please help me out! I am a newbie to the whole kind of programming


In both instances the << is being used as an operator.

self << Y.new is equivalent to self.send(:'<<', Y.new) so for instance if self was an Array, this would push Y.new into it.

In the second example you are defining the operator << and k is the argument .

0

精彩评论

暂无评论...
验证码 换一张
取 消