[Solved] hash inside liste of hash extracted from array [closed]

I’m not sure what exactly you want to do, but you can try the following: arr = [“Japan”, “Egypt”, “Spain”, “Brazil”] arr.each { |a| Object.const_set(a, Hash.new(a)) } #initialize hashes Country = arr.map {|a| [a, 0]}.to_h #=> {“Japan”=>0, “Egypt”=>0, “Spain”=>0, “Brazil”=>0} or Country = Hash[arr.map {|a| [a, 0]}] #=> {“Japan”=>0, “Egypt”=>0, “Spain”=>0, “Brazil”=>0} Note: Ruby variables … Read more

[Solved] How to extract integer after “=” sign using ruby

I’d do something like this: string = <<EOT var i=0; var recharge=[]; var recharge_text=[]; var recharge_String=””; var mrp=””; var talktime=””; var validity=””; var mode=””;mrp=’1100′; talktime=”1200.00″; validity=’NA’; mode=”E-Recharge”; if(typeof String.prototype.trim !== ‘function’) { String.prototype.trim = function() { return this.replace(/^ +| +$/g, ”); } } mrp=mrp.trim(); if(isNaN(mrp)) { recharge_text.push({MRP:mrp, Talktime:talktime, Validity:validity ,Mode:mode}); } else { mrp=parseInt(mrp); recharge.push({MRP:mrp, … Read more

[Solved] extract string using ruby regex

You can try this also 2.1.2 :007 > s = “The Fashion Project – The Project of Fashion-Technology and Science-institute” => “The Fashion Project – The Project of Fashion-Technology and Science-institute” 2.1.2 :008 > s.split(/\s[-\b]\s/) => [“The Fashion Project”, “The Project of Fashion-Technology and Science-institute”] 2.1.2 :009 > 1 solved extract string using ruby regex

[Solved] result of passing a symbol to method [closed]

Symbol#empty is basically defined as self.to_s.empty? (with self being the symbol). So to answer your question why :””.empty? is true: It is because :””.to_s (the empty string) is empty. To adress your comment: :any_symbol.empty? is false, because :any_symbol.to_s.empty? is false. It’s the same thing. Maybe empty? is not the method you are looking for. Maybe … Read more

[Solved] Rails: Find record by created_at beginning_of_week

Because created_at is a DateTime object which includes both a Date and Time value, then your Message.find_by(created_at: Date.today.beginning_of_week) # Message Load (6.2ms) SELECT “messages”.* FROM “messages” WHERE “messages”.”created_at” = $1 LIMIT $2 [[“created_at”, “2018-01-29”], [“LIMIT”, 1]] … will try to find a record at exactly 2018-01-29 00:00:00 which is a Message record that is exactly … Read more

[Solved] Why learn Ruby on Rails [closed]

You are mixing some stuff: Ruby on Rails is a framework to create server side web applications using the Ruby language jQuery is a client side JavaScript library that simplifies writing JavaScript web clients Node.js is a server for the execution of server side JavaScript, thus providing a server version of JavaScript PHP is a … Read more

[Solved] Ruby code doesn’t work [closed]

I checked your codes and found some problems: num_array.reverse.each_with_index { |value , index| if index % 3 == 0 && num_array[index] != 0 num_string = num_string << ones_place[value-1] elsif index % 3 == 0 && value > 0 && num_array[index] != 0 num_string = num_string << other_place[index/3] << ones_place[value-1] elsif index % 3 == 1 … Read more

[Solved] slow algorithm contest

Your code looks OK, you might make it faster by checking the words have the same length before the actual sort: while !STDIN.gets.chomp.empty? first_word, second_word = $_.downcase.split if first_word.length == second_word.length && first_word.chars.sort == second_word.chars.sort puts “YES” else puts “NO” end end 1 solved slow algorithm contest

[Solved] Upgrading ruby 1.8.6 to ruby 1.9.2

In Ruby a lot of us face these types of situations, where upgrading to a newer version could potentially break your code which used to work fine in an older one. The fantastic Mr. Wayne E. Seguin faced it too, and created a great tool for solving this called rvm. In a nutshell rvm lets … Read more

[Solved] Counting changes in a nested array

result = input.map do |k, t| [k, DateTime.parse(t)] end.each_with_object(prev: nil, count: 0) do |(k, t), acc| case k # keep the time of the previous occurrence of “red” when “red” then acc[:prev] = t when “green” # seconds acc[:count] += 1 if 24.0 * 60 * 60 * (t – acc[:prev]) < 10 acc[:prev] = … Read more

[Solved] Missing config.ru when using rackup [closed]

You need to add a file called config.ru to your root/ directory. config.ru is the configuration file for rackup, and controls what rackup will actually run. Look at section 2.3 for an explanation: http://guides.rubyonrails.org/rails_on_rack.html To use rackup instead of Rails’ rails server, you can put the following inside config.ru of your Rails application’s root directory: … Read more