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 Category < ActiveRecord::Base | |
| has_and_belongs_to_many :products | |
| end | |
| class Product < ActiveRecord::Base | |
| has_and_belongs_to_many :categories | |
| # :c can be an array of categories. | |
| scope :by_categories, ->(c) { includes(:categories).where(categories: {id: c}) } | |
| 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
| # Example: | |
| # | |
| # 1 | |
| # / \ | |
| # 2 12 | |
| # / | |
| # 4 | |
| class Node | |
| attr_accessor :left, :right, :value |
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 Hello | |
| def self.world | |
| puts 'Hello World !' | |
| end | |
| end | |
| def return_hello | |
| return Hello | |
| 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
| "olo".gsub(/[ol]/, 'o' => 'l', 'l' => 'o') | |
| # output | |
| # | |
| # 'lol' |
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 : | |
| # | |
| # Ruby tries to load the library named string relative to the requiring file’s path. | |
| # If the file’s path cannot be determined a LoadError is raised. | |
| # If a file is loaded true is returned and false otherwise. | |
| # | |
| # Tree directory: | |
| # | |
| # ./ | |
| # |- test |
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` method calls `to_s` method when a class is given as param | |
| # `p` method calls `inspect` method when a class is given as param | |
| class Factory | |
| def initialize | |
| @workers = { workers: [] } # A lot of workers | |
| @machines = { machines: [] } # A lot of machines | |
| end | |
| def add_worker(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
| # Gemfile consists of plain Ruby | |
| # So it's authorized to do this: | |
| branch = 'develop' | |
| group :development do | |
| %(a b c d).each do |lib| | |
| gem lib, :git => "https://github.com/abc/#{lib}.git", :branch => branch | |
| 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
| # more information here: http://ruby-doc.org//core-2.2.0/Integer.html#method-i-chr | |
| puts 219.chr #=> "?" | |
| puts 219.chr(Encoding::UTF_8) #=> "Û" |
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 ValentineDay | |
| def to_s | |
| "I love you" | |
| end | |
| end | |
| puts "#{ValentineDay.new}" # => I love you |
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
| def add(a) | |
| return Proc.new do |b| | |
| a + b | |
| end | |
| end | |
| addition = add(4) | |
| puts addition.call(5) # => 9 |