ruby

  1. S

    Accessing a specific item in a JSON return in Rails

    I have this JSON from the NHL API, and I tried to access the value gamesPlayed: "teams"=>[ { "id"=>5, "name"=>"Pittsburgh Penguins", "link"=>"/api/v1/teams/5", "venue"=>{ "id"=>5034, "name"=>"PPG Paints Arena", "link"=>"/api/v1/venues/5034"...
  2. B

    TCPSocket connection reset by peer

    require 'socket' socket = TCPSocket.open('stream-api.betfair.com', '443') while line = socket.gets puts line.chop end socket.close I should receive something like {"op":"connection","connectionId":"002-230915140112-174"} but I receive Connection reset by peer which betfair included a nodejs...
  3. F

    UML sequence diagram - how to represent method arguments that instantiate objects

    I'm not sure how to represent something like the following in a sequence diagram (in Ruby): class FirstClass def process thing = SecondClass.new('string argument', third_class, 2) end def third_class ThirdClass.new('another string argument',) end end The first...
  4. P

    Convert camelCase to dash-case (hypens) in pure ruby

    There are plenty of posts about the opposite way. But how to I convert camelCase to camel-case in ruby? My regex-game is pretty low ... here is it the other way around: def underscore(string) string.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2')...
  5. S

    Understanding routes with :path in Ruby on Rails

    I'm trying to understand routes in Ruby on Rails and it's very hard to find the right info just by googling. So this is my question: what routes does the following snippet define ? scope '/admin' do resource :photo, :only => [:edit], :path => '/d/:domain_name', :domain_name =>...
  6. S

    How to override a method of a ruby gem for certain conditions?

    I am trying to override one of the private methods of a gem only when certain conditions are met. I would like to use this method only if a certain condition is met else use the gem's method. module Net class FTP def makepasv if condition @sock.peeraddr[0] == 'AF_INET' ...
  7. R

    How many times does array appear in another array?

    I'm new to Ruby on Rails. This question seems to be answered in other programming languages but not RoR. I have an array with 3 items in it. array1 = [1, 2, 3] I then have items in another array which i want to check and see how many times array1 appear in array2: array2 = [1, 2, 3, 4, 5, 6...
  8. M

    RSpec/AnyInstance: Avoid stubbing using allow_any_instance_of problem

    Help me. I don't understand how to fix it. I tried to many variations... driver_iq_driver_spec.rb describe '.perform' do it 'correctly parse response' do driver = described_class.new(dot_application, background_check_type, provider_setting).perform expect(driver).to...
  9. S

    PG gem won't install in Rails app: Gem::Ext::BuildError: ERROR: Failed to build gem native extension

    I'm trying to deploy my Rails app to Heroku and when I add the pg gem to my gemfile and run bundle install I get an error: An error occurred while installing pg (1.1.3), and Bundler cannot continue. Make sure that `gem install pg -v '1.1.3'` succeeds before bundling. The bottom of my gemfile...
  10. R

    Where are the official Ruby 1.9 Docker Images?

    I ran the following Docker command from a Git Bash shell on Windows 10: docker pull ruby:1.9.3 to get the following output (I removed a couple lines for clarity): 1.9.3: Pulling from library/ruby ... Digest: sha256:c296bd581b8c1ee0585a77cafc0b14342356cea30a10fa2c9f82370909665149 Status: Image...
  11. T

    Rails wrap link_to on html code based on condition

    Is there a clean syntax to wrap link_to on html code based on condition? Because DRY, I don't want to repeat the same code. <% if condition? %> <%= link_to bla_bla_path do %> <p>Some html here</p> <% end %> <% else %> <p>Some html here</p> <% end %> I know there is link_to_if but it...
  12. E

    Given a hash of arrays, how to create an array of hashes with each possible combo

    Given a hash with values containing arrays of unequal lengths, {a: [1, 2, 3], b: [1, 2], c: [1]} is it possible to create an array of hashes containing the same keys, of all permutations of single values like the following without resorting to nesting multiple loops? [ {a: 1, b: 1, c: 1}...
  13. F

    Simple Form behaving different than form_for

    Simple_form <%= simple_form_for(@book, wrapper: :horizontal_form, wrapper_mappings: { }, html: { class: 'form-horizontal' }) do |f| %> <%= f.error_notification %> <div class="image-upload"> <%= f.input :book_cover, as: :file , input_html: { class: 'fa fa-cloud-download'}%> </div>...
  14. J

    Get value between brackets

    I have: item = "[a,b,c,d]" I want the value between []: "a,b,c,d" I tried: item.split(/,/) but this does not work. I tried split/each but neither works.
  15. D

    Decoding CSR using Ruby OpenSSL with SANs

    I have been using the Ruby openssl module to decode information from a CSR. I have the basic CSR decoded, but I cannot work out how to retrieve the Subject Alternate Names from the CSR. Code so far: require 'openssl' def parse_csr(csr) csr = OpenSSL::X509::Request.new csr puts...
  16. D

    How to splice array and merge back in Ruby?

    Say I have 2D array like so: [ 3, 4, 8, 12 ] [ 2, 6, 7, 16 ] [ 1, 10, 11, 15 ] [ 5, 9, 13, 14 ] I want to shift 6 in [1][1] and unshift it into [2][1] So that I can get the following: [ 3, 4, 8, 12 ] [ 2, 7, 11, 16 ] [ 1, 6, 10, 15 ] [ 5, 9...
  17. S

    How can I show together in the views multiple attributes from different entries, that have the same name, like “nested” attributes?

    In a rental system, I have a model called Boardgame, with the attributes: name, copy and situation. Situation is another model, with belongs_to. So an example of my database: Catan, copy 1, available War, copy 1, rented War, copy 2, available War, copy 3, rented My code in the index view is...
  18. C

    Cannot Query Parent with Many to Many Association - Rails

    I have 3 models with the following structure: class Customer has_many :invoices has_and_belongs_to_many :locations end class Invoice belongs_to :customer end class Location has_and_belongs_to_many :customers end I'm trying to write a query that will grab invoices that have...
  19. C

    Is there a better way cancel the current request and redirect to an error page in Rails?

    Frequently when I'm dealing with controllers I have found the need to stop the current action and then show an error page. Currently I have a method to show my error page in application controller: class ApplicationController < ActionController::Base def show_error msg render file...
  20. C

    Undefined method exists? for Draper::CollectionDecorator

    I am trying to implement the Draper gem for my Rails project and having trouble getting it to work. I'm getting the error message: undefined method `exists?' for #Draper::CollectionDecorator:0x000055b625da7a10> The error seems to be coming up from a partial I'm rendering on my index view. The...
Top