How does Ruby's max function order duplicates?

I've been looking at the max method in Ruby's Enumerable mixin (v2.4.1). It's a fairly straightforward method, but how it presents items when duplicates are present is a bit confusing. For example: x = [1,2,3,4,5,6,7,8,9] x.max {|a, b| a%2 <=&g...
more »

2017-09-18 21:09 (0) Answers

Rails pagination - faster count of total pages?

I'm using the Kaminari gem to paginate my searches: @outfits = Outfit.where("description LIKE ?", "%warm%").page(params[:page]).per(20) And using this code to decide whether or not to show more: <% unless @outfits.current_page == @outfits.tota...
more »

2017-09-18 19:09 (2) Answers

Storing string in arrays in ruby

I am attempting to write a method that changes one string to another one letter at a time. However, I never noticed in ruby that if you push a string variable to an array it seems to push the address of the string variable and not the string. def mu...
more »

2017-09-17 21:09 (2) Answers

Why is rubocop failing only on my branch changes?

In our project rubocop.yml we have following check for number of lines in a class: ClassLength: Max: 150 # Default 100 There's a file in lib/utils/foo.rb which is already more than 200 lines. If I run rubocop inspection on master branch then r...
more »

2017-09-17 20:09 (1) Answers

ruby include method name collision

If a class includes many modules, and the methods gained from the include call private methods from their originating modules, and private methods exist in other modules with the same name, it should use the definition from its own module. In code: ...
more »

2017-09-17 16:09 (2) Answers

Inheriting from class Module

I am trying to understand the shrine gem source code that is a toolkit for file attachments. You can define an uploader on your model in rails like so: class Picture < ApplicationRecord include end The ...
more »

2017-09-17 08:09 (3) Answers

Ruby: iterate and nest hashes on condition

I have two arrays of hashes like this: h1=[{id:1, item:1, from:,9,4,6,0,0,'+0300'), to:,9,4,17,59,59,'+0300'), value:10}, {id:1, item:2, from:,9,4,18,0,0,'+0300'), to:,9,4,2...
more »

2017-09-17 06:09 (1) Answers

Custom relationship similar to dependent destroy

I would like to implement certain relationship between 2 models. I have 2 models: quiz and question that have many-to-many relationship. Quiz model have quiz_flag and question model have question_flag. What I want to happen is when quiz_flag is cha...
more »

2017-09-16 20:09 (2) Answers

Ruby loop create empty lines

I'm newbie in Ruby (frankly, I've just started learning it for fun, without any future plans), and I've noticed strange behaviour of loops. I assume my problem comes from lacks in knowledge in the mechanics of Ruby. boysNames = ["Martin", "Lucas", "...
more »

2017-09-16 17:09 (1) Answers

form_for models with specific relations

I have Models class Group < ApplicationRecord has_many :group_artists has_many :singers, -> { where role: "Singer" }, class_name: "GroupArtist" has_many :guitarists, -> { where role: "Guitarist" }, class_name: "GroupArtist" end cla...
more »

2017-09-16 16:09 (3) Answers

unable to locate libxml2 when installing gem

Problem with install of twilio-ruby. I have installed libxml2 using homebrew and confirmed it is there. However when I try running gem install twilio-ruby I recieve an error that it cannot find libxml2. I have tried specifying the path to libxml2 ...
more »

2017-09-16 07:09 (1) Answers

RuntimeError: can't modify frozen Array

I updated my Ruby on Rails app to Ruby on Rails 5.1.4. When I run my tests it returns me following error: An error occurred while loading ./spec/views/surveys/results.html.haml_spec.rb. Failure/Error: require File.expand_path('../../config/environme...
more »

2017-09-15 14:09 (0) Answers

Generating a LOT of unique tokens in Ruby

I needed to generate a lot of unique codes, for two lists of codes, that cannot match with each other and be unique codes. It has to be seven characters in length. BUT I'm not satisfied with my final solution, that I had to let run all night. I wo...
more »

2017-09-15 09:09 (2) Answers

Can't modify lines of a string

I have a string file, and I want to delete some of its lines. When I use some string functions as delete, replace, gsub, clear, etc. I get my string variable to the original state. For example: file # => "#include <hidef.h> /* for EnableIn...
more »

2017-09-15 04:09 (1) Answers

OpenSSL key length too short in Ruby, not in Bash

I originally experimented with a simple encryption script in Bash and it worked pretty much as expected. However, I'm now trying to do the same thing in Ruby and the Ruby version seems function a little differently. Bash Encrypt echo 'hello' | ope...
more »

2017-09-15 02:09 (1) Answers


When I use the "exchange_to (: CLP) .to_i" method, it gives me the error Money :: Bank :: GoogleCurrencyFetchError in OrdersController#create Money :: Bank :: GoogleCurrencyFetchError # class OrdersController < ApplicationController def create ...
more »

2017-09-14 22:09 (2) Answers

How to insert data in elastic search using ruby

I am trying to insert data using elastic search PUT with the Elasticsearch gem in ruby, but I am getting method not found error. Here is the sample code: def self.insert_data_in_es name = (here I am giving the url) body = (actual data) El...
more »

2017-09-14 19:09 (1) Answers

How to multiply integer digits between them?

I want to my n to multiply with next number for example if n=99 i want it to 9*9 and then return a result, and then i want the result (9*9 = 81 then 8*1 = 8) to multiply until it becomes 1 digit. Here's my code: def persistence(n) if n <= 9 ...
more »

2017-09-14 18:09 (2) Answers

Configuration in Allure to Cucumber in Ruby

I need to configure the following params: environment, trend, history, executors, retries, etc. I need these params for cucumber to work with ruby. I searched in a lot of places and I did not find much. I would appreciate if you can provide thes...
more »

2017-09-14 15:09 (1) Answers

Sublime Text 3: Ruby syntax highlighting bug

On 13th September Sublime released the first non beta version of Sublime Text 3. I just did the upgrade and now I have issues with the "improved syntax highlighting" when I use Ruby. def foo @bar / @baz end So if I want to divide instance vari...
more »

2017-09-14 12:09 (1) Answers