Get name of lambda function as string in Ruby

I have a defined a lambda function as my_lambda = lambda { |x| 100 * x } I am passing this function as an input to another function which needs to verify its identify, something like: def function_verifier(func) if func.to_s == "my_lambda" ...
more »

2017-10-03 21:10 (1) Answers

Rendering an image from the Unsplash API in Rails

I have extensively researched this matter both on Stack Overflow and Google but found nothing conclusive. Since I'm completely new to the concept of API usage within Rails I have to ask for some advice. I have followed the procedure from the github ...
more »

2017-10-03 19:10 (1) Answers

Rails, subtracting dates in nested model

Hoping someone can help me here, think I am not seeing something simple. I'm trying to subtract from a date field in a nested model. When I have: - Photo.minimum(:date) This works fine, and calculates the difference between...
more »

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

Error installing while installing unf_ext

I installed MacOS High Sierra yesterday, and I was testing my ruby code and it was failing due to one of my gems not being installed. I tried installing it today and got this back: Fetching gem metadata from Fetching v...
more »

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

How to use 'stale?' in rails with eTAg

I am new to rails. I am trying to use cache and Etag to throw 304 status when the content is not modified. My controller code is like this: def index @projects = @current_user.projects if stale?(@projects, public: true) render json:...
more »

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

Rails 5 custom validate

I am learning Rails and I stuck with this custom validation. I tried many different approaches to pass the form field called (:cpf) to validate without success. I also tried using ActiveModel::EachValidator, but I get more confused. The validation is...
more »

2017-09-26 22:09 (1) Answers

Ruby to_s a special method?

I'm working on Ruby Koans about_to_str.rb. I've gotten it to work but some code I don't understand. I'm confused why the CanNotBeTreatedAsString classes to_s produces different results than the CanBeTreatedAsString to_s Both tests test_to_str_allows...
more »

2017-09-25 07:09 (0) 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

get all indexes of value nil in array

I have an array of N elements and this array contains either 0, 1 or nil. I want to get all indexes at which nil is present or sort the array so that all nil comes first. I am looking for an efficient way because the array size can be very large. H...
more »

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

Pass parameter to Validation - Rails 3

We are currently valid different sections via the following code: on_boarding_form.rb def valid? @sections.all? {|section| section.valid?} end This goes to on_boarding_section_form.rb. Where we do some checks validate do @fields.w...
more »

2017-09-11 09:09 (0) Answers

Object containing an array going to nil

I have a class Deck that represents an array of 81 cards but when I try to use my getter functions on any card in the array it gives the error undefined method getColor for nil:NilClass (NoMethodError) which I assume means that everything in my array...
more »

2017-09-11 00:09 (1) Answers

How do I remove a URL from a string in Ruby?

I'm using Ruby 2.4. I want to remove URLs from my string so I tried this puts "str before: #{my_str}" my_str.gsub!(/#{URI::regexp}/, '') puts "str after url sub: #{my_str}" but only the "http" is getting stripped off. Here is the output from the...
more »

2017-09-09 21:09 (1) Answers

Ruby on Rails: how to pass JSON to JavaScript

I have a JavaScript snippet in my Ruby on Rails application that talks to a remote server and passes it a JSON composed of an array of dictionaries containing things like SKU, price, etc. It's simple to generate in Ruby: @items = @order.line_items....
more »

2017-09-01 21:09 (1) Answers