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 JSONable | |
| # serialize object to json | |
| def to_json | |
| hash = Hash.new | |
| self.instance_variables.each do |ivar| | |
| hash[ivar] = self.instance_variable_get ivar | |
| end | |
| hash.to_json | |
| 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
| <!-- _error_messages.html.erb --> | |
| <% if @user.errors.any? %> | |
| <div id="error_explanation"> | |
| <div class="alert alert-danger"> | |
| The form contains <%= pluralize(@user.errors.count, "error") %>. | |
| </div> | |
| <ul> | |
| <% @user.errors.full_messages.each do |msg| %> | |
| <li><%= msg %></li> |
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
| ActionView::Base.field_error_proc = Proc.new do |html_tag, instance| | |
| html = %(<div class="field_with_errors">#{html_tag}</div>).html_safe | |
| # add nokogiri gem to Gemfile | |
| form_fields = [ | |
| 'textarea', | |
| 'input', | |
| 'select' | |
| ] |
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
| - flash.each do |type, message| | |
| %div{ class: "alert #{bootstrap_class_for(type)}" } | |
| %button.close{ :data => { :dismiss => "alert" } } x | |
| = message | |
| # From application helper | |
| def bootstrap_class_for flash_type |
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 'rubygems' | |
| require 'pp' | |
| #require 'ap' # Awesome Print | |
| class Object | |
| # expects [ [ symbol, *args ], ... ] | |
| def recursive_send(*args) | |
| args.inject(self) { |obj, m| obj.send(m.shift, *m) } | |
| 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 'benchmark' | |
| stringAZ = Hash[("a".."z").to_a.zip((1..26).to_a)] | |
| symbolAZ = Hash[(:a..:z).to_a.zip((1..26).to_a)] | |
| string_time = Benchmark.realtime do | |
| 100_000.times{stringAZ["r"]} | |
| end | |
| symbol_time = Benchmark.realtime do |
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
| ################################################### | |
| # calling a proc on a collection of numbers | |
| increment_by_1 = proc { |n| n + 1 } | |
| # the & converts the proc into a block that is then invoked on each item in the collection | |
| [1,2,3].map(&increment_by_1) | |
| # returns [2, 3, 4] | |
| ################################################### | |
| # calling a lambda on a collection of numbers | |
| incrememnt_by_1 = ->(n){n+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
| # No yielding | |
| class Person | |
| attr_accessor :first, :last | |
| def initialize(first = nil, last = nil) | |
| @first = first | |
| @last = last | |
| end | |
| def hello | |
| puts "#{@first} #{@last} says hello!" |
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
| %h2.text-center Sign in | |
| %br/ | |
| = form_for(resource, as: resource_name, url: session_path(resource_name), html: {class: 'form-horizontal'}) do |f| | |
| .form-group | |
| = f.label :email, class: "col-sm-2 control-label" | |
| .col-sm-6 | |
| = f.email_field :email, autofocus: true , class: "form-control" | |
| .form-group | |
| = f.label :password, class: "col-sm-2 control-label" | |
| .col-sm-6 |
NewerOlder