Ruby Variables
local variable |
local_variable |
instance variable |
@instance_variable |
class variable |
@@class_variable |
global variable |
$global_variable |
Ruby Data Types
String |
"Sequence of characters."
or 'Sequence of characters.'
|
Symbol (identifier) |
:a_symbol, :another_symbol, :name
|
Integer |
|
Float (floating point) |
1.1, 3.1415926, 5.5, -128.6
|
Numeric Methods |
|
Comparison Operators |
|
Return Value |
Last line of a ruby method/block/expression in IRB |
Boolean |
|
|
counts as false in Ruby, everything counts as true |
Array (0 indexed) |
letters = ["a", "b", "c"]
|
|
#returns "c" |
|
|
ERB
ERB |
Embedded Ruby for making dynamic HTML templates |
|
Sinatra looks in "views" folder for hello.erb file |
Show @tweets |
<% if @tweets %> <% @tweets.each do |tweet| %> <p><%= tweet %></p> <% end %> <% end %> |
Instance Variables |
Any instance variables you declare in your routes, will be accessible in these erb files |
Ruby Expressions
|
Save string to variable |
|
Save integer to variable |
|
Save another integer to another variable |
answer = (num + other_num) * 6
|
Save result of adding/multiplying. |
String Interpolation
|
puts "Dogs say #{dog_speak}"
|
or |
puts "Dogs say " + dog_speak
|
Conditionals
if/else |
if input == admin_password
|
|
|
|
|
|
puts "Sorry, wrong password."
|
|
|
Useful Ruby Methods
|
Breaks a string into an array. |
|
Takes an array and joins into a string. |
|
Makes characters capital letters. |
|
Reverse characters in a string. |
|
Randomly choose element. |
|
Do the same thing to every element. |
Research
Cmd+Click (Mac) or Ctrl+Click |
Open links in new tabs. |
ruby-doc.org ("The Ruby Docs") |
Ruby coding best practices. |
Stack Overflow |
Oops. Research your error. |
Guides and blog posts |
Has someone done this before? |
|
|
Sinatra
Sinatra |
Framework for building a web server written in Ruby |
Routing |
Action taken in response to request |
Path |
text and slashes after domain |
http://localhost:4567/hello |
Sinatra looks for get '/hello'
|
|
|
|
#code to return a response to the browser
|
|
|
String Response |
|
|
"Hello, friendly web traveler!"
|
|
|
HTML File Response |
|
|
|
|
|
Get tweets route |
|
|
@tweets = $client.user_timeline.map { |tweet| tweet.text }
|
|
|
|
|
|
Get server to run code in get '/' route |
Array Methods
|
Add to end of array: letters << "elephant"
or letters.push("elephant")
|
|
Shovel operator. Push to end of array. |
|
Add element to beginning of array: letters.unshift("zebra")
|
|
Remove from end of array. letters.pop
|
|
Remove from start of array. letters.shift
|
|
Call a method with arguments. Some methods require no arguments. |
|
Method without arguments. |
Hashes
Instantiate a Hash (hash rockets) |
person = {:name => "Lauren", :age => 89, :sex => "female"}
|
Instantiate a Hash (shortcut) |
person = {name: "Lauren", age: 89, sex: "female"}
|
Access value by key. |
person[:name] # returns "Lauren"
|
Update value by key. |
|
Insert key, value pair. |
person[:gender] = "cis female"
|
Remove value by key. |
person.delete(:sex) # returns "female"
|
Methods
Greeting Method Accepts Name |
def create_greeting(name)
|
|
return "Welcome, #{name}"
|
|
|
Call Greeting Method |
create_greeting("Lauren")
|
|
#returns 'Welcome, Lauren"
|
Upcase On Call To Greeting Method |
create_greeting("Lauren").upcase
|
Arguments |
Input given to methods. |
Parameters |
Define what can be passed to a method. |
Return value |
Output from a function |
Implicit Return |
Returning last line of method without explicit "return" |
|
|
Command Line
Ruby gems |
Download, install, and use ruby software packages on your system |
|
Get Ruby Version |
|
Load up interactive Ruby |
|
Install sinatra gem |
|
Install twitter gem |
|
Tell Ruby interpreter to run code. |
Ctrl+C |
Stop server from running. |
Web and HTTP
Server |
has IP address, processes requests made by the (client) browser |
HTTP |
protocol enabling communication between clients and servers |
HTTP Verbs |
GET, POST, PUT, PATCH, DELETE
|
GET |
Request Tweets(data) from Twitter API |
POST |
Submit Tweet(data) to Twitter API |
https: |
scheme |
twitter.com |
domain |
lauren_n_roth/status/699996428429561856 |
path |
Params hash |
|
Loops
times |
|
|
|
|
|
times loop with counter |
|
|
|
|
|
|
letters = ["a", "b", "c"]
|
|
letters.each do |letter|
|
|
puts "Letter #{letter}"
|
|
|
|
capital_letters = letters.map do |letter|
|
|
|
|
|
block style |
letters.each { |letter| puts "Upcase #{letter.upcase}!" }
|
elegant map method |
capitals = letters.map { |letter| letter.upcase }
|
Twitter API
Download files |
bit.ly/dbc-twitter-sinatra |
Sign Up To Use Twitter API |
apps.twitter.com |
Create New App |
1. Create new App 2. Create Access Tokens 3. Ensure permissions are read/write |
Set up Twitter $client global variable |
|
|
$client = Twitter::REST::Client.new do |config|
|
|
config.consumer_key = "YOUR_CONSUMER_KEY" config.consumer_secret = "YOUR_CONSUMER_SECRET" config.access_token = "YOUR_ACCESS_TOKEN" config.access_token_secret = "YOUR_ACCESS_SECRET"
|
|
|
Get user timeline |
$client.user_timeline
|
Send tweet |
$client.update("omg I am tweeting from the command line. So cool!")
|
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment