Print the text "Hello, world!"
puts "Hello, world!"
What data type did you print?
string
Print the result of the expression 2 + 2
print 2 + 2
What data type did you print?
integer
Assign the result of the expression 2 + 2
to a variable named result
result = 2 + 2
print result
What data type is the value of result
?
integer
Write a method which takes two parameters and adds them, returning the result.
def adding(num1,num2)
num1 + num2
end
puts adding(1,2)
On what datatypes will the function you wrote work correctly? Incorrectly?
on an integer or float to work correctly, on a string to not work correctly
What is the difference between testing for equality and assigning a value to a variable? (Show an example of both)
testing for equality should use ==
assigning a variable should use =
What data type is [1, 2, 3, 4, 5]
?
array
Iterate through the above data structure, printing each number multiplied by two.
arr = [1, 2, 3, 4, 5]
arr.each do |i|
i * 2
end
p i
Iterate through the above data structure, printing "I am the [n]th element"
, replacing 'n' with each element's index.
arr = [1, 2, 3, 4, 5]
i = arr.each
for arr[i] do |i|
puts "I am element #{i}"
end
What data type is {cats: 3, dogs: 2, fish: 13}
?
hash
What is the data type and role of cats
in the above structure?
it's a element in a hash and the key of a key value pair
What are the roles of the integers in the above structure?
they're the values
Iterate through the above data structure, printing "Kyle has [n] [thing]s"
, replacing 'n' and 'thing' with the appropriate values.
pets = {cats: 3, dogs: 2, fish: 13}
for pets {:v, :k}
end
puts "Kyle has #{v} #{k}"
Write a method called max_animal
that accepts a data structure like the above as a parameter, and returns which type of animal Kyle has the most of.
m = pets.max
puts m
Write a class called Man
with an instance method move
, which prints walk
, and an attribute name
with a reader and writer method.
class Man
attr_reader :name
attr_writer :name
initialize (move, name)
@move = move
@name = name
end
end
man1 = Man.move("walk")
puts = man1
Write a subclass of Man
called Superman
, whose move
method prints fly
Man << Superman
@move = fly
end
man2 = Superman.move
puts man2
Add a class method to your Superman
class called is_superhero?
which returns true
.
Superman.is_superhero? == true
Do you need to add the name
attribute to Superman
? If not, why?
No, because name is inherited from the Man class
Does Ruby have methods, functions, or both?
Ruby has methods AND functions
Whichever of the above (method/function) you decided, can it be stored in a variable?
the object of methods can be stored in variable, but the actual method cannot. functions cannot be stored in a variable
What is the name of this construct in Ruby: do |thing| thing * 2 end
method
Print the text "Hello, world!"
console.log("Hello, world!")
Print the result of the expression 2 + 2
console.log(2 + 2)
Assign the result of the expression 2 + 2
to a local variable named result
var result == (2 + 2)
console.log(result)
Write a function which takes two parameters and adds them, returning the result.
var result = adding(num1, num2) {
num1 + num2;
}
return (adding(1,2))
On what datatypes will the function you wrote work correctly? Incorrectly?
integers will work correctly strings will not
What data type is [1, 2, 3, 4, 5]
?
array
Iterate through the above data structure, printing each number multiplied by two.
arr = [1, 2, 3, 4, 5]
for (var i=0; i < arr.length; i++){
return arr[i] * 2;
};
What data type is {cats: 3, dogs: 2, fish: 13}
? (Hint: NOT the same as the Ruby answer)
object
In setTimeout(doStuff, 2000)
, what datatype should doStuff
be and what do we call that datatype in this role?
object, and in this role it's a method
What datatype is foo
in var foo = function() { alert("Hello"); };
?
foo is a variable
Is a function a type of object?
yep
Show how you would use var Person = function(){};
to construct a Person.
What is a prototype?
an object that takes the place of (or stands in for) a different, probably more complicated object from earlier in the code
What do I mean every time I say "In Javascript, functions are first-class values"?
because they are executed first
What is the difference between an AJAX request and a typical HTTP request?
AJAX operates on the same page
What type (three letter acronym) of framework is Rails?
aaah, forget
Define each of the three components
In your shell...
Make a new rails project called blog
rails new blog
In that project, make a new controller PostsController
rails g controller Posts
Make a Post
model
rails g model Post
Make PostsController
and the Post
model, as well as RESTful routes, in a single step.
rails g contoller Posts, rails g model Post, scaffolding
Run the migrations for Post
rake db:migrate
See all your app's routes
rake routes
Start the web server
In general...
rails s
What is the name of the (default) superclass of all of our generated controllers?
<b>Base</b>
What do we call controller instance methods that respond to web requests?
CRUD
What, by default, would one such method (say, show
) do if I leave its method body empty?
print the page
What kind of template is application.html.erb
?
view
What kind of template is posts/_form.html.erb
?
partial
In an ERB template, with a @post
var available, how would I render a link to that post's show page?
<%= link_to @post, "show#index" %>
What relationships would you define in your models if a user can have multiple posts?
has_many :posts
What field would you need to add to post?
belongs_to :user
What relationships would you define in your models if a post could have multiple categories, and a category could belong to multiple posts? What tables would you need to add?
In the Rails console...
inner_join
How many Post
s exist in the database?
1
How many Post
s by user with id 1
? (assuming I've properly associated a User
model with the Post
model)
2
Find the first post
Post.first
Destroy the last post
Post.last(delete)
Destroy all posts by user with id 5
.
Post.delete(5)
Create a post with user id 1
, title "My Post", and body "This is the body of my first post"
Post.create(id: '1', title: 'My Post', body: 'This is the body of my first post')
Find all the caterogies associated with the first post.
Post.find.first.all
Print out all the titles of the categories associated with the first post.
Post.all.first
#####fucking hell, without my notes or Google i don't know any of these :(
foo
bar
<div>
s on the page.baz
.foo
input
elements from the page.foo
oops
foo
GET
request to /thingList.json
, iterating through the returned result set and logging each item to the console.