How to remove empty string in hash

hash = {“name”=>”XYZ”, “address”=>{“street”=>{“street_address”=>””,”city”=>”City name”}}, “form”=>{“id”=>11,”f_name”=>””}, “test”=>””} def remove_blanks hash hash.map do |k, v| v == ” ? nil : [k, v.is_a?(Hash) ? remove_blanks(v) : v] end.compact.to_h end remove_blanks hash #⇒ { # “address” => { # “street” => { # “city” => “City name” # } # }, # “form” => { # “id” … Read more

Write regular expressions [closed]

I would suggest something like this: /\) (?!.*\))(\S+)/ rubular demo Or if you don’t want to have capture groups, but potentially slower: /(?<=\) )(?!.*\))\S+/ rubular demo (?!.*\)) is a negative lookahead. If what’s inside matches, then the whole match will fail. So, if .*\) matches, then the match fails, in other terms, it prevents a … Read more

Rails validation required parameter (lock_version)

This type of validation is handled by validates_presence_of. Seems like you’re looking for validates_presence_of :lock_version, on: :update I strongly advise to take a look on http://apidock.com/rails/ActiveRecord/Validations/ClassMethods/validates_presence_of There are plenty of validations in Rails and this is probably the most basic one. http://guides.rubyonrails.org/active_record_validations.html

How to convert Time format in ruby

You can easily do that by using the strftime method, in this case it would be something like this: Time.now.strftime(“%d/%m/%Y %H:%M”) You can find the complete docs here: http://apidock.com/ruby/DateTime/strftime