Вы находитесь на странице: 1из 3

RubyForBeginners Cheat Sheet

by laurennicoleroth via cheatography.com/26292/cs/7316/

Ruby Variables ERB Conditionals

local variable local_variable ERB Embedded Ruby for making if/else if input == admin_password

instance variable @instance_variable dynamic HTML templates


admin_logged_in = true
erb Sinatra looks in "views" folder for
class variable @@class_variable
else
:hello hello.erb file
global variable $global_variable
puts "Sorry, wrong
Show <% if @tweets %> <%
password."
Ruby Data Types @tweets @tweets.each do |tweet| %>
<p><%= tweet %></p> <% end %> end
String "Sequence of
<% end %>
characters." or
Instance Any instance variables you declare Useful Ruby Methods
'Sequence of
Variables in your routes, will be accessible in
split Breaks a string into an array.
characters.' these erb files

Symbol :a_symbol, join Takes an array and joins into a

(identifier) Ruby Expressions string.


:another_symbol, :name
upcase Makes characters capital letters.
Integer 42, 100, 1002, -256 seen_it = "seen Save string to
again" variable reverse Reverse characters in a string.
Float 1.1, 3.1415926, 5.5, -
(floating 128.6 num = 3 Save integer to sample Randomly choose element.
point) variable
map Do the same thing to every
Numeric +, -, *, /, % other_num = 5 Save another integer element.
Methods to another variable

Comparison ==, !=, >, >=, <, <= answer = (num + Save result of Research
Operators other_num) * 6 adding/multiplying.
Cmd+Click (Mac) or Open links in new
Return Last line of a ruby Ctrl+Click tabs.
Value method/block/expression in IRB String Interpolation
ruby-doc.org ("The Ruby coding best
Boolean true or false dog_speak = puts "Dogs say # Ruby Docs") practices.

nil counts as false in Ruby, "woof!" {dog_speak}" Stack Overflow Oops. Research your
everything counts as true error.
or puts "Dogs say " +
Array (0 letters = ["a", "b", dog_speak Guides and blog posts Has someone done
indexed) "c"] this before?

letters[2 #returns "c"


]

letters[1 ["a", "monkey", "c"]


] =
"monkey"

By laurennicoleroth Published 25th February, 2016. Sponsored by Readability-Score.com


Last updated 25th February, 2016. Measure your website readability!
Page 1 of 3. https://readability-score.com

cheatography.com/laurennicoleroth/
RubyForBeginners Cheat Sheet
by laurennicoleroth via cheatography.com/26292/cs/7316/

Sinatra Array Methods Hashes (cont)

Sinatra Framework for building a web push Add to end of array: letters << Remove person.delete(:sex) #
server written in Ruby "elephant" or value by returns "female"
key.
Routing Action taken in response to letters.push("elephant")
request
<< Shovel operator. Push to end of
Methods
Path text and slashes after domain array.
http://localh Sinatra looks for get '/hello' Greeting Method def
unshift Add element to beginning of array:
ost:4567/he Accepts Name create_greeting(nam
letters.unshift("zebra")
llo e)
pop Remove from end of array.
get '/hello' do return "Welcome, #
letters.pop
#code to return a {name}"
shift Remove from start of array.
response to the browser letters.shift end
end .method Call a method with arguments. Call Greeting create_greeting("La
String get '/hello' (argum Some methods require no Method uren")
Response ents) arguments.
#returns 'Welcome,
"Hello, friendly web letters Method without arguments. Lauren"
traveler!" .pop
Upcase On Call create_greeting("La
end To Greeting uren").upcase
Hashes Method
HTML File get '/hello' do
Response Instantiate person = {:name => Arguments Input given to methods.

erb :hello a Hash "Lauren", :age => 89, Parameters Define what can be
(hash :sex => "female"} passed to a method.
end
rockets)
Return value Output from a function
Get tweets get '/' do Instantiate person = {name:
Implicit Return Returning last line of
route a Hash "Lauren", age: 89, sex: method without explicit
@tweets = (shortcut) "female"} "return"
$client.user_timeline.map
Access person[:name] # returns
{ |tweet| tweet.text } value by Command Line
"Lauren"
erb :index key.
Ruby Download, install, and use ruby
Update person[:age] = 12 gems software packages on your system
end
value by
$ Get Ruby Version
Redirect Get server to run code in get '/' key.
route ruby
'/'
Insert key, person[:gender] = "cis
-v
value pair. female"
$ Load up interactive Ruby
irb

By laurennicoleroth Published 25th February, 2016. Sponsored by Readability-Score.com


Last updated 25th February, 2016. Measure your website readability!
Page 2 of 3. https://readability-score.com

cheatography.com/laurennicoleroth/
RubyForBeginners Cheat Sheet
by laurennicoleroth via cheatography.com/26292/cs/7316/

Command Line (cont) Loops Twitter API (cont)

$ gem install Install sinatra gem times 10.times do Set up require twitter
sinatra Twitter
puts "Hello!"
$client
$ gem install Install twitter gem
end global
twitter variable
times 10.times do |counter|
$ ruby Tell Ruby interpreter to loop with $client =
server.rb run code. counter Twitter::REST::Client.new

Ctrl+C Stop server from puts "#{counter}" do |config|


running. config.consumer_key =
end
"YOUR_CONSUMER_KEY"
each letters = ["a", "b",
Web and HTTP
config.consumer_secret =
"c"]
Server has IP address, processes "YOUR_CONSUMER_SECRET"
letters.each do |letter|
requests made by the config.access_token =
(client) browser puts "Letter #{letter}" "YOUR_ACCESS_TOKEN"
HTTP protocol enabling end config.access_token_secret
communication between = "YOUR_ACCESS_SECRET"
clients and servers map capital_letters =
letters.map do |letter| end
HTTP Verbs GET, POST, PUT,
PATCH, DELETE letter.upcase Get $client.user_timeline
user
GET Request Tweets(data) end
timeline
from Twitter API
block letters.each { |letter| Send $client.update("omg I am
POST Submit Tweet(data) to style puts "Upcase # tweet tweeting from the command
Twitter API
{letter.upcase}!" } line. So cool!")
https: scheme
elegant capitals = letters.map {
twitter.com domain map |letter| letter.upcase }
lauren_n_roth/stat path method
us/6999964284295
61856 Twitter API

Params hash params[:tweet]


Download bit.ly/dbc-twitter-sinatra
files

Sign Up apps.twitter.com
To Use
Twitter
API

Create 1. Create new App 2. Create


New App Access Tokens 3. Ensure
permissions are read/write

By laurennicoleroth Published 25th February, 2016. Sponsored by Readability-Score.com


Last updated 25th February, 2016. Measure your website readability!
Page 3 of 3. https://readability-score.com

cheatography.com/laurennicoleroth/

Вам также может понравиться