Friday, May 4, 2018

Add syntax Highlighing extension to Sublime for Vue

If like me, you have never added an package to Sublime previously then first you have to add a package manager to your Sublime.

1, Go to the toolbar of you Sublime
Click on: View/Show Console
Go to:
https://packagecontrol.io/installation
I have Sublime Text 3 so I added this code to the console and pressed enter.
import urllib.request,os,hashlib; h = '6f4c264a24d933ce70df5dedcf1dcaee' + 'ebe013ee18cced0ef93d5f746d80ef60'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)
2, Once you do that for your version of Sublime go to
Preferences in your Sublime Toolbar and you'll see a new option:
Preferences/Package Control
Click on that and you will get a pop-up menu.
3. Just type in the thing you are looking for and click on it.

I used Vue Syntax Highlight (I picked it randomly)

This is what my Preferences/Package Settings/Package Control/Settings-Default looks like after I added it.
{
"bootstrapped": true,
"in_process_packages":
[
],
"installed_packages":
[
"Package Control",
"Vue Syntax Highlight"

}
Voila! Now my Vue files are beautiful and have useful highlighting.

Wednesday, February 8, 2017

Naming conventions are important even in Fizz Buzz

I was playing around with Fizz Buzz today.

I started thinking about the dividing part of Fizz Buzz.

I originally was creating 3 methods which I then called in the print_fizzbuzz method.

def divide_by_fifteen(num)
  num % 15
end

def divide_by_three(num)
  num % 3
end

def divide_by_five(num)
  num % 5
end

I realized that I could condense these into one method.

So I went back to math to figure out the best wording for the arguments.

dividend  / divisor == quotient

And thus the final method that is very clear as long as you remember your math.
I added this: ( dividend  / divisor == quotient ) as a comment to be crystal clear.

def divide_by(dividend, divisor)
  dividend % divisor == 0
end

----------------------------------

Then to print the fizz buzz I was looping through a range, for the argument I was using the placeholder num.

I started to think about what I was actually trying to say there.
Instead of just saying num what was it that I really meant?

It took me a while to distill down the words in my head.

First I thought about the problem.
What is "num"? It is the number that I want the Fizz Buzz to stop at.
So I thought end_num, end_number, limit.

When trying to narrow down a word I find it useful to use a Thesaurus
Here are the synonyms for the word limit: http://www.thesaurus.com/browse/limit

These are the words that I thought might be good for my argument after viewing that list:
cap
restraint
bound
conclusion
concludes_at
terminates at
end of range

Then I noticed that I was calling a range.
What could the beginning and the end of a rangebe called?
Answer: lower bound and upper bound


  def print_fizz_buzz(upper_bound)
    fizzbuzz = []
    (1..upper_bound).each do | dividend |
      if divide_by(dividend, 15)
        fizzbuzz << "fizz buzz"
      elsif divide_by(dividend,3)
        fizzbuzz << "fizz"
      elsif divide_by(dividend,5)
        fizzbuzz << "buzz"
      else
        fizzbuzz << dividend
      end
    end
    fizzbuzz
  end



Friday, January 20, 2017

Fun with Ruby and CSV

https://ruby-doc.org/stdlib-2.4.0/libdoc/csv/rdoc/CSV.html

https://www.sitepoint.com/guide-ruby-csv-library-part/

http://technicalpickles.com/posts/parsing-csv-with-ruby/

In Ruby, you can import your CSV file either at once (storing all of the file content in memory) or read from it row-by-row

Either way you do it, Ruby will store each table row as an array, with each cell being a string element of the array.

Tuesday, November 29, 2016

Thursday, September 1, 2016

View your Gems and its methods locally with no internet

In this example I am viewing my Devise Gem.
(Your paths will be different than mine.)

$ gem which devise
==> /home/vagrant/.rvm/gems/ruby-2.3.0/gems/devise-4.2.0/lib/devise.rb

$ gem open devise

$ cd /home/vagrant/.rvm/gems/ruby-2.3.0/gems/devise-4.2.0/lib

$  ls
==> devise  devise.rb  generators

$ cd devise/

$ ls
==> controllers     hooks       models.rb    orm                     rails.rb         time_inflector.rb
delegator.rb    mailers     modules.rb   parameter_filter.rb     strategies       token_generator.rb
encryptor.rb    mapping.rb  omniauth     parameter_sanitizer.rb  test             version.rb
failure_app.rb  models      omniauth.rb  rails                   test_helpers.rb

$ cd controllers/

$ ls
==> helpers.rb  rememberable.rb  scoped_views.rb  sign_in_out.rb  store_location.rb  url_helpers.rb

$ cd ..

Here I am looking for the RegistrationsController in all the files with Grep. This is like when you are in Sublime and search all the files.

$ grep -r RegistrationsC .
==> ./rails/routes.rb:    #    class RegistrationsController < Devise::RegistrationsController
./parameter_sanitizer.rb:  # +password_confirmation+ for the `RegistrationsController`), and you can
./parameter_sanitizer.rb:    #    # Inside the `RegistrationsController#create` action.

$ cd rails/

$ ls
==> routes.rb  warden_compat.rb

This is where it is. I open it in vim and look around.
$ vim routes.rb 

Thursday, August 25, 2016

git add with sophictication

I found this git command that I have been looking for forever.
I used to use it then I forgot it and have been googling for it ever since/.

The perfect pairing to git add -p, drum roll please...

git add . -N && git add -p
  • The -N flag means is short for --intent-to-add 
  • git add . -N will stage an empty file representing your newly added file.
    When git add --patch is called 
  • Git will do the normal patch procedure over your newly created file's changes.
  • If no changes are patched in,
    the empty file will not be included in your commit.

Always be sure to check your status!

Sunday, August 14, 2016

Rails Cheat Sheet: Create Models, Tables and Migrations

Create a new table in Rails

rails g model Supplier name:string
rails g model Product name:string:index sku:string{10}:uniq count:integer description:text supplier:references popularity:float 'price:decimal{10,2}' available:boolean availableSince:datetime image:binary
Resulting migrations:
class CreateSuppliers < ActiveRecord::Migration
  def change
    create_table :suppliers do |t|
      t.string :name

      t.timestamps null: false
    end
  end
end

class CreateProducts < ActiveRecord::Migration
  def change
    create_table :products do |t|
      t.string :name
      t.string :sku, limit: 10
      t.integer :count
      t.text :description
      t.references :supplier, index: true, foreign_key: true
      t.float :popularity
      t.decimal :price, precision: 10, scale: 2
      t.boolean :available
      t.datetime :availableSince
      t.binary :image

      t.timestamps null: false
    end
    add_index :products, :name
    add_index :products, :sku, unique: true
  end
end

Rails migration to add a column

rails g migration AddKeywordsSizeToProduct keywords:string size:string
Resulting migration:
class AddKeywordsSizeToProduct < ActiveRecord::Migration
  def change
    add_column :products, :keywords, :string
    add_column :products, :size, :string
  end
end

Rails migration to remove a column

rails g migration RemoveKeywordsFromProduct keywords
Resulting migration:
class RemoveKeywordsFromProduct < ActiveRecord::Migration
  def change
    remove_column :products, :keywords, :string
  end
end

Rails migration to rename a column

rails g migration RenameProductPopularityToRanking
You need to add the rename_column command manually to the resulting migration:
class RenameProductPopularityToRanking < ActiveRecord::Migration
  def change
    rename_column :products, :popularity, :ranking
  end
end

Rails migration to change a column type

rails g migration ChangeProductPopularity
You need to add the change_column command manually to the resulting migration:
class ChangeProductPopularity < ActiveRecord::Migration
  def change
      change_column :products, :ranking, :decimal, precision: 10, scale: 2
  end
end

Running migrations

rake db:migrate
In production:
rake db:migrate RAILS_ENV="production"