Ruby 2.5 added a new method named yield_self. It yields the receiver to the given block and returns output of the last statement in the block. 1irb> "Hello".yield_self { |str| str + " World" } 2 => "Hello World"Copy How is it different from try in Rails ? Without a method argument try behaves similar to yield_self. It would yield to the given block unless the receiver is nil and returns the output
Ruby 2.5 adds a very interesting method Object#yield_self. This is its definition. Slightly simplified # object.yield_self {|x| block } → an_object # Yields self to the block and returns the result of the block. class Object def yield_self yield(self) end end At first, it doesn’t look like a noticeable feature. It just returns what the block returns. However it turns out that it’s akin to the pipe
RubyのcloneとdupとActiveRecordのcloneとdupは別物なのでしょうか? 同じものです。Method#owner でメソッドが定義されたクラスが得られますが、下記のいずれも true になります。 Piyo.new.method(:clone).owner == Object.method(:clone).owner` Piyo.new.method(:dup).owner == Object.method(:dup).owner` ただし、clone の場合は initialize_clone、dup の場合は initialize_dup メソッドが呼ばれます (参考: initialize_clone, initialize_dup and initialize_copy in Ruby: Jon Leighton)。 ActiveRecord の場合、Act
// ケース 1: === を用いるのと同じ結果になる評価式 Object.is(25, 25); // true Object.is("foo", "foo"); // true Object.is("foo", "bar"); // false Object.is(null, null); // true Object.is(undefined, undefined); // true Object.is(window, window); // true Object.is([], []); // false const foo = { a: 1 }; const bar = { a: 1 }; const sameFoo = foo; Object.is(foo, foo); // true Object.is(foo, bar); // false Object.is(foo, s
This webpage was generated by the domain owner using Sedo Domain Parking. Disclaimer: Sedo maintains no relationship with third party advertisers. Reference to any specific service or trade mark is not controlled by Sedo nor does it constitute or imply its association, endorsement or recommendation.
New version of this blog post: inside “ECMAScript 6: new OOP features besides classes”. Copying all properties of one object to another one is a common operation in JavaScript. This blog post explains ECMAScript 6’s implementation of it, which is called Object.assign(). This merging operation has a name in the JavaScript ecosystem that is only used there (and, unfortunately, clashes with classic O
V8 SpiderMonkey JavaScriptCore Chakra Carakan KJS Other ⬤ Minor difference (1 point) ⬤ Small feature (2 points) ⬤ Medium feature (4 points) ⬤ Large feature (8 points) Compilers/polyfills Desktop browsers Servers/runtimes Mobile Feature name Current browser ES6 Trans- piler Traceur Babel 6 + core-js 2 Babel 7 + core-js 2 Babel 7 + core-js 3 Closure 2020.09 Closure 2021.08 Closure 2021.09 Closure 20
perlで動的にクラス名を決めたいときはこんな感じに突っ込んで呼び出してやるといけるんですけど、 use strict; use warnings; package Hoge; sub hello { print "hello\n"; } package main; my $str = "Hoge"; $str->hello(); これ、rubyで同じことやろうとすると、ruby的にはこの$strは文字列なのでそんなメソッドありませんよというエラーがかえってくるのでしょうが無いからしらべたらObject.const_getを使えばいいということだったのでメモしておく。(ググり方が悪かったのか、メタプログラミング ruby Classとかやっても最初でてこなかった) class Hoge def hello puts "hello" end end str = "Hoge" Object.c
Ruby on Rails 7.1.3.4 Class ActiveSupport::HashWithIndifferentAccess < Hash activesupport/lib/active_support/hash_with_indifferent_access.rb Hash With Indifferent Access Implements a hash where keys :foo and "foo" are considered to be the same. rgb = ActiveSupport::HashWithIndifferentAccess.new rgb[:black] = '#000000' rgb[:black] # => '#000000' rgb['black'] # => '#000000' rgb['white'] = '#FFFFFF'
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く