(ruby 2.5) Yields self to the block and returns the result of the block.
"my string".yield_self {|s| s.upcase } #=> "MY STRING"
3.next.yield_self {|x| x**x }.to_s #=> "256"
enum_for(method = :each, *args){|*args| block} → enum Creates a new Enumerator which will enumerate by calling method on obj, passing args if any.
str = "xyz"
enum = str.enum_for(:each_byte)
enum.each { |b| puts b }
# => 120
# => 121
# => 122
# protect an array from being modified by some_method
a = [1, 2, 3]
some_method(a.to_enum)
Returns the freeze status of obj.
a = [ "a", "b", "c" ]
a.freeze #=> ["a", "b", "c"]
a.frozen? #=> true
In general, clone and dup may have different semantics in descendant classes. While clone is used to duplicate an object, including its internal state, dup typically uses the class of the descendant object to create the new instance.
When using dup, any modules that the object has been extended with will not be copied.
class Klass
attr_accessor :str
end
module Foo
def foo; 'foo'; end
end
s1 = Klass.new #=> #<Klass:0x401b3a38>
s1.extend(Foo) #=> #<Klass:0x401b3a38>
s1.foo #=> "foo"
s2 = s1.clone #=> #<Klass:0x401b3a38>
s2.foo #=> "foo"
s3 = s1.dup #=> #<Klass:0x401b3a38>
s3.foo #=> NoMethodError: undefined method `foo' for #<Klass:0x401b3a38>
address = params[:account].try(:[], :owner).try(:[], :address)
# or
address = params[:account].fetch(:owner) .fetch(:address)
You can now simply use Hash#dig and accomplish the same thing:
address = params.dig(:account, :owner, :address)
account = Account.new(owner: nil) # account without an owner
account.owner.address
# => NoMethodError: undefined method `address' for nil:NilClass
account && account.owner && account.owner.address
# => nil
account.try(:owner).try(:address)
# => nil
account&.owner&.address
# => nil
Classname of the object
Returns an integer identifier for obj.
Returns true if obj responds to the given method.
class Klass
def hello(*args)
"Hello " + args.join(' ')
end
end
k = Klass.new
k.send :hello, "gentle", "readers" #=> "Hello gentle readers"
def my_method
puts "reached the top"
yield
puts "reached the bottom"
end
my_method do
puts "reached yield"
end
# output
reached the top
reached yield
reached the bottom
def my_method
yield("John", 2)
puts "Hi #{name}"
end
my_method { |name, age| puts "#{name} is #{age} years old" }
# output
John is 2 years old
NameError: undefined local variable or method `name' for #<IRB::...>
more on blocs : https://mixandgo.com/learn/mastering-ruby-blocks-in-less-than-5-minutes
-
Object#yield_self --> https://docs.ruby-lang.org/en/2.5.0/Object.html#method-i-yield_self
-
Proc#curry --> https://ruby-doc.org/core-2.5.3/Proc.html#method-i-curry
-
Method#curry --> https://ruby-doc.org/core-2.5.3/Method.html#method-i-curry
-
Enumerable#inject
-
Enumerable#map
-
Kernel#callcc https://ruby-doc.org/core-2.2.0/Continuation.html
-
Enumerator#lazy https://ruby-doc.org/core-2.2.0/Enumerator/Lazy.html
10 new features in Ruby 2.5
: https://blog.jetbrains.com/ruby/2017/10/10-new-features-in-ruby-2-5/