diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..afab3cd --- /dev/null +++ b/.gitignore @@ -0,0 +1,58 @@ +### Ruby template +*.gem +*.rbc +/.config +/coverage/ +/InstalledFiles +/pkg/ +/spec/reports/ +/spec/examples.txt +/test/tmp/ +/test/version_tmp/ +/tmp/ + +# Used by dotenv library to load environment variables. +# .env + +# Ignore Byebug command history file. +.byebug_history + +## Specific to RubyMotion: +.dat* +.repl_history +build/ +*.bridgesupport +build-iPhoneOS/ +build-iPhoneSimulator/ + +## Specific to RubyMotion (use of CocoaPods): +# +# We recommend against adding the Pods directory to your .gitignore. However +# you should judge for yourself, the pros and cons are mentioned at: +# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control +# +# vendor/Pods/ + +## Documentation cache and generated files: +/.yardoc/ +/_yardoc/ +/doc/ +/rdoc/ + +## Environment normalization: +/.bundle/ +/vendor/bundle +/lib/bundler/man/ + +# for a library or gem, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# Gemfile.lock +# .ruby-version +# .ruby-gemset + +# unless supporting rvm < 1.11.0 or doing something fancy, ignore this: +.rvmrc + +# Used by RuboCop. Remote config files pulled in from inherit_from directive. +# .rubocop-https?--* + diff --git a/Writerside/c.list b/Writerside/c.list new file mode 100644 index 0000000..c4c77a2 --- /dev/null +++ b/Writerside/c.list @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/Writerside/cfg/buildprofiles.xml b/Writerside/cfg/buildprofiles.xml new file mode 100644 index 0000000..0c1f95e --- /dev/null +++ b/Writerside/cfg/buildprofiles.xml @@ -0,0 +1,12 @@ + + + + + + + true + + + + diff --git a/Writerside/images/completion_procedure.png b/Writerside/images/completion_procedure.png new file mode 100644 index 0000000..3535a3f Binary files /dev/null and b/Writerside/images/completion_procedure.png differ diff --git a/Writerside/images/completion_procedure_dark.png b/Writerside/images/completion_procedure_dark.png new file mode 100644 index 0000000..a65beb0 Binary files /dev/null and b/Writerside/images/completion_procedure_dark.png differ diff --git a/Writerside/images/convert_table_to_xml.png b/Writerside/images/convert_table_to_xml.png new file mode 100644 index 0000000..2518a64 Binary files /dev/null and b/Writerside/images/convert_table_to_xml.png differ diff --git a/Writerside/images/convert_table_to_xml_dark.png b/Writerside/images/convert_table_to_xml_dark.png new file mode 100644 index 0000000..4716122 Binary files /dev/null and b/Writerside/images/convert_table_to_xml_dark.png differ diff --git a/Writerside/images/new_topic_options.png b/Writerside/images/new_topic_options.png new file mode 100644 index 0000000..bc6abb6 Binary files /dev/null and b/Writerside/images/new_topic_options.png differ diff --git a/Writerside/images/new_topic_options_dark.png b/Writerside/images/new_topic_options_dark.png new file mode 100644 index 0000000..bf3e48d Binary files /dev/null and b/Writerside/images/new_topic_options_dark.png differ diff --git a/Writerside/r.tree b/Writerside/r.tree new file mode 100644 index 0000000..7754cfc --- /dev/null +++ b/Writerside/r.tree @@ -0,0 +1,12 @@ + + + + + + + + + \ No newline at end of file diff --git a/Writerside/topics/CheetSheet.md b/Writerside/topics/CheetSheet.md new file mode 100644 index 0000000..ecc68b4 --- /dev/null +++ b/Writerside/topics/CheetSheet.md @@ -0,0 +1,468 @@ +# Ruby Cheatsheet + +##### Table of Contents + +- [Basics](#basics) + +- [Vars, Constants, Arrays, Hashes & Symbols](#vars-constants-arrays-hashes--symbols) + +- [Methods](#methods) + +- [Classes](#classes) + +- [Modules](#modules) + +- [Blocks & Procs](#blocks--procs) + +- [Lambdas](#lambdas) + +- [Calculation](#calculation) + +- [Commenting](#commenting) + +- [Conditions](#conditions) + +- [Printing & Putting](#printing--putting) + +- [User Input](#user-input) + +- [Loops](#loops) + +- [Sorting & Comparing](#sorting--comparing) + +- [Useful Methods](#useful-methods) + + ## Basics + +- `$ irb`: to write ruby in the terminal + +- don't use `'` in ruby, use `"` instead + +- you can replace most `{}` with `do end` and vice versa –– not true for hashes or `#{}` escapings + +- Best Practice: end names that produce booleans with question mark + +- CRUD: create, read, update, delete + +- `[1,2].map(&:to_i)` + +- `integer`: number without decimal + +- `float`: number with decimal + +- tag your variables: + +- - `$`: global variable +- - `@`: instance variable +- - `@@`: class variable +- `1_000_000`: 1000000 –– just easier to read\* + + ## Vars, Constants, Arrays, Hashes & Symbols + + ```Ruby + my_variable = “Hello” + my_variable.capitalize! # ! changes the value of the var same as my_name = my_name.capitalize + my_variable ||= "Hi" # ||= is a conditional assignment only set the variable if it was not set before. + ``` + + ### Constants + + ```Ruby + MY_CONSTANT = # something + ``` + + ### Arrays + + ```Ruby + my_array = [a,b,c,d,e] + my_array[1] # b + my_array[2..-1] # c , d , e + multi_d = [[0,1],[0,1]] + [1, 2, 3] << 4 # [1, 2, 3, 4] same as [1, 2, 3].push(4) + ``` + + ### Hashes + + ```Ruby + hash = { "key1" => "value1", "key2" => "value2" } # same as objects in JavaScript + hash = { key1: "value1", key2: "value2" } # the same hash using symbols instead of strings + my_hash = Hash.new # same as my_hash = {} – set a new key like so: pets["Stevie"] = "cat" + pets["key1"] # value1 + pets["Stevie"] # cat + my_hash = Hash.new("default value") + hash.select{ |key, value| value > 3 } # selects all keys in hash that have a value greater than 3 + hash.each_key { |k| print k, " " } # ==> key1 key2 + hash.each_value { |v| print v } # ==> value1value2 + my_hash.each_value { |v| print v, " " } + # ==> 1 2 3 + ``` + + ### Symbols + + ```Ruby + :symbol # symbol is like an ID in html. :symbol != "symbol" + # Symbols are often used as Hash keys or referencing method names. + # They can not be changed once created. They save memory (only one copy at a given time). Faster. + :test.to_s # converts to "test" + "test".to_sym # converts to :test + "test".intern # :test + # Symbols can be used like this as well: + my_hash = { key: "value", key2: "value" } # is equal to { :key => "value", :key2 => "value" } + ``` + + #### Functions to create Arrays + + ```Ruby + "bla,bla".split(“,”) # takes string and returns an array (here ["bla","bla"]) + ``` + + ## Methods + + **Methods** + + ```Ruby + def greeting(hello, *names) # *names is a split argument, takes several parameters passed in an array + return "#{hello}, #{names}" + end + start = greeting("Hi", "Justin", "Maria", "Herbert") # call a method by name + def name(variable=default) + ### The last line in here gets returned by default + end + ``` + + ## Classes + + _custom objects_ + + ```Ruby + class Person # class names are rather written in PascalCase (It is similar to camelCase, but the first letter is capitalized) + @@count = 0 + attr_reader :name # make it readable + attr_writer :name # make it writable + attr_accessor :name # makes it readable and writable + def Methodname(parameter) + @classVariable = parameter + @@count += 1 + end + def self.show_classVariable + @classVariable + end + def Person.get_counts # is a class method + return @@count + end + private + def private_method; end # Private methods go here + end + matz = Person.new("Yukihiro") + matz.show_name # Yukihiro + ``` + + ### Inheritance + + ```Ruby + class DerivedClass < BaseClass; end # if you want to end a Ruby statement without going to a new line, you can just type a semicolon. + class DerivedClass < Base + def some_method + super(optional args) # When you call super from inside a method, that tells Ruby to look in the superclass of the current class and find a method with the same name as the one from which super is called. If it finds it, Ruby will use the superclass' version of the method. + # Some stuff + end + end + end + # Any given Ruby class can have only one superclass. Use mixins if you want to incorporate data or behavior from several classes into a single class. + ``` + + ## Modules + + ```Ruby + module ModuleName # module names are rather written in PascalCase + # variables in modules doesn't make much sence since modules do not change. Use constants. + end + Math::PI # using PI constant from Math module. Double colon = scope resolution operator = tells Ruby where you're looking for a specific bit of code. + require 'date' # to use external modules. + puts Date.today # 2016-03-18 + module Action + def jump + @distance = rand(4) + 2 + puts "I jumped forward #{@distance} feet!" + end + end + class Rabbit + include Action # Any class that includes a certain module can use those module's methods! This now is called a Mixin. + extend Action # extend keyword mixes a module's methods at the class level. This means that class itself can use the methods, as opposed to instances of the class. + attr_reader :name + def initialize(name) + @name = name + end + end + peter = Rabbit.new("Peter") + peter.jump # include + Rabbit.jump # extend + ``` + + ## Blocks & Procs + + ### Code Blocks + + _Blocks are not objects._ A block is just a bit of code between do..end or {}. It's not an object on its own, but it can be passed to methods like .each or .select. + + ```Ruby + def yield_name(name) + yield("Kim") # print "My name is Kim. " + yield(name) # print "My name is Eric. " + end + yield_name("Eric") { |n| print "My name is #{n}. " } # My name is Kim. My name is Eric. + yield_name("Peter") { |n| print "My name is #{n}. " } # My name is Kim. My name is Eric. My name is Kim. My name is Peter. + ``` + + ### Proc + + _Saves blocks and are objects._ A proc is a saved block we can use over and over. + + ```Ruby + cube = Proc.new { |x| x ** 3 } + [1, 2, 3].collect!(&cube) # [1, 8, 27] # the & is needed to transform the Proc to a block. + cube.call # call procs directly + ``` + + ## Lambdas + + ```Ruby + lambda { |param| block } + multiply = lambda { |x| x * 3 } + y = [1, 2].collect(&multiply) # 3 , 6 + ``` + + Diff between procs and lambdas: + +- a lambda checks the number of arguments passed to it, while a proc does not (This means that a lambda will throw an error if you pass it the wrong number of arguments, whereas a proc will ignore unexpected arguments and assign nil to any that are missing.) + +- when a lambda returns, it passes control back to the calling method; when a proc returns, it does so immediately, without going back to the calling method. + So: A lambda is just like a proc, only it cares about the number of arguments it gets and it returns to its calling method rather than returning immediately. + + ## Calculation + +- Addition (+) + +- Subtraction (-) + +- Multiplication (\*) + +- Division (/) + +- Exponentiation (\*\*) + +- Modulo (%) + +- The concatenation operator (<<) + +- you can do 1 += 1 –– which gives you 2 but 1++ and 1-- does not exist in ruby + +- `"A " << "B"` => `"A B"` but `"A " + "B"` would work as well but `"A " + 4 + " B"` not. So rather use string interpolation (`#{4}`) + +- `"A #{4} B"` => `"A 4 B"` + + ## Commenting + + ```Ruby + =begin + Bla + Multyline comment + =end + ``` + + ```Ruby + # single line comment + ``` + + ## Conditions + + ### If + + ```Ruby + if 1 < 2 + puts “one smaller than two” + elsif 1 > 2 # *careful not to mistake with else if. In ruby you write elsif* + puts “elsif” + else + puts “false” + end + # or + puts "be printed" if true + puts 3 > 4 ? "if true" : "else" # else will be putted + ``` + + ### Unless + + ```Ruby + unless false # unless checks if the statement is false (opposite to if). + puts “I’m here” + else + puts “not here” + end + # or + puts "not printed" unless true + ``` + + ### Case + + ```Ruby + case my_var + when "some value" + ### + when "some other value" + ### + else + ### + end + # or + case my_var + when "some value" then ### + when "some other value" then ### + else ### + end + ``` + +- `&&`: and + +- `||`: or + +- `!`: not + +- `(x && (y || w)) && z`: use parenthesis to combine arguments + +- problem = false + +- print "Good to go!" unless problem –– prints out because problem != true + + ## Printing & Putting + + ```Ruby + print "bla" + puts "test" # puts the text in a separate line + ``` + + ## String Methods + + ```Ruby + "Hello".length # 5 + "Hello".reverse # “olleH” + "Hello".upcase # “HELLO” + "Hello".downcase # “hello” + "hello".capitalize # “Hello” + "Hello".include? "i" # equals to false because there is no i in Hello + "Hello".gsub!(/e/, "o") # Hollo + "1".to_i # transform string to integer –– 1 + "test".to_sym # converts to :test + "test".intern # :test + :test.to_s # converts to "test" + ``` + + ## User Input + + ```Ruby + gets # is the Ruby equivalent to prompt in javascript (method that gets input from the user) + gets.chomp # removes extra line created after gets (usually used like this) + ``` + + ## Loops + + ### While loop + + ```Ruby + i = 1 + while i < 11 + puts i + i = i + 1 + end + ``` + + ### Until loop + + ```Ruby + i = 0 + until i == 6 + puts i + i += 1 + end + ``` + + ### For loop + + ```Ruby + for i in 1...10 # ... tells ruby to exclude the last number (here 10 if we .. only then it includes the last num) + puts i + end + ``` + + ### Loop iterator + + ```Ruby + i = 0 + loop do + i += 1 + print "I'm currently number #{i}” # a way to have ruby code in a string + break if i > 5 + end + ``` + + ### Next + + ```Ruby + for i in 1..5 + next if i % 2 == 0 # If the remainder of i / 2 is zero, we go to the next iteration of the loop. + print i + end + ``` + + ### .each + + ```Ruby + things.each do |item| # for each things in things do something while storing that things in the variable item + print “#{item}" + end + ``` + + on hashes like so: + + ```Ruby + hashes.each do |x,y| + print "#{x}: #{y}" + end + ``` + + ### .times + + ```Ruby + 10.times do + print “this text will appear 10 times” + end + ``` + + ### .upto / .downto + + ```Ruby + 10.upto(15) { |x| print x, " " } # 10 11 12 13 14 15 + "a".upto("c") { |x| print x, " " } # a b c + ``` + + ## Sorting & Comparing + + ```Ruby + array = [5,4,1,3,2] + array.sort! # = [1,2,3,4,5] – works with text and other as well. + "b" <=> "a" # = 1 – combined comparison operator. Returns 0 if first = second, 1 if first > second, -1 if first < second + array.sort! { |a, b| b <=> a } # to sort from Z to A instead of A to Z + ``` + + ## Useful Methods + + ```Ruby + 1.is_a? Integer # returns true + :1.is_a? Symbol # returns true + "1".is_a? String # returns true + [1,2,3].collect!() # does something to every element (overwrites original with ! mark) + .map() # is the same as .collect + 1.2.floor # 1 # rounds a float (a number with a decimal) down to the nearest integer. + cube.call # implying that cube is a proc, call calls procs directly + Time.now # displays the actual time + ``` \ No newline at end of file diff --git a/Writerside/topics/rails-CheetSheet.md b/Writerside/topics/rails-CheetSheet.md new file mode 100644 index 0000000..ced5174 --- /dev/null +++ b/Writerside/topics/rails-CheetSheet.md @@ -0,0 +1,220 @@ +# Ruby on Rails Cheatsheet + +##### Table of Contents + +[Basics](#basics) +[Connect a Database](#connect-a-database) +[Rake](#rake) +[Troubleshoots](#troubleshoots) + +## Basics + +#### request/response cycle + +controller > route > view +learn request/response cycle: https://www.codecademy.com/articles/request-response-cycle-forms + +#### 1. Generate a new Rails app + +```bash +$ rails new MySite +# or using Postgress $ rails new myapp --database=postgresql +$ bundle install +$ rails server +> http://localhost:8000 # or similar Number up and running! +``` + +#### 2. Generate a controller and add an action + +``` +$ rails generate controller Pages +``` + +generates a new controller named Pages +Open: app/controllers/pages_controller.rb + +```Ruby +class PagesController < ApplicationController + def home # add the home action/method to the pages controller + end +end +``` + +#### 3. Create a route that maps a URL to the controller action + +Open: config/routes.rb + +```Ruby +Rails.application.routes.draw do + get 'welcome' => 'pages#home' +end +``` + +#### 4. Create a view with HTML and CSS + +Open: app/views/pages/home.html.erb + +```html + +
+
+

Hello my name is King

+

I make Rails apps.

+
+
+``` + +## Connect a Database + +#### Example: messaging system: + +#### 1. Generate a new Model + +``` +$ rails generate model Message +``` + +create model named Message. With two files: model file app/models/message.rb. Represents a table in database. Migration file db/migrate/. Way to update database. +Open: db/migrate/*.rb –– *The name of the migration file starts with the timestamp of when it was created + +```Ruby +class CreateMessages < ActiveRecord::Migration + def change + create_table :messages do |t| + t.text :content # add this + t.timestamps + end + end +end +``` + +1. The change method tells Rails what change to make to the database. Here create_table method create a new table in database for storing messages. + +2. t.text :content. Create text column called content in the messages tables. + +3. t.timestamps is a Rails command that creates two more columns in the messages table called created_at and updated_at. These columns are automatically set when a message is created and updated. + Open: db/seeds.rb + + ```Ruby + m1 = Message.create(content: "We're at the beach so you should meet us here! I make a mean sandcastle. :)") + m2 = Message.create(content: "Let's meet there!") + ``` + +Just to have dummy messages to load with db:seed +in Terminal run + + ``` + $ rails db:migrate + $ rake db:setup + $ rake db:migrate + $ rake db:seed + ``` + +updates the database with the new messages data model. +seeds the database with sample data from db/seeds.rb + +#### 2. Generate a controller and add actions + + ``` + $ rails generate controller Messages + ``` + +Open: app/controllers/messages_controller.rb + + ```Ruby + class MessagesController < ApplicationController + # Rails defines standard controller actions can be used to do common things such as display and modify data. + # index, show, new, create, edit, update, and destroy + # code below is optional + def index + @messages = Message.all # retrieves all messages from database and stores them in variable @messages. + end + def new + @message = Message.new # + end + def create + @message = Message.new(message_params) + if @message.save + redirect_to '/messages' + else + render 'new' + end + end + private + def message_params + params.require(:message).permit(:content) + end + end + ``` + +#### 3. Create a route that maps a URL to the controller action + +Open: config/routes.rb + + ```Ruby + Rails.application.routes.draw do + get 'messages' => 'messages#index' + get 'messages/new' => 'messages#new' + post 'messages' => 'messages#create' + end + ``` + +#### 4. Create a view with HTML and CSS + +Open: app/views/messages/index.html.erb + + ```html +
+
+ <% @messages.each do |message| %> + +
+

<%= message.content %>

+

<%= message.created_at %>

+
+ <% end %> <%= link_to 'New Message', "messages/new" %> + +
+
+ ``` + +Open: app/views/messages/new.html.erb + + ```html +
+
+ + <%= form_for(@message) do |f| %> +
+ <%= f.label :message %>
+ <%= f.text_area :content %> +
+
<%= f.submit "Create" %>
+ <% end %> +
+
+ ``` + +## Rake + + ```bash + rake db:create # Creates the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:create:all to create all databa... + rake db:drop # Drops the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:drop:all to drop all databases in... + rake db:fixtures:load # Load fixtures into the current environment's database + rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog) + rake db:migrate:status # Display status of migrations + rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n) + rake db:schema:cache:clear # Clear a db/schema_cache.dump file + rake db:schema:cache:dump # Create a db/schema_cache.dump file + rake db:schema:dump # Create a db/schema.rb file that is portable against any DB supported by AR + rake db:schema:load # Load a schema.rb file into the database + rake db:seed # Load the seed data from db/seeds.rb + ``` + +## Troubleshoots + +4. When seeing only a blank page after running rails server: + http://stackoverflow.com/questions/25951969/rails-4-2-and-vagrant-get-a-blank-page-and-nothing-in-the-logs + +5. Follow instructions to use postgres: + https://www.digitalocean.com/community/tutorials/how-to-setup-ruby-on-rails-with-postgres \ No newline at end of file diff --git a/Writerside/topics/starter-topic.md b/Writerside/topics/starter-topic.md new file mode 100644 index 0000000..84100e8 --- /dev/null +++ b/Writerside/topics/starter-topic.md @@ -0,0 +1,11 @@ +# Ruby + +```bash +sudo apt install ruby --classic +sudo apt install ruby-full +sudo apt install build-essentials +``` + +```bash +ruby -v +``` \ No newline at end of file diff --git a/Writerside/v.list b/Writerside/v.list new file mode 100644 index 0000000..2d12cb3 --- /dev/null +++ b/Writerside/v.list @@ -0,0 +1,5 @@ + + + + + diff --git a/Writerside/writerside.cfg b/Writerside/writerside.cfg new file mode 100644 index 0000000..a548f8e --- /dev/null +++ b/Writerside/writerside.cfg @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file