こちらの記事の Rubyで配列から重複している値を抽出するのを考えてみた。 http://d.hatena.ne.jp/kiyo560808/20110801/1312196444 a = [1, 2, 3, 4, 5, 6, 5, 4] a.uniq.reject{|i| a.index(i) == a.rindex(i)} #=> [4, 5] もしくは a = [1, 2, 3, 4, 5, 6, 5, 4] a.uniq.reject{|i| a.count(i) == 1} #=> [4, 5] こんな感じか? ふとパフォーマンスが気になったので調べてみた。 #!/usr/bin/env ruby # -*- coding: utf-8 -*- require 'benchmark' a = (1..10000).to_a a += [1, 10000] Benchmark.bm
Sergii Makagon Ruby Developer with more than 17 years of experience. I like TDD, Clean Code, Design Patterns and Domain-Driven Design. Read more. Follow @makagon We all work with arrays on daily basis. It's quite easy to start using arrays, no problem with that. But there are some interesting methods and behavior that I wanted to cover in this post. Let's start with this one: arr = [1, 2, 3] arr[9
I love the Ruby language, undoubtedly! When you love something, you should love not only the bright side but also the dark side of it too, unconditionally. Probably that is the message Katy Perry tries to deliver. In this post I am trying to point out some Ruby features you might want to use with a lot of caution. I have another post about five gotchas in Rails because I love the framework too. 1.
I am Benoit Hamelin. I work with the excellent hackers at Arcadia G.P., where I develop software, solve problems and convert strong tea into ideas --- hopefully good ones. This is my blog about programming languages, development methodologies, elegant hacks and all sorts of software trickery. Also, I've done a Ph.D. in medical imaging in a previous life, and I'm still curious about scientific adva
『るびま』は、Ruby に関する技術記事はもちろんのこと、Rubyist へのインタビューやエッセイ、その他をお届けするウェブ雑誌です。 Rubyist Magazine について 『Rubyist Magazine』、略して『るびま』は、Rubyist の Rubyist による、Rubyist とそうでない人のためのウェブ雑誌です。 最新号 Rubyist Magazine 0064 号 バックナンバー Rubyist Magazine 0064 号 Rubyist Magazine 0063 号 Rubyist Magazine 0062 号 Kaigi on Rails 特集号 RubyKaigi Takeout 2020 特集号 Rubyist Magazine 0061 号 Rubyist Magazine 0060 号 RubyKaigi 2019 直前特集号 Rubyist
In an array, deleting an index necessitates shifting back all the following episodes, and so is an O(n) operation; If your predicate rejects a fixed fraction of elements, the total runtime is this O(n²). This bug is fairly straightforward, but the part I find most interesting was why it was introduced. The code used to be linear, but it regressed in response to bug #2545, which concerned the behav
This guide covers PostgreSQL specific usage of Active Record. After reading this guide, you will know: How to use PostgreSQL's datatypes. How to use UUID primary keys. How to include non-key columns in indexes. How to use deferrable foreign keys. How to use unique constraints. How to implement exclusion constraints. How to implement full text search with PostgreSQL. How to back your Active Record
Short answer For an integer range : Enumerable#sum returns (range.max-range.min+1)*(range.max+range.min)/2 Enumerable#inject(:+) iterates over every element. Theory The sum of integers between 1 and n is called a triangular number, and is equal to n*(n+1)/2. The sum of integers between n and m is the triangular number of m minus the triangular number of n-1, which is equal to m*(m+1)/2-n*(n-1)/2,
Given: a1 = [5, 1, 6, 14, 2, 8] I would like to determine if it contains all elements of: a2 = [2, 6, 15] In this case the result is false. Are there any built-in Ruby/Rails methods to identify such array inclusion? One way to implement this is: a2.index{ |x| !a1.include?(x) }.nil? Is there a better, more readable, way?
dig(idx, ...) -> object | nil[permalink][rdoc][edit] self 以下のネストしたオブジェクトを dig メソッドで再帰的に参照して返します。途中のオブジェクトが nil であった場合は nil を返します。 [PARAM] idx: インデックスを整数で任意個指定します。 例 a = [[1, [2, 3]]] a.dig(0, 1, 1) # => 3 a.dig(1, 2, 3) # => nil a.dig(0, 0, 0) # => TypeError: Integer does not have #dig method [42, {foo: :bar}].dig(1, :foo) # => :bar [SEE_ALSO] Hash#dig, Struct#dig, OpenStruct#dig
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く