Fri Jun 17 2022 09:18:45 GMT+0000 (Coordinated Universal Time)
Saved by @metamoni #ruby #rubyonrails
<%= image_tag "rails.png", alt: "Alternative text" %>
#ruby #rubyonrails
file = "#{Rails.root}/public/users.csv" headers = ["Name", "Company Name", "Email", "Role", "Team Name"] CSV.open(file, 'w', write_headers: true, headers: headers) do |writer| Team.all.each do |team| team.users.each do |user| writer << [user.name, user.company_name , user.email, user.roles&.first&.name, team.name] end end end
#howto #rubyonrails #webdev #interviewquestions
# get "/articles?page=2" request.original_url # => "http://www.example.com/articles?page=2"
namespace :javascript do desc "Compiles each js file" task validate: :environment do JS_PATH = "app/assets/javascripts/**/*.js" Dir[JS_PATH].each do |file_name| puts "\n#{file_name}" puts Uglifier.compile(File.read(file_name)) end end end
#ruby
# Single line comment =begin multiline comment =end
array.each do |item| puts "The current array item is: #{item}" end
#rubyonrails #ruby
$ rake db:reset db:migrate
$ rake db:drop db:create db:migrate
require 'pry' class String def sentence? self.end_with?(".") end def count_sentences self.split(/\.|\?|\!/).delete_if {|w| w.size < 2}.size end end
support_price = order.site.price_for_support( addons.find_by(group: "support"), order.should_show_incentive_addon_prices? ) * facilitators
#ruby #webscraping #serpapi
# tmp/profiler.rb require "ruby-prof" profile = RubyProf.profile do search_params = { engine: "google", q: "roller blades", location: "Austin, United States", google_domain: "google.com", hl: "en", gl: "us", num: "500", device: "desktop", tbm: "shop", tbs: "p_ord:rv", file_path: "tmp/roller-blades.html" } Search.new(search_params).parse! end printer = RubyProf::FlatPrinter.new(profile) printer.print($stdout, min_percent: 2)
#json #ruby #webscraping #serpapi
def extracted_thumbnails return @extracted_thumbnails if @extracted_thumbnails.present? js_image_regexes = JS_IMAGE_REGEXES.detect { |key, _| engine.starts_with?(key.to_s) }&.last || JS_IMAGE_REGEXES[:all] @extracted_thumbnails = js_image_regexes.collect { |regex| regex_capture_names = regex.names thumbnail_index = regex_capture_names.index(THUMBNAIL_CAPTURE_NAME) thumbnail_id_index = regex_capture_names.index(THUMBNAIL_ID_CAPTURE_NAME) html.scan(regex).collect do |match| found_thumbnail = match[thumbnail_index] found_thumbnail_id = match[thumbnail_id_index] found_thumbnail_id.split(",").map { |thumb| Hash[thumb.tr("'", "").squish, found_thumbnail] } end }.flatten.inject(:merge) || {} end
def extract_js_image(image_node) return unless image_node thumbnail_id = image_node["id"] return unless thumbnail_id if (found_thumbnail = extracted_thumbnails[thumbnail_id]) JSUtils.unescape(found_thumbnail) end end
class Bear def hunt forest.select(&:food?) end end
class Bear def eat(food) raise "#{food} is not edible!" unless food.respond_to? :nutrition_value food.nutrition_value end end
Copy this HTML code:
Preview:
open_in_newInstructions on embedding in Medium
Comments