2024-06-21
This commit is contained in:
58
.gitignore
vendored
Normal file
58
.gitignore
vendored
Normal file
@@ -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?--*
|
||||||
|
|
||||||
6
Writerside/c.list
Normal file
6
Writerside/c.list
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE categories
|
||||||
|
SYSTEM "https://resources.jetbrains.com/writerside/1.0/categories.dtd">
|
||||||
|
<categories>
|
||||||
|
<category id="wrs" name="Writerside documentation" order="1"/>
|
||||||
|
</categories>
|
||||||
12
Writerside/cfg/buildprofiles.xml
Normal file
12
Writerside/cfg/buildprofiles.xml
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<buildprofiles xsi:noNamespaceSchemaLocation="https://resources.jetbrains.com/writerside/1.0/build-profiles.xsd"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
|
|
||||||
|
<variables></variables>
|
||||||
|
<build-profile instance="r">
|
||||||
|
<variables>
|
||||||
|
<noindex-content>true</noindex-content>
|
||||||
|
</variables>
|
||||||
|
</build-profile>
|
||||||
|
|
||||||
|
</buildprofiles>
|
||||||
BIN
Writerside/images/completion_procedure.png
Normal file
BIN
Writerside/images/completion_procedure.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 125 KiB |
BIN
Writerside/images/completion_procedure_dark.png
Normal file
BIN
Writerside/images/completion_procedure_dark.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 67 KiB |
BIN
Writerside/images/convert_table_to_xml.png
Normal file
BIN
Writerside/images/convert_table_to_xml.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 341 KiB |
BIN
Writerside/images/convert_table_to_xml_dark.png
Normal file
BIN
Writerside/images/convert_table_to_xml_dark.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 119 KiB |
BIN
Writerside/images/new_topic_options.png
Normal file
BIN
Writerside/images/new_topic_options.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 306 KiB |
BIN
Writerside/images/new_topic_options_dark.png
Normal file
BIN
Writerside/images/new_topic_options_dark.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 274 KiB |
12
Writerside/r.tree
Normal file
12
Writerside/r.tree
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE instance-profile
|
||||||
|
SYSTEM "https://resources.jetbrains.com/writerside/1.0/product-profile.dtd">
|
||||||
|
|
||||||
|
<instance-profile id="r"
|
||||||
|
name="Ruby"
|
||||||
|
start-page="starter-topic.md">
|
||||||
|
|
||||||
|
<toc-element topic="starter-topic.md"/>
|
||||||
|
<toc-element topic="CheetSheet.md"/>
|
||||||
|
<toc-element topic="rails-CheetSheet.md"/>
|
||||||
|
</instance-profile>
|
||||||
468
Writerside/topics/CheetSheet.md
Normal file
468
Writerside/topics/CheetSheet.md
Normal file
@@ -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
|
||||||
|
```
|
||||||
220
Writerside/topics/rails-CheetSheet.md
Normal file
220
Writerside/topics/rails-CheetSheet.md
Normal file
@@ -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
|
||||||
|
<!-- write some html -->
|
||||||
|
<div class="main">
|
||||||
|
<div class="container">
|
||||||
|
<h1>Hello my name is King</h1>
|
||||||
|
<p>I make Rails apps.</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
```
|
||||||
|
|
||||||
|
## 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
|
||||||
|
<div class="messages">
|
||||||
|
<div class="container">
|
||||||
|
<% @messages.each do |message| %>
|
||||||
|
<!-- iterates through each message in @messages created in the Messages controller's index -->
|
||||||
|
<div class="message">
|
||||||
|
<p class="content"><%= message.content %></p>
|
||||||
|
<p class="time"><%= message.created_at %></p>
|
||||||
|
</div>
|
||||||
|
<% end %> <%= link_to 'New Message', "messages/new" %>
|
||||||
|
<!-- Sets a link to the creation page -->
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
```
|
||||||
|
|
||||||
|
Open: app/views/messages/new.html.erb
|
||||||
|
|
||||||
|
```html
|
||||||
|
<div class="create">
|
||||||
|
<div class="container">
|
||||||
|
<!-- Create a form with a textarea field -->
|
||||||
|
<%= form_for(@message) do |f| %>
|
||||||
|
<div class="field">
|
||||||
|
<%= f.label :message %><br />
|
||||||
|
<%= f.text_area :content %>
|
||||||
|
</div>
|
||||||
|
<div class="actions"><%= f.submit "Create" %></div>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
```
|
||||||
|
|
||||||
|
## 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
|
||||||
11
Writerside/topics/starter-topic.md
Normal file
11
Writerside/topics/starter-topic.md
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
# Ruby
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo apt install ruby --classic
|
||||||
|
sudo apt install ruby-full
|
||||||
|
sudo apt install build-essentials
|
||||||
|
```
|
||||||
|
|
||||||
|
```bash
|
||||||
|
ruby -v
|
||||||
|
```
|
||||||
5
Writerside/v.list
Normal file
5
Writerside/v.list
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE vars SYSTEM "https://resources.jetbrains.com/writerside/1.0/vars.dtd">
|
||||||
|
<vars>
|
||||||
|
<var name="product" value="Writerside"/>
|
||||||
|
</vars>
|
||||||
8
Writerside/writerside.cfg
Normal file
8
Writerside/writerside.cfg
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE ihp SYSTEM "https://resources.jetbrains.com/writerside/1.0/ihp.dtd">
|
||||||
|
|
||||||
|
<ihp version="2.0">
|
||||||
|
<topics dir="topics" web-path="topics"/>
|
||||||
|
<images dir="images" web-path="images"/>
|
||||||
|
<instance src="r.tree"/>
|
||||||
|
</ihp>
|
||||||
Reference in New Issue
Block a user