'Hello world'.class # String nil.class # NilClass String.class # Class String.ancestors # [String, Comparable, Object, Kernel, BasicObject]; nil.nil? # true Object.new.methods # Object; - nil.respond_to?('nil?') # true
Regarding the latter: it is important for duck typing . a == b # , a.==(b) # .==() -
nil.respond_to?('nil?') # true nil.respond_to? 'nil?' # true # : if nil.respond_to? 'nil?' puts 'ok' end # if 10.between? 1, 5 puts 'ok' end # , if 10.between?(1, 50) && 20.between?(1, 50) puts 'ok' end
a = :nil? # b = 'nil?' # nil.respond_to? a # true nil.respond_to? b # true # a == b # false a.to_s == b && a == b.to_sym # true;
a = {:key1 => 'value1', :key2 => 'value2'} # ( ) a = {key1: 'value1', key2: 'value2'} # - , (Ruby >= 1.9.3)
Since the topic has come: a = ['value1', 'value2'] # s = 'String' s = "Double-quoted #{s}" # "Double-quoted String" - , ,
%w[value1 value2] # ["value1", "value2"] - , %i[value1 value2] # [:value1, :value2] - , (Ruby >= 2.0) s = %q(String) s = %Q(Double-quoted #{s}) %x('ls') # `ls` # %r(.*) == /.*/ # true;
a = 10; puts a # 10 if nil.respond_to? 'nil?'; puts 'ok'; end # -
But it is better to write single-line if puts 'ok' if nil.respond_to? 'nil?'
class Foo def bar 10 # - end def baz(a) bar + 20 end end puts Foo.new.baz(10) # 30
module B # class Foo # def initialize(k) @k = k end # def bar; @k + 20 end end end puts B.Foo.new(3).bar # 23 puts B::Foo.new(3).bar # 23; , module C # def bar; @k + 25 end end class Foo include C; def initialize(k) @k = k end end puts Foo.new(3).bar # 28
class Foo def bar; 20 end end class Foo # def baz; bar + 2 end end puts Foo.new.baz # 22 # - : Foo.class_eval do def bazz; bar + 3 end end puts Foo.new.bazz # 23 # - class Boo < Foo def boo; bar + 2 end end puts Boo.new.boo # 22 # - , a = Foo.new a.instance_eval do # def booo; bar + 3 end end puts a.booo # 23 puts Foo.new.booo rescue puts 'error' # error; , puts a.respond_to? :booo # true puts Foo.new.respond_to? :booo # false # - def a.booboo bar + 4 end puts a.booboo # 24
And what if to make instance_eval for a class? Of course, static methods will be added. class Foo def self.bar; 10 end # end puts Foo.bar # 10 Foo.instance_eval do def baz; bar + 1 end end puts Foo.baz # 11
You can play with it enough, just remember - take care of your feet. class Foo def self.add_bar # , self.class_eval do def bar; 'bar' end end end end puts Foo.new.respond_to? :bar # false class Boo < Foo # add_bar # end puts Boo.new.bar # bar # , Foo.instance_eval do def add_baz self.class_eval do def baz; 'baz' end end end end class Baz < Foo add_baz end puts Baz.new.baz # baz
class Foo def bar # @bar # end def bar=(val) # @bar = val # end end
class Foo attr_accessor :bar, :baz # attr_reader :boo # attr_writer :booo # end
# bar # barr 0 # baz - true def foo(bar, barr = 0, baz: true) baz && bar + barr end p foo 1 # 1 p foo 1, 2 # 3 p foo 1, baz: false # false
def foo2(bar, *args) args end p foo2 1, 2, :baz => false # [2, {:baz=>false}] def foo3(bar, *args) options = args.extract_otions! # args p bar p args p options end foo3 1, 2, 3, :baz => false # 1 # [2, 3] # {:baz=>false}
def foo4(bar, baz:); bar end foo4 1 # foo4 1, baz: 2 # 1
Foo.instance_eval do # instance_eval def baz; bar + 1 end end
[1,2,3].each do |val| p val # , p(val) - shortcut puts(val.inspect) end # 1 2 3 # , [1,2,3].each { |val| p val } # 1 2 3 [1,2,3].each_with_index { |val, i| puts val.to_s + ' ' + i.to_s } #
def foo puts yield # puts yield + yield # , end foo { 2 } # 2 4 def bar(&block) # , puts yield block # end bar { 3 } # 3
proc = Proc.new { |a| a - 1 } # p proc.call(10) #9 p proc.class # Proc l = lambda { |a| a + 1 } # p l.call(10) #11 p l.class # Proc new_l = ->(a) { a + 2 } # (Ruby >= 2.0) p new_l.call(10) #12
require 'json/pure' # , github
source 'https://rubygems.org' gem 'json_pure'
require 'rubygems' require 'bundler/setup'
thread = Thread.new do # a = 0 1000000.times { a+= 1 } # puts a # end puts 'thread started' # thread.join #
fiber = Fiber.new do # Fiber.yield "fiber 1" # 'fiber 2' # end puts fiber.resume # puts 'context' puts fiber.resume #
a = 0 threads = [] 10.times do # 10 threads << Thread.new do # 100000.times { a+= 1 } # 100 000 end end threads.each(&:join) # puts a #
Source: https://habr.com/ru/post/206416/
All Articles