This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # a gem created by github user ecleel. | |
| require 'hijri' | |
| t = Time.new(1993, 8, 23, 21, 0, 0, "+02:00") | |
| d = DateTime.new(t.year, t.month, t.day, t.hour, t.min, t.sec, Rational(t.gmt_offset / 3600, 24)) | |
| h = d.to_hijri | |
| puts "You were born on #{h.strftime('%c')}" | |
| # الاثنين الموافق 5 من ربيع الأول لعام 1414 هجريه |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require_relative 'random_password' | |
| include RandomPassword | |
| class Array | |
| def insertion_sort | |
| start_time = Time.now | |
| arr = self | |
| for i in (1...(arr.size)) | |
| if arr[i-1] > arr[i] | |
| i.downto(1) do |el| |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Product.joins(:categories).where(category: { id: [1,2,3,4,5] }).where('products.name like ?', "%ant%") | |
| Product Load (0.5ms) SELECT "products".* FROM "products" INNER JOIN "product_categories" ON "product_categories"."product_id" = "products"."id" INNER JOIN "categories" ON "categories"."id" = "product_categories"."category_id" WHERE "category"."id" IN (1, 2, 3, 4, 5) AND (products.name like '%ant%') | |
| ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: category.id: SELECT "products".* FROM "products" INNER JOIN "product_categories" ON "product_categories"."product_id" = "products"."id" INNER JOIN "categories" ON "categories"."id" = "product_categories"."category_id" WHERE "category"."id" IN (1, 2, 3, 4, 5) AND (products.name like '%ant%') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Taggable | |
| extend ActiveSupport::Concern | |
| included do | |
| has_many :taggings, as: :taggable, dependent: :destroy | |
| has_many :tags, through: :taggings | |
| end | |
| def tag_names | |
| tags.map(&:name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'date' | |
| lazy_dates = (Date.today..Date.new(9999)).lazy | |
| fridays_the_13th = lazy_dates.select { |d| d.friday? and d.day == 13 }.first(10) | |
| puts fridays_the_13th |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Select only the attributes you want from a hash. | |
| # this is crazy useful when getting web parameters for example. | |
| # --------------------------------------------------------------------- | |
| require 'active_support/core_ext' | |
| bacon = { delicious: true, color: "red" } | |
| bacon.slice(:color) | |
| # => { color:"red" } | |
| # it returns a new hash, you can slice unknown keys and it still works. | |
| # there's a lot of great stuff in active_support (from the rails gem) | |
| # it mostly stands by itself but it's big so you can require just 'active_support/core_ext' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Person | |
| def name(n) | |
| @name = n | |
| self | |
| end | |
| def age(a) | |
| @age = a | |
| self | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Ruby Dose Facebook Page | |
| require 'test/unit' | |
| class Test::Unit::TestCase | |
| def self.must(name, &block) | |
| # convert "any method description" to any_method_description | |
| test_name = "test_#{name.gsub(/\s+/, '_')}".to_sym |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Info | |
| class << self | |
| def method_missing(name, *args, &block) | |
| case name | |
| when /^get_(.*)/ | |
| send($1, *args, &block) | |
| else | |
| super | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'logger' | |
| # Generally its a good practice to avoid global variables, but a good use of them would be in a logger constant | |
| # you can try other Logger creation options such as | |
| # Keep data for the current month only | |
| # Logger.new('this_month.log', 'monthly') | |
| # Keep data for today and the past 20 days. | |
| # Logger.new('application.log', 20, 'daily') | |
| # Start the log over whenever the log exceeds 100 megabytes in size. | |
| # Logger.new('application.log', 0, 100 * 1024 * 1024) | |
| $LOG = Logger.new('app.log', 'monthly') |