Ruby convert array to nested hash
Try this: array.reverse.inject(value) { |assigned_value, key| { key => assigned_value } } #=> {“this”=>{“is”=>{“a”=>{“test”=>42}}}}
Try this: array.reverse.inject(value) { |assigned_value, key| { key => assigned_value } } #=> {“this”=>{“is”=>{“a”=>{“test”=>42}}}}
the rails console never writes to the log file, but you can achieve it quite easily, for example, if you execute following after starting the rails console ActiveRecord::Base.logger = Logger.new STDOUT rails will log all SQL statements to stdout, thus display them in your terminal. and since Logger.new accepts any stream as first argument, you … Read more
Distance between two coordinates on earth is usually calculated using Haversine formula. This formula takes into consideration earth shape and radius. This is the code I use to calculate distance in meters. def distance(loc1, loc2) rad_per_deg = Math::PI/180 # PI / 180 rkm = 6371 # Earth radius in kilometers rm = rkm * 1000 … Read more
iltempo and buru’s answers helped me: sudo apt-get install libreadline-dev rvm remove 1.9.3 rvm install 1.9.3 Then add to your Gemfile: gem ‘rb-readline’
Steve’s answer works but since the instance is of class BaseUser when save is called, validations and callbacks defined in User will not run. You’ll probably want to convert the instance using the becomes method: user = BaseUser.where(email: “[email protected]”).first_or_initialize user = user.becomes(User) # convert to instance from BaseUser to User user.type = “User” user.save!
I faced this problem when I moved my project to another PC running a different version of JRuby. resolution: close RubyMine delete .idea/ folder re-open project in RubyMine
I do not think there is one best practice, but I’ll let you know what I do. I have a series of js files each for with their own purpose in the public/javascripts/ directory. Some examples could be utility.js chat.js shopping_basket.js and so on. I use asset packager and define one big fat collection for … Read more
This article gives some techniques for running Ruby code from Python which should also be applicable in the reverse direction (such as XML-RPC or pipes) as well as specific techniques for running Python code from Ruby. In particular rubypython or Ruby/Python look like they may do what you want.
Interesting question; something that I want to know more about as well…. So dug in a bit, and here is what I uncovered: Thoughtbot style guide dictum about let, etc. In an earlier version of the style guide, there’s more to that statement: Avoid its, let, let!, specify, subject, and other DSLs. Prefer explicitness and … Read more
Look at #shift from CSV Class: The primary read method for wrapped Strings and IOs, a single row is pulled from the data source, parsed and returned as an Array of fields (if header rows are not used) An Example: require ‘csv’ # CSV FILE # name, surname, location # Mark, Needham, Sydney # David, … Read more