OSCON 2005 – Day 2
On the agenda for today is “Learning Ajax” with Alex Russell and probably the XSLT track with Lentz. As much as I hate XSLT, I should probably go because I am having to use it for my current project. Perhaps I’ll learn something.
Was just looking over my blog from yesterday, and they pretty much suck as compared to this and this
Mine seem more like notes.
OSCON 2005 – Day 1 (Rails w/ DHH)
Kickoff – should be a code heavy presentation…
Install ruby dated 12/25/2004
gem install rails to install rails (requires ruby gems)
Demo based around creating a blog app.
David is giving a detailed explanation of directory structure and built in webserver. Seems to be standard stuff in docs. Major point is that everything is layed out for you so you can get started more quickly and do not have to make those decisions.
… Speaks about the ./script/generate program which stubs out controllers, models, etc.
if a rhtml file is named the same as a method on the appropriate controller, that rhtml file will be rendered by default as the return from the action method.
precoderedirect_to :action = “method_name”/code/pre will jump to another action method on controller.
./script/destroy can remove models/controller/etc just like it can generate them.
Rails has extended the to_s method to include parameters (for date) so you can say my_date.to_s(:long) or my_date.to_s(:short)
foreign key names are not plural – should be same as model name. fk or comments to posts is post_id not posts_id (sorry if that one is not in context of anything else)
can specify foreign keys when defining the relationship
precodehas_many :comments, :foreign_key = “crazy_dba_convention_id”/code/pre
with ./script/console you can interact directly with your model.
Pass in :locals { :variable = my_variable } you can render specific to what ever scope you are in.
Foreign key collections have extra convience methods such as create and build.
@post.comments.create(blah) where comments is a collections of comments on post.
layout the reverse of jsp include. view maps to controller name and is used as a template for any html that needs to be rendered for that controller.
Use ApplicationController which was generated as part of generating your project for extension points such as authentication (ApplicationController is a base class of all other generated Action Controllers)
Unit tests with fixtures, fixtures load the entire fixture file for that db class and loads the values into instance variables by their name.
precode
def test_fixtures_work
assert_equal “FooBarBaz”, @my_first_post[‘title’]
end
/code/pre
Where there is a fixture entry with the title my_first_post with the title of ‘FooBarBaz’
Unit tests usually only useful for testing your methods and the domain model.
Functional Testing Mocks already built for simulating gets and posts. Used for testing controllers and workflow of controllers. Can assert things such as responses (assert_response :success assert_template ‘foobar’)
When functional testing, you can test create or update methods similar to this: (code may make no sense, but methods will be like this)
precode
def test_create_post
post :create, :post = { :title = “foo”, :body = “bar” }
assert_response :redirect
assert_kind_of Post, Post.find_by_title(“This is my title”)
post :create, :post = { :title = "", :body = “bar” }
assert_response :success
assert_equal “don’t leave me out”, assigns(:post) .errors_on(:title)
end
/code/pre
AJAX
In template do
precode
%= javascript_include :defaults %
/code/pre
damnit, think the javascript_include is correct but may be wrong – DHH moved on.
use the remote call replacement (linkto_remote instead of link_to for example) and pass in parameters (controller, id of element in html, etc)
can call precoderender :nothing = true/code/pre in action if nothing needs to be rendered for ajax call
can distinguish ajxx from non ajax by using the method request.xml_http_request?
DHH must be tired – he’s ajaxing the wrong files and it’s getting slightly confusing.
Need to clear input fields when ajaxing a form. Example if you have inputs for name and address, you will have to clear the name and address input boxes.
OSCON 2005 – Day 1 (Ruby w/ Dave Thomas)
Finally…, it’s here. And the line up for today is Intro to ruby w/ Dave Thomas and Rails with DHH.
Dave Thomas is one of my most favorite technical authors – this should be good…
Introduction to Ruby
Is programming still fun? in and of itself it is fun – it’s the language and the tools. Rise of scripting language make it more fun b/c less time to run program.
Ruby born in japan 1994 – Mats
Ruby passes the 5 minute test.
Wait – this is funny – Dave is presenting in socks.
Ruby removes alot of the inherited cruft of other languages – no need to put () around method and class definitions – don’t need to put ; at the end of a line. These things are unnecessary -the compiler/interpreter can figure it out.
new is not a keyword. It is built into the object. Makes overriding the functionality of creating the object more flexible (perhaps by using class level variables?)
attributes begin w/ @
Attributes and methods are one and the same. Makes for universal access
precode
@my_var
attr_reader :my_var
/code/pre
is the same as
precode
def my_var=(x)
@a = x
end
def myvar
return @a
end
/code/pre
Blocks and iterators are pervasive in Ruby
The santa clause theory: precode3.times { puts “Ho!” }/code/pre
| variable |
is called a block parameter
yield inside a method definition looks at code and stores in your “back pocket” which it executes later. (Thats a pretty crappy writeup…)
Ruby convention for iterators. Block should test for params. If block is not given , code should store in an array and return at the end of the method/iterator call.
Blocks as callbacks – use callback syntax as argument to initialize. If block not given, nil is stored. Can use later like
precode
callback.execute if callback
/code/pre
Resource management w/ blocks. Use block with resources to ensure resources are closed afterwards.
precode
File.open(‘/etc/passwd’) do
#… do something
end
/code/pre
File will be closed automagically at the end of the block.
Did not know this. Per-statement exception handling
precode
def x(name)
f = File.open(name)
yield f ensure f.close
end
/code/pre
where yield f ensure f.close is the per statement exception handling
Transparent block passing
precode
def x(name, callback)
File.open(name, callback)
end
/code/pre
Duck typing. Ruby has strongly-typed objects but untyped variables and methods. Type is determined by object protocol (by what object can do) Just have to make sure the types you are passing in support the methods that the object needs. If it walks like a duck and talks like a duck, it must be a duck.
Ruby community differentiates the type and class of an object
* type: what it can do
* class: what generated it (who created this object)
Metaprogramming mini DSL’s within you classes
How do we get there:
- Classes are open and can be extended – ex: can add stuff to string. ex – could add an encryption method to string so you could say cat.encrypt. yes it is dangerous to redefine core class methods like overriding the + method on Fixednum, but just b/c some idiot might do it doesn’t mean it should not be there. I you do something stupid, take ourself out to the parking lot and beat self w/ rubber hose.
- Definitions are active Add to classes with things such as adding tomorrow and yesterday methods to the Time class. One thing that was really driven home for me is that you can execute arbitrary code in you classes (functional). For ex. Caching. in one of your classes, you could read a file and store it in a string as part of the definition. Then methods on the call would have ready acccess to the string or array. Put another way, you can write code that executes during class definitions.
- All methods calls have a receiver “ruby”.length “ruby” is the receiver.
This is very powerful for metaprogramming
precode
class Doug
end
class Bryant
end
variable = day.even? Doug: Bryant
variable.do_something
/code/pre
Learned you can use the “inherited(subclass)” method on base classes and will automagically be called when you extend the base class.
Other stuff
Object space can tell you about objects at runtime. ex. give me all the strings in my program. Useful for such things as debugging – why is that person object still hanging around
Dave Thomas is an excellent speaker. I was very impressed. If you ever get a chance to hear him speak, do it. He has a very clear and concise way of explaining things. Not to mention a good sense of humor. He keeps things technical and assumes the audience are not all dumbasses – he knows we have all done some sort of software development before and does not stick on the syntax of the language.
Nerdcore
I ran across this on slashdot this afternoon. It’s rap set to computer science and math instead of gangsta rap.
Check out these lyrics from monzy.
One of the funny quotes:
Your mom circulates like a public key,
Servicing more requests than HTTP.
ruby, ruby, ruby. learning ruby
I have been studying ruby very closely this week while trying build up my ruby chops.
Some interesting articles I ran across…
Explanation of class self At ruby garden here and
here
And another interesting article about ruby garbage collection
Strange conversation at drug store
I stopped by a Kerr Drugs this morning to pick up some advil for a headache.
The clerk, a middle aged woman, engaged me in a very interesting and disturbing conversation.
Self: Morning, how are you doing.
Clerk: Good.
Clerk: Do you know where to buy pepper spray?
Self: No… (thinking) Do you mean something like mace.
Self: Perhaps a hunting supply store or something like that. I don’t remember ever seeing any in stores. (Now a very perplexed look on my face)
Clerk: I’ll just get my nephew at the highway department to get me some.
Clerk: I want some because someone who works in here keeps on touching my face.
Clerk: Do you think that will make the person stop touching my face?
Self: Uhhh… (very confused and amused now) Well it should. See ya. (walking quickly out of store at this point)
And the sad thing is that I don’t think she was joking.
OSCON 2005
I just got signed up for OSCON 2005 in Portland. I’m totally stoked. There is a great line up this year.
Some of the tracks I intend to attend… (Very Ruby Heavy)
- Intro to Ruby w/ Dave Thomas
- Ruby on Rails: Enjoying the Ride of Programming
- Learning Ajax
- Integrate: Building a Site from Open Source Gems
- Application Development With Firefox and Mozilla
- WebWork vs. Spring MVC Smackdown
- States of the Databases
- Open Office Xml Doc Format
- SiteMesh: A Simple Approach to Web Site Layout
- Tapestry In Action
- Ruby Blocks
- Pragmatic Project Automation with Ruby
- Metaprogramming Ruby
- Dependency Injection: irrelevant?
Holy Cow! I knew it was ruby heavy, but did did not realize how heavy it was until I saw it listed here.
Quote of the day
From a co-worker while talking about Revenge of the Sith
If I ever find myself homeless, I’ll just camp outside of a movie theater. That way, nobody will ever know I am homeless.
