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
| # 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
| 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
| 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
| 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
| 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
| # 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 selection_sort | |
| start_time = Time.now | |
| # Selection sort (very slow on large lists) | |
| a = self | |
| # get the number of indices in the array | |
| n = a.size - 1 |
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' | |
| def my_next_n_birthdays(number_of_years = 60, birthday = 24, birth_month = 8) | |
| lazy_dates = (Date.today..Date.new(9999)).lazy | |
| my_next_birthdays = lazy_dates.select { |d| d.day == birthday and d.month == birth_month }.first(number_of_years).map { |date| "#{date.strftime('%A')} - #{date}"} | |
| end | |
| puts my_next_n_birthdays |
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
| puts 'Provide the sentence you want to perform letter frequency on' | |
| text = gets.chomp # gets the sentence to iterate | |
| puts "Performing on: #{text[0..6]}..." | |
| text.downcase! | |
| freqs = {} # creating a hash to iterate 'text' | |
| freqs.default = 0 | |
| count = 0 # number of charachters in 'text' | |
| # iterating through text | |
| text.each_char do |char| |