ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Feels Like Ruby
                     Sarah Mei
                    Pivotal Labs

                        @sarahmei
                   sarah@pivotallabs.com




                                           1

¤¹¤ß¤Þ¤»¤ó©`¤Ï¤¸¤Þ¤ê¤Þ¤·¤ç¤¦¤«¡£
½Ô¤µ¤ó¡¢¤³¤ó¤Ë¤Á¤Ï

          Hi everybody



                         2

¤ß¤ó¤Ê¤µ¤ó¡¢¤³¤ó¤Ë¤Á?¤Ï¡£
¥á¥¤        ¥µ¥é
     ÃûÇ°¤Ï MEI Sarah ¤Ç¤¹¡£

            I¡¯m Sarah Mei.




                                               3

¤Ê¤Þ¤¨?¤Ï?Mei Sarah?¤Ç¤¹¡£¤Ï¤¸¤á¤Þ¤·¤Æ¡¢¤É¤¦¤¾¤¦?¤è¤í¤·¤¯?¤ª¤Í¤¬¤¤¤·¤Þ¤¹¡£
ÈÕ±¾Ó郎¤è¤¯»°¤»¤Ê¤¤¤Ç¤¹¤«¤é¡¢
              ¤¹¤ß¤Þ¤»¤ó¡£

    I don¡¯t speak Japanese very well, so
   thanks in advance for your patience.




                                           4

¤Ë¤Û¤ó¤´?¤¬?¤è¤¯?¤Ï¤Ê¤»¤Ê¤¤?¤Ç¤¹?¤«¤é¡¢¤¹¤ß¤Þ¤»¤ó¡£
http://www.?ickr.com/photos/sunrise/4064417/
                                                                       5

San Francisco¤Ë¤¹¤ó¤Ç¤¤¤Þ¤¹¡£
¥¢¥á¥ê¥«ÈˤǤ¹¡£                             I¡¯m American.




               http://www.?ickr.com/photos/junewess/3756778580/
                                                                  6

America¤¸¤ó¤Ç¤¹¡£
¸é³Ü²ú²â¤È¸é²¹¾±±ô²õ¤Î¿ª°kÕߤǤ¹¡£


          I¡¯m a Ruby & Rails
               developer.


                       Image copyright 2006-2010 Yukihiro Matsumoto
                                                                  7

Ruby?¤Î?¤«¤¤¤Ï¤Ä¤·¤ã?¤Ç¤¹¡£
I work at
                                             ¥Ô¥ô¥©¥¿¥ë¥é¥Ö¥¹


                                ¤ËÇÚ¤á¤Æ¤¤¤Þ¤¹¡£




                                                                     8

Pivotal Labs?¤Ë?¤Ä¤È¤á¤Æ?¤¤¤Þ¤¹¡£Pivotal Labs?¤Ï?Pivotal Tracker?¤ò?¤»¤¤¤µ¤¯?¤·¤Æ?¤¤
¤Þ¤¹¡£¤Ç¤â¡¢¤Û¤È¤ó¤É?¤Î?Pivotal Labs?¤Î?¤·¤ã¤¤¤ó?¤Ï?¥³¥ó¥½¥ë¥¿¥ó¥Ã¥È?¤Ç¤¹.?
I work at
                                             ¥Ô¥ô¥©¥¿¥ë¥é¥Ö¥¹


                                ¤ËÇÚ¤á¤Æ¤¤¤Þ¤¹¡£


                               ¥Ô¥ô¥©¥¿¥ë¥È¥é¥Ã¥«©`



                                                                     8

Pivotal Labs?¤Ë?¤Ä¤È¤á¤Æ?¤¤¤Þ¤¹¡£Pivotal Labs?¤Ï?Pivotal Tracker?¤ò?¤»¤¤¤µ¤¯?¤·¤Æ?¤¤
¤Þ¤¹¡£¤Ç¤â¡¢¤Û¤È¤ó¤É?¤Î?Pivotal Labs?¤Î?¤·¤ã¤¤¤ó?¤Ï?¥³¥ó¥½¥ë¥¿¥ó¥Ã¥È?¤Ç¤¹.?
£¨¤³¤ÎÏȤ«¤éÓ¢Óï¤Ç¤¹¡£¤´¤á¤ó¤Í£©




                                               9

¤¸¤³¤·¤ç¤¦¤«¤¤?¤¬?¤ª¤ï¤ê?¤Ç¤¹?¤«¤é¡¢¤³¤Î?¤µ¤­?¤«¤é?Ó¢ÕZ?¤Ç¤¹¡£¤´¤á¤ó?¤Í¤¨¡£¡£¡£
£¨¤³¤ÎÏȤ«¤éÓ¢Óï¤Ç¤¹¡£¤´¤á¤ó¤Í£©


        Feels Like Ruby



                                               9

¤¸¤³¤·¤ç¤¦¤«¤¤?¤¬?¤ª¤ï¤ê?¤Ç¤¹?¤«¤é¡¢¤³¤Î?¤µ¤­?¤«¤é?Ó¢ÕZ?¤Ç¤¹¡£¤´¤á¤ó?¤Í¤¨¡£¡£¡£
£¨¤³¤ÎÏȤ«¤éÓ¢Óï¤Ç¤¹¡£¤´¤á¤ó¤Í£©


        Feels Like Ruby
           Making JavaScript a
             Real Language



                                               9

¤¸¤³¤·¤ç¤¦¤«¤¤?¤¬?¤ª¤ï¤ê?¤Ç¤¹?¤«¤é¡¢¤³¤Î?¤µ¤­?¤«¤é?Ó¢ÕZ?¤Ç¤¹¡£¤´¤á¤ó?¤Í¤¨¡£¡£¡£
What I like about Ruby




                                                                                                  10

I want to set the stage by talking about what I enjoy about Ruby. First and foremost, it¡¯s the
language itself. I love the expressiveness and accessibility.

But as a working developer, what I appreciate the most is the ability to test-drive everything.
Between rspec, test::unit, cucumber, capybara, webrat, shoulda, steak....I can pick the most
appropriate test tools for each project.
What I like about Ruby



           ? The language itself.



                                                                                                  10

I want to set the stage by talking about what I enjoy about Ruby. First and foremost, it¡¯s the
language itself. I love the expressiveness and accessibility.

But as a working developer, what I appreciate the most is the ability to test-drive everything.
Between rspec, test::unit, cucumber, capybara, webrat, shoulda, steak....I can pick the most
appropriate test tools for each project.
What I like about Ruby



           ? The language itself.
           ? I can test-drive everything.


                                                                                                  10

I want to set the stage by talking about what I enjoy about Ruby. First and foremost, it¡¯s the
language itself. I love the expressiveness and accessibility.

But as a working developer, what I appreciate the most is the ability to test-drive everything.
Between rspec, test::unit, cucumber, capybara, webrat, shoulda, steak....I can pick the most
appropriate test tools for each project.
What I like about Ruby



           ? The language itself.         JavaScript

           ? I can test-drive everything. JavaScript


                                                                                 11

So let¡¯s see how these options stack up in JavaScript. The language itself...?
What I like about Ruby



           ? The language itself.             X
                                          JavaScript

           ? I can test-drive everything. JavaScript


                                                                                               12

Well, I¡¯m never going to be able to change that. I don¡¯t dislike JavaScript, but Ruby ?ts my
personality better. But what about test-driving?
What I like about Ruby



           ? The language itself.             X
                                          JavaScript

                                              O
           ? I can test-drive everything. JavaScript


                                                                                                13

We should be able to test-drive JavaScript. But until earlier this year, I didn¡¯t. And some of the
best Rails developers I know, who test-drive all their Ruby code, still don¡¯t test-drive their
JavaScript. So the question is...
http://www.?ickr.com/photos/petereed/496392956
                                                                                     14

Why not? Why do we make JavaScript development so painful?

I think part of it is a philosophical mismatch in the way Rails treats JavaScript.
15

Web applications are fundamentally written in multiple languages. Here¡¯s a very generic
representation of a Ruby web application. On the front end, there¡¯s HTML, CSS, and
Javascript, and on the back end there is one (or more!) server languages, plus SQL. And of
course, normal applications have other front-end languages too, like ActionScript or
Objective-J, and other back-end languages interacting with Ruby, and usually additional data
stores as well. I can¡¯t remember the last time I worked on an application that had ONLY a
relational database for storage.

But traditionally, in this setup, programming languages sit here in the middle. But
frameworks try to extend on either side. That¡¯s what Rails does.
ActiveRecord




                                                                                     Rails




                                                                                             16

For example, Rails extends this direction, and abstracts away SQL with ActiveRecord. It¡¯s
pretty successful with that. Even on really complex applications, I can get away with the
generated SQL most of the time.
erb
  sass
   rjs
                                                                                ActiveRecord




                Rails/gems                                                             Rails




                                                                                                   17

But it also extends the way with erb and rjs to generate HTML and JavaScript. And with other
gems, you can generate CSS too. And although this was a good idea with SQL and
ActiveRecord, and it works decently well with HTML and CSS, RJS is kind of a disaster. I¡¯ll show
you an example in moment.

But ?rst I want to point out that this is not a failing of Rails, or of the people who wrote it.
This is the nature of Javascript. It might be the one language whose use is evolving even
faster than Ruby. SQL has an ISO standard and a committee, and if they make any signi?cant
changes, we¡¯ll have ?ve years¡¯ notice. Moreover, the database products that implement SQL
have committees, so as SQL end-users, we¡¯re exceedingly well-insulated from any language
churn.
JavaScript Developers




                                 http://www.?ickr.com/photos/gem66/387400306
                                                                                                        18

But Javascript...is the wild west. It¡¯s the frontier. While the language itself is relatively stable,
its libraries and usage patterns are changing faster than that of Ruby, or of Rails, or of any
other framework.

What does that mean for us as web application developers, as multi-lingual programmers?
erb
  sass
   rjs
                                                                             ActiveRecord




                Rails/gems                                                          Rails




                                                                                            19

It means we need to opt-out, to make an exception, and to handle JavaScript differently.
Now, that¡¯s the philosophical reason - now I want to show you the practical reason.
20

In Rails 2, using RJS means you have little bits of Javascript all over your code.

Here¡¯s an example I adapted from the ¡°complex forms¡± railscast. This is a to-do list app, in
which you can create a project with any number of tasks attached. This is the new project
form. It has 3 task text?elds, but you can add another one by clicking ¡°Add a task¡±, and you
can delete one by clicking the ¡°remove¡± link that¡¯s next to it. Both of these are implemented
as simple JavaScript that modi?ed the DOM. For the purposes of this example, we¡¯re going to
focus on the remove link.

So let¡¯s look at how that¡¯s implemented.
21

This is the partial that is rendered for each task. The important part is the link_to_function,
where when we click remove, it calls that little piece of javascript.
22

Here¡¯s what the rendered HTML looks like. You¡¯ve got that little bit of inline javascript, which
on its own, is fairly simple. But it¡¯s not really testable.

Sure, you could write a selenium test that tests that it actually removes the dom element.
However, Selenium is slow. This is one simple interaction - in a typical modern web
application, there¡¯s likely to be dozens of these on one page, if not hundreds. Testing them
all with Selenium would mean you¡¯d have a test suite that never stopped running.

Plus, Selenium is an integration test. If all you¡¯re doing is integration tests, you¡¯re doing it
wrong. You need both unit tests and integration tests to probe all the behavior of your code.

So how can we re-do this in a way that is testable and repeatable?
A different approach




                                                                                                 23

1. Forget RJS - it gets in your way once you do anything beyond the very simple.
2. Put your JS in classes - of course JS is a prototype-based language instead of an
inheritance-based language, but you can still organize your functions into sets. I¡¯ll show you
what that looks like in a moment.
3. Organize your JS by behavior, and by location.
4. Test-drive all your JS.
A different approach

        ? Forget RJS - write functions




                                                                                                 23

1. Forget RJS - it gets in your way once you do anything beyond the very simple.
2. Put your JS in classes - of course JS is a prototype-based language instead of an
inheritance-based language, but you can still organize your functions into sets. I¡¯ll show you
what that looks like in a moment.
3. Organize your JS by behavior, and by location.
4. Test-drive all your JS.
A different approach

        ? Forget RJS - write functions
        ? Put your functions in classes




                                                                                                 23

1. Forget RJS - it gets in your way once you do anything beyond the very simple.
2. Put your JS in classes - of course JS is a prototype-based language instead of an
inheritance-based language, but you can still organize your functions into sets. I¡¯ll show you
what that looks like in a moment.
3. Organize your JS by behavior, and by location.
4. Test-drive all your JS.
A different approach

        ? Forget RJS - write functions
        ? Put your functions in classes
        ? Organize classes by behavior and
             location




                                                                                                 23

1. Forget RJS - it gets in your way once you do anything beyond the very simple.
2. Put your JS in classes - of course JS is a prototype-based language instead of an
inheritance-based language, but you can still organize your functions into sets. I¡¯ll show you
what that looks like in a moment.
3. Organize your JS by behavior, and by location.
4. Test-drive all your JS.
A different approach

        ? Forget RJS - write functions
        ? Put your functions in classes
        ? Organize classes by behavior and
             location

        ? Test-drive your classes.


                                                                                                 23

1. Forget RJS - it gets in your way once you do anything beyond the very simple.
2. Put your JS in classes - of course JS is a prototype-based language instead of an
inheritance-based language, but you can still organize your functions into sets. I¡¯ll show you
what that looks like in a moment.
3. Organize your JS by behavior, and by location.
4. Test-drive all your JS.
A different approach
                                                   Unobtrusive JavaScript

         ? Forget RJS - write functions
         ? Put your functions in classes
         ? Organize classes by behavior and
             location

         ? Test-drive your classes.


                                                                                               24

These ?rst two techniques are collectively known as ¡°unobtrusive JavaScript.¡± I am happy to
report that Rails 3 is moving to unobtrusive JavaScript. So Rails 3 will help this problem quite
a bit, but, as you can see, there is more to do beyond ¡°unobtrusiveizing.¡± Let¡¯s look at how
we¡¯d do it.
25

Here¡¯s our original example, with the remove link. How we could re-implement it? Before, the
code looked like this...
26

But we can simplify this now. All we really have here is a link that we want to add a behavior
to.
27

So in the erb template, we¡¯ll just make a link with a class on it. No reference to JavaScript
anywhere here. And here is...
28

...the HTML it generates. So. If we don¡¯t put our Javascript in the HTML...where should we put
it?
public/javascripts !




                                                                                                29

In general, I keep my JavaScript ?les in public/javascripts. So we¡¯ll create a ?le in public/
javascripts called project_form.js
project_form.js




                                                                                                30

In this ?le, we create a class called RubyKaigi.ProjectForm, and we put in two functions. The
two functions are initialize and remove_task.
project_form.js




                                                                                                   31

In initialize, we add a click event to all links with class .remove-link. When that link is clicked,
our other function is called.
project_form.js




                                                                                           32

That function, remove_task, actually removes the element with class ¡®task¡¯ from the DOM.
project_form.js




                                                                                                 33

The last thing that¡¯s important here is the document.ready, which calls initialize, which sets
up the click events, once the page is loaded.
34

So I¡¯ve created a JavaScript class that encapsulates the behavior of the project form. Once I
implement the ¡°Add a task¡± link, that behavior will go in the same class.
35

Then it might look something like this, with a new function at the bottom, and some extra
initialization at the top.
Organizing by page
What if you use the ProjectForm JS on
more than one page?




                                        36
Organizing by page
What if you use the ProjectForm JS on
more than one page?



Move ProjectForm.initialize to a page
class initializer




                                        36
37
project_edit_page.js




                       38
project_new_page.js




                      39
http://www.?ickr.com/photos/uwehermann/132244826
                                                                                               40

So that¡¯s how you organize your JavaScript so it¡¯s all in one place. But what about testing?
Jasmine




                             @jasminebdd
                                                                                              41

That¡¯s where Jasmine comes in. Jasmine is Pivotal¡¯s open-source JavaScript testing
framework.

It lets you do ¡°rspec-style¡± testing.

The philosophy of Jasmine is that JavaScript should be tested with JavaScript. There are gems
out there such as harmony that let you test JavaScript with Ruby, but that just adds another
layer of indirection. JavaScript should be a ?rst-class language in web applications.

Also, it is not dependent on the DOM, so it can be used to test other types of JavaScript, such
as for WebOS.
Jasmine
           ? Open-source JavaScript test
                framework from Pivotal




                             @jasminebdd
                                                                                              41

That¡¯s where Jasmine comes in. Jasmine is Pivotal¡¯s open-source JavaScript testing
framework.

It lets you do ¡°rspec-style¡± testing.

The philosophy of Jasmine is that JavaScript should be tested with JavaScript. There are gems
out there such as harmony that let you test JavaScript with Ruby, but that just adds another
layer of indirection. JavaScript should be a ?rst-class language in web applications.

Also, it is not dependent on the DOM, so it can be used to test other types of JavaScript, such
as for WebOS.
Jasmine
           ? Open-source JavaScript test
                framework from Pivotal

           ? BDD in the style of rspec



                             @jasminebdd
                                                                                              41

That¡¯s where Jasmine comes in. Jasmine is Pivotal¡¯s open-source JavaScript testing
framework.

It lets you do ¡°rspec-style¡± testing.

The philosophy of Jasmine is that JavaScript should be tested with JavaScript. There are gems
out there such as harmony that let you test JavaScript with Ruby, but that just adds another
layer of indirection. JavaScript should be a ?rst-class language in web applications.

Also, it is not dependent on the DOM, so it can be used to test other types of JavaScript, such
as for WebOS.
Jasmine
           ? Open-source JavaScript test
                framework from Pivotal

           ? BDD in the style of rspec
           ? Philosophy:


                             @jasminebdd
                                                                                              41

That¡¯s where Jasmine comes in. Jasmine is Pivotal¡¯s open-source JavaScript testing
framework.

It lets you do ¡°rspec-style¡± testing.

The philosophy of Jasmine is that JavaScript should be tested with JavaScript. There are gems
out there such as harmony that let you test JavaScript with Ruby, but that just adds another
layer of indirection. JavaScript should be a ?rst-class language in web applications.

Also, it is not dependent on the DOM, so it can be used to test other types of JavaScript, such
as for WebOS.
Jasmine
           ? Open-source JavaScript test
                framework from Pivotal

           ? BDD in the style of rspec
           ? Philosophy:
            ? Test JavaScript with JavaScript

                             @jasminebdd
                                                                                              41

That¡¯s where Jasmine comes in. Jasmine is Pivotal¡¯s open-source JavaScript testing
framework.

It lets you do ¡°rspec-style¡± testing.

The philosophy of Jasmine is that JavaScript should be tested with JavaScript. There are gems
out there such as harmony that let you test JavaScript with Ruby, but that just adds another
layer of indirection. JavaScript should be a ?rst-class language in web applications.

Also, it is not dependent on the DOM, so it can be used to test other types of JavaScript, such
as for WebOS.
Jasmine
           ? Open-source JavaScript test
                framework from Pivotal

           ? BDD in the style of rspec
           ? Philosophy:
            ? Test JavaScript with JavaScript
            ? No DOM dependency
                             @jasminebdd
                                                                                              41

That¡¯s where Jasmine comes in. Jasmine is Pivotal¡¯s open-source JavaScript testing
framework.

It lets you do ¡°rspec-style¡± testing.

The philosophy of Jasmine is that JavaScript should be tested with JavaScript. There are gems
out there such as harmony that let you test JavaScript with Ruby, but that just adds another
layer of indirection. JavaScript should be a ?rst-class language in web applications.

Also, it is not dependent on the DOM, so it can be used to test other types of JavaScript, such
as for WebOS.
gem install jasmine



                      42
43

When you install jasmine, it comes with a rake task that creates a javascripts directory under
spec.
44

I create a new ?le called project_form_spec.js. In this ?le I describe the behavior of the
functions in the RubyKaigi.ProjectForm class. Here¡¯s the spec for removeTask.
45

It starts with a ¡°describe¡±, like rspec, and the describe contains an ¡°it¡± block. Both the
describe and the it take a string describing the behavior you¡¯re expecting.

Jasmine comes with a small set of matchers - here you see expect().toEqual(). You can also
write custom matchers, as in rspec.
46

It comes with a little server built in, that starts on port 8888 by default, where you can run
your specs in a browser.

To re-run them, just reload the page.

Jasmine also comes with a continuous integration task that runs the specs in a browser and
uses selenium to determine whether they pass or fail. There are plug-ins that run your specs
headlessly.
Summary


? Respect your JavaScript!
? Test-drive


                             47
http://www.?ickr.com/photos/zachklein/54389823
                                                 48
http://www.?ickr.com/photos/rappensuncle/248625025
                                                     49
Questions?
      Ù|†–¤¬¤¢¤ê¤Þ¤¹¤«¡£
                   @jasminebdd

                @sarahmei
           sarah@pivotallabs.com

              http://www.?ickr.com/photos/rappensuncle/248625025
                                                                   50

¤¸¤ã¡¢¤³¤ì¤Ï?¤¤¤¸¤ç¤¦?¤Ç¤¹¡£

More Related Content

Similar to Feels Like Ruby - Ruby Kaigi 2010 (20)

Wed Development on Rails
Wed Development on RailsWed Development on Rails
Wed Development on Rails
James Gray
?
What rails taught me ¨C Eugene Pirogov
What rails taught me ¨C Eugene PirogovWhat rails taught me ¨C Eugene Pirogov
What rails taught me ¨C Eugene Pirogov
Ruby Meditation
?
Bitter Java, Sweeten with JRuby
Bitter Java, Sweeten with JRubyBitter Java, Sweeten with JRuby
Bitter Java, Sweeten with JRuby
Brian Sam-Bodden
?
Loading... Ruby on Rails 3
Loading... Ruby on Rails 3Loading... Ruby on Rails 3
Loading... Ruby on Rails 3
Rafael Garc¨ªa
?
Languages used by web app development services remotestac x
Languages used by web app development services  remotestac xLanguages used by web app development services  remotestac x
Languages used by web app development services remotestac x
Remote Stacx
?
Intro To Ror
Intro To RorIntro To Ror
Intro To Ror
myuser
?
Javascript beginner-handbook
Javascript beginner-handbookJavascript beginner-handbook
Javascript beginner-handbook
Faina Fridman
?
javascript-beginner-handbook.pdf
javascript-beginner-handbook.pdfjavascript-beginner-handbook.pdf
javascript-beginner-handbook.pdf
RaviKumar76265
?
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
ousli07
?
Ruby and rails around the web fun, informative sites for new and experienced...
Ruby and rails around the web  fun, informative sites for new and experienced...Ruby and rails around the web  fun, informative sites for new and experienced...
Ruby and rails around the web fun, informative sites for new and experienced...
Durga Prasad Tumu
?
?vod do Ruby on Rails
?vod do Ruby on Rails?vod do Ruby on Rails
?vod do Ruby on Rails
Karel Minarik
?
Exploring Ruby on Rails and PostgreSQL
Exploring Ruby on Rails and PostgreSQLExploring Ruby on Rails and PostgreSQL
Exploring Ruby on Rails and PostgreSQL
Barry Jones
?
Ruby on Rails: a brief introduction
Ruby on Rails: a brief introductionRuby on Rails: a brief introduction
Ruby on Rails: a brief introduction
Luigi De Russis
?
There Is No JavaScript
There Is No JavaScriptThere Is No JavaScript
There Is No JavaScript
Noam Kfir
?
Noam Kfir - There is no Java Script - code.talks 2015
Noam Kfir - There is no Java Script - code.talks 2015Noam Kfir - There is no Java Script - code.talks 2015
Noam Kfir - There is no Java Script - code.talks 2015
AboutYouGmbH
?
Merb Camp Keynote
Merb Camp KeynoteMerb Camp Keynote
Merb Camp Keynote
Yehuda Katz
?
Adventures of java developer in ruby world
Adventures of java developer in ruby worldAdventures of java developer in ruby world
Adventures of java developer in ruby world
Orest Ivasiv
?
Introduction to Ruby & Modern Programming
Introduction to Ruby & Modern ProgrammingIntroduction to Ruby & Modern Programming
Introduction to Ruby & Modern Programming
Christos Sotirelis
?
JRuby - The Perfect Alternative
JRuby - The Perfect AlternativeJRuby - The Perfect Alternative
JRuby - The Perfect Alternative
Ram Vijapurapu
?
Plataforma java
Plataforma javaPlataforma java
Plataforma java
Andre Fonseca
?
Wed Development on Rails
Wed Development on RailsWed Development on Rails
Wed Development on Rails
James Gray
?
What rails taught me ¨C Eugene Pirogov
What rails taught me ¨C Eugene PirogovWhat rails taught me ¨C Eugene Pirogov
What rails taught me ¨C Eugene Pirogov
Ruby Meditation
?
Bitter Java, Sweeten with JRuby
Bitter Java, Sweeten with JRubyBitter Java, Sweeten with JRuby
Bitter Java, Sweeten with JRuby
Brian Sam-Bodden
?
Languages used by web app development services remotestac x
Languages used by web app development services  remotestac xLanguages used by web app development services  remotestac x
Languages used by web app development services remotestac x
Remote Stacx
?
Intro To Ror
Intro To RorIntro To Ror
Intro To Ror
myuser
?
Javascript beginner-handbook
Javascript beginner-handbookJavascript beginner-handbook
Javascript beginner-handbook
Faina Fridman
?
javascript-beginner-handbook.pdf
javascript-beginner-handbook.pdfjavascript-beginner-handbook.pdf
javascript-beginner-handbook.pdf
RaviKumar76265
?
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
ousli07
?
Ruby and rails around the web fun, informative sites for new and experienced...
Ruby and rails around the web  fun, informative sites for new and experienced...Ruby and rails around the web  fun, informative sites for new and experienced...
Ruby and rails around the web fun, informative sites for new and experienced...
Durga Prasad Tumu
?
Exploring Ruby on Rails and PostgreSQL
Exploring Ruby on Rails and PostgreSQLExploring Ruby on Rails and PostgreSQL
Exploring Ruby on Rails and PostgreSQL
Barry Jones
?
Ruby on Rails: a brief introduction
Ruby on Rails: a brief introductionRuby on Rails: a brief introduction
Ruby on Rails: a brief introduction
Luigi De Russis
?
There Is No JavaScript
There Is No JavaScriptThere Is No JavaScript
There Is No JavaScript
Noam Kfir
?
Noam Kfir - There is no Java Script - code.talks 2015
Noam Kfir - There is no Java Script - code.talks 2015Noam Kfir - There is no Java Script - code.talks 2015
Noam Kfir - There is no Java Script - code.talks 2015
AboutYouGmbH
?
Adventures of java developer in ruby world
Adventures of java developer in ruby worldAdventures of java developer in ruby world
Adventures of java developer in ruby world
Orest Ivasiv
?
Introduction to Ruby & Modern Programming
Introduction to Ruby & Modern ProgrammingIntroduction to Ruby & Modern Programming
Introduction to Ruby & Modern Programming
Christos Sotirelis
?
JRuby - The Perfect Alternative
JRuby - The Perfect AlternativeJRuby - The Perfect Alternative
JRuby - The Perfect Alternative
Ram Vijapurapu
?

Recently uploaded (20)

UiPath Community Dubai: Discover Unified Apps
UiPath Community Dubai: Discover Unified AppsUiPath Community Dubai: Discover Unified Apps
UiPath Community Dubai: Discover Unified Apps
UiPathCommunity
?
Cybersecurity-Threat-Landscape-March-31-April-7-2025.pdf
Cybersecurity-Threat-Landscape-March-31-April-7-2025.pdfCybersecurity-Threat-Landscape-March-31-April-7-2025.pdf
Cybersecurity-Threat-Landscape-March-31-April-7-2025.pdf
Joe Shenouda
?
CEE Software Development M&A Report 2025
CEE Software Development M&A Report 2025CEE Software Development M&A Report 2025
CEE Software Development M&A Report 2025
Yevgen Sysoyev
?
Automating Behavior-Driven Development: Boosting Productivity with Template-D...
Automating Behavior-Driven Development: Boosting Productivity with Template-D...Automating Behavior-Driven Development: Boosting Productivity with Template-D...
Automating Behavior-Driven Development: Boosting Productivity with Template-D...
DOCOMO Innovations, Inc.
?
Ricardo Jebb Bruno - A Structural CAD Technician
Ricardo Jebb Bruno - A Structural CAD TechnicianRicardo Jebb Bruno - A Structural CAD Technician
Ricardo Jebb Bruno - A Structural CAD Technician
Ricardo Jebb Bruno
?
Build Your Uber Clone App with Advanced Features
Build Your Uber Clone App with Advanced FeaturesBuild Your Uber Clone App with Advanced Features
Build Your Uber Clone App with Advanced Features
V3cube
?
Cloudflare¡¯s Game-Changing Move The First Remote MCP Server for AI Agent Deve...
Cloudflare¡¯s Game-Changing Move The First Remote MCP Server for AI Agent Deve...Cloudflare¡¯s Game-Changing Move The First Remote MCP Server for AI Agent Deve...
Cloudflare¡¯s Game-Changing Move The First Remote MCP Server for AI Agent Deve...
davidandersonofficia
?
Human Centered Design By Gnanasambandham
Human Centered Design By GnanasambandhamHuman Centered Design By Gnanasambandham
Human Centered Design By Gnanasambandham
Gnanasambandham Anbazhagan CSP, CSM, CSPO
?
Presentation Session 4 -Agent Builder.pdf
Presentation Session 4 -Agent Builder.pdfPresentation Session 4 -Agent Builder.pdf
Presentation Session 4 -Agent Builder.pdf
Mukesh Kala
?
SAP Automation with UiPath: Top 10 Use Cases Across FI/MM/SD/Basis/PP Modules...
SAP Automation with UiPath: Top 10 Use Cases Across FI/MM/SD/Basis/PP Modules...SAP Automation with UiPath: Top 10 Use Cases Across FI/MM/SD/Basis/PP Modules...
SAP Automation with UiPath: Top 10 Use Cases Across FI/MM/SD/Basis/PP Modules...
DianaGray10
?
BrightonSEO April 2025 - hreflang XML E-Commerce - Nick Samuel.pdf
BrightonSEO April 2025 - hreflang XML E-Commerce - Nick Samuel.pdfBrightonSEO April 2025 - hreflang XML E-Commerce - Nick Samuel.pdf
BrightonSEO April 2025 - hreflang XML E-Commerce - Nick Samuel.pdf
Nick Samuel
?
New from BookNet Canada for 2025: BNC SalesData and BNC LibraryData
New from BookNet Canada for 2025: BNC SalesData and BNC LibraryDataNew from BookNet Canada for 2025: BNC SalesData and BNC LibraryData
New from BookNet Canada for 2025: BNC SalesData and BNC LibraryData
BookNet Canada
?
From SBOMs to xBOMs to Transparency - Pavel Shukhman at OWASP Ottawa on 2025-...
From SBOMs to xBOMs to Transparency - Pavel Shukhman at OWASP Ottawa on 2025-...From SBOMs to xBOMs to Transparency - Pavel Shukhman at OWASP Ottawa on 2025-...
From SBOMs to xBOMs to Transparency - Pavel Shukhman at OWASP Ottawa on 2025-...
Pavel Shukhman
?
Codequiry: A Code Similarity Checker Every Developer Should Know
Codequiry: A Code Similarity Checker Every Developer Should KnowCodequiry: A Code Similarity Checker Every Developer Should Know
Codequiry: A Code Similarity Checker Every Developer Should Know
Code Quiry
?
Misc: The Internet Story - How Data Travels, Transit Works, and the Role of C...
Misc: The Internet Story - How Data Travels, Transit Works, and the Role of C...Misc: The Internet Story - How Data Travels, Transit Works, and the Role of C...
Misc: The Internet Story - How Data Travels, Transit Works, and the Role of C...
3G4G
?
How to Achieve High-Accuracy Results When Using LLMs
How to Achieve High-Accuracy Results When Using LLMsHow to Achieve High-Accuracy Results When Using LLMs
How to Achieve High-Accuracy Results When Using LLMs
Aggregage
?
ScotSecure Cyber Security Summit 2025 Edinburgh
ScotSecure Cyber Security Summit 2025 EdinburghScotSecure Cyber Security Summit 2025 Edinburgh
ScotSecure Cyber Security Summit 2025 Edinburgh
Ray Bugg
?
Build With AI X GDG Harare Beginners .pdf
Build With AI X GDG Harare Beginners .pdfBuild With AI X GDG Harare Beginners .pdf
Build With AI X GDG Harare Beginners .pdf
Google Developer Group - Harare
?
2025-04-05 - Block71 Event - The Landscape of GenAI and Ecosystem.pdf
2025-04-05 - Block71 Event - The Landscape of GenAI and Ecosystem.pdf2025-04-05 - Block71 Event - The Landscape of GenAI and Ecosystem.pdf
2025-04-05 - Block71 Event - The Landscape of GenAI and Ecosystem.pdf
Ivan Tang
?
TNBC Research Presentation and medical virology .pptx
TNBC Research Presentation and medical virology .pptxTNBC Research Presentation and medical virology .pptx
TNBC Research Presentation and medical virology .pptx
MohamedHasan816582
?
UiPath Community Dubai: Discover Unified Apps
UiPath Community Dubai: Discover Unified AppsUiPath Community Dubai: Discover Unified Apps
UiPath Community Dubai: Discover Unified Apps
UiPathCommunity
?
Cybersecurity-Threat-Landscape-March-31-April-7-2025.pdf
Cybersecurity-Threat-Landscape-March-31-April-7-2025.pdfCybersecurity-Threat-Landscape-March-31-April-7-2025.pdf
Cybersecurity-Threat-Landscape-March-31-April-7-2025.pdf
Joe Shenouda
?
CEE Software Development M&A Report 2025
CEE Software Development M&A Report 2025CEE Software Development M&A Report 2025
CEE Software Development M&A Report 2025
Yevgen Sysoyev
?
Automating Behavior-Driven Development: Boosting Productivity with Template-D...
Automating Behavior-Driven Development: Boosting Productivity with Template-D...Automating Behavior-Driven Development: Boosting Productivity with Template-D...
Automating Behavior-Driven Development: Boosting Productivity with Template-D...
DOCOMO Innovations, Inc.
?
Ricardo Jebb Bruno - A Structural CAD Technician
Ricardo Jebb Bruno - A Structural CAD TechnicianRicardo Jebb Bruno - A Structural CAD Technician
Ricardo Jebb Bruno - A Structural CAD Technician
Ricardo Jebb Bruno
?
Build Your Uber Clone App with Advanced Features
Build Your Uber Clone App with Advanced FeaturesBuild Your Uber Clone App with Advanced Features
Build Your Uber Clone App with Advanced Features
V3cube
?
Cloudflare¡¯s Game-Changing Move The First Remote MCP Server for AI Agent Deve...
Cloudflare¡¯s Game-Changing Move The First Remote MCP Server for AI Agent Deve...Cloudflare¡¯s Game-Changing Move The First Remote MCP Server for AI Agent Deve...
Cloudflare¡¯s Game-Changing Move The First Remote MCP Server for AI Agent Deve...
davidandersonofficia
?
Presentation Session 4 -Agent Builder.pdf
Presentation Session 4 -Agent Builder.pdfPresentation Session 4 -Agent Builder.pdf
Presentation Session 4 -Agent Builder.pdf
Mukesh Kala
?
SAP Automation with UiPath: Top 10 Use Cases Across FI/MM/SD/Basis/PP Modules...
SAP Automation with UiPath: Top 10 Use Cases Across FI/MM/SD/Basis/PP Modules...SAP Automation with UiPath: Top 10 Use Cases Across FI/MM/SD/Basis/PP Modules...
SAP Automation with UiPath: Top 10 Use Cases Across FI/MM/SD/Basis/PP Modules...
DianaGray10
?
BrightonSEO April 2025 - hreflang XML E-Commerce - Nick Samuel.pdf
BrightonSEO April 2025 - hreflang XML E-Commerce - Nick Samuel.pdfBrightonSEO April 2025 - hreflang XML E-Commerce - Nick Samuel.pdf
BrightonSEO April 2025 - hreflang XML E-Commerce - Nick Samuel.pdf
Nick Samuel
?
New from BookNet Canada for 2025: BNC SalesData and BNC LibraryData
New from BookNet Canada for 2025: BNC SalesData and BNC LibraryDataNew from BookNet Canada for 2025: BNC SalesData and BNC LibraryData
New from BookNet Canada for 2025: BNC SalesData and BNC LibraryData
BookNet Canada
?
From SBOMs to xBOMs to Transparency - Pavel Shukhman at OWASP Ottawa on 2025-...
From SBOMs to xBOMs to Transparency - Pavel Shukhman at OWASP Ottawa on 2025-...From SBOMs to xBOMs to Transparency - Pavel Shukhman at OWASP Ottawa on 2025-...
From SBOMs to xBOMs to Transparency - Pavel Shukhman at OWASP Ottawa on 2025-...
Pavel Shukhman
?
Codequiry: A Code Similarity Checker Every Developer Should Know
Codequiry: A Code Similarity Checker Every Developer Should KnowCodequiry: A Code Similarity Checker Every Developer Should Know
Codequiry: A Code Similarity Checker Every Developer Should Know
Code Quiry
?
Misc: The Internet Story - How Data Travels, Transit Works, and the Role of C...
Misc: The Internet Story - How Data Travels, Transit Works, and the Role of C...Misc: The Internet Story - How Data Travels, Transit Works, and the Role of C...
Misc: The Internet Story - How Data Travels, Transit Works, and the Role of C...
3G4G
?
How to Achieve High-Accuracy Results When Using LLMs
How to Achieve High-Accuracy Results When Using LLMsHow to Achieve High-Accuracy Results When Using LLMs
How to Achieve High-Accuracy Results When Using LLMs
Aggregage
?
ScotSecure Cyber Security Summit 2025 Edinburgh
ScotSecure Cyber Security Summit 2025 EdinburghScotSecure Cyber Security Summit 2025 Edinburgh
ScotSecure Cyber Security Summit 2025 Edinburgh
Ray Bugg
?
2025-04-05 - Block71 Event - The Landscape of GenAI and Ecosystem.pdf
2025-04-05 - Block71 Event - The Landscape of GenAI and Ecosystem.pdf2025-04-05 - Block71 Event - The Landscape of GenAI and Ecosystem.pdf
2025-04-05 - Block71 Event - The Landscape of GenAI and Ecosystem.pdf
Ivan Tang
?
TNBC Research Presentation and medical virology .pptx
TNBC Research Presentation and medical virology .pptxTNBC Research Presentation and medical virology .pptx
TNBC Research Presentation and medical virology .pptx
MohamedHasan816582
?

Feels Like Ruby - Ruby Kaigi 2010

  • 1. Feels Like Ruby Sarah Mei Pivotal Labs @sarahmei sarah@pivotallabs.com 1 ¤¹¤ß¤Þ¤»¤ó©`¤Ï¤¸¤Þ¤ê¤Þ¤·¤ç¤¦¤«¡£
  • 2. ½Ô¤µ¤ó¡¢¤³¤ó¤Ë¤Á¤Ï Hi everybody 2 ¤ß¤ó¤Ê¤µ¤ó¡¢¤³¤ó¤Ë¤Á?¤Ï¡£
  • 3. ¥á¥¤ ¥µ¥é ÃûÇ°¤Ï MEI Sarah ¤Ç¤¹¡£ I¡¯m Sarah Mei. 3 ¤Ê¤Þ¤¨?¤Ï?Mei Sarah?¤Ç¤¹¡£¤Ï¤¸¤á¤Þ¤·¤Æ¡¢¤É¤¦¤¾¤¦?¤è¤í¤·¤¯?¤ª¤Í¤¬¤¤¤·¤Þ¤¹¡£
  • 4. ÈÕ±¾Ó郎¤è¤¯»°¤»¤Ê¤¤¤Ç¤¹¤«¤é¡¢ ¤¹¤ß¤Þ¤»¤ó¡£ I don¡¯t speak Japanese very well, so thanks in advance for your patience. 4 ¤Ë¤Û¤ó¤´?¤¬?¤è¤¯?¤Ï¤Ê¤»¤Ê¤¤?¤Ç¤¹?¤«¤é¡¢¤¹¤ß¤Þ¤»¤ó¡£
  • 5. http://www.?ickr.com/photos/sunrise/4064417/ 5 San Francisco¤Ë¤¹¤ó¤Ç¤¤¤Þ¤¹¡£
  • 6. ¥¢¥á¥ê¥«ÈˤǤ¹¡£ I¡¯m American. http://www.?ickr.com/photos/junewess/3756778580/ 6 America¤¸¤ó¤Ç¤¹¡£
  • 7. ¸é³Ü²ú²â¤È¸é²¹¾±±ô²õ¤Î¿ª°kÕߤǤ¹¡£ I¡¯m a Ruby & Rails developer. Image copyright 2006-2010 Yukihiro Matsumoto 7 Ruby?¤Î?¤«¤¤¤Ï¤Ä¤·¤ã?¤Ç¤¹¡£
  • 8. I work at ¥Ô¥ô¥©¥¿¥ë¥é¥Ö¥¹ ¤ËÇÚ¤á¤Æ¤¤¤Þ¤¹¡£ 8 Pivotal Labs?¤Ë?¤Ä¤È¤á¤Æ?¤¤¤Þ¤¹¡£Pivotal Labs?¤Ï?Pivotal Tracker?¤ò?¤»¤¤¤µ¤¯?¤·¤Æ?¤¤ ¤Þ¤¹¡£¤Ç¤â¡¢¤Û¤È¤ó¤É?¤Î?Pivotal Labs?¤Î?¤·¤ã¤¤¤ó?¤Ï?¥³¥ó¥½¥ë¥¿¥ó¥Ã¥È?¤Ç¤¹.?
  • 9. I work at ¥Ô¥ô¥©¥¿¥ë¥é¥Ö¥¹ ¤ËÇÚ¤á¤Æ¤¤¤Þ¤¹¡£ ¥Ô¥ô¥©¥¿¥ë¥È¥é¥Ã¥«©` 8 Pivotal Labs?¤Ë?¤Ä¤È¤á¤Æ?¤¤¤Þ¤¹¡£Pivotal Labs?¤Ï?Pivotal Tracker?¤ò?¤»¤¤¤µ¤¯?¤·¤Æ?¤¤ ¤Þ¤¹¡£¤Ç¤â¡¢¤Û¤È¤ó¤É?¤Î?Pivotal Labs?¤Î?¤·¤ã¤¤¤ó?¤Ï?¥³¥ó¥½¥ë¥¿¥ó¥Ã¥È?¤Ç¤¹.?
  • 10. £¨¤³¤ÎÏȤ«¤éÓ¢Óï¤Ç¤¹¡£¤´¤á¤ó¤Í£© 9 ¤¸¤³¤·¤ç¤¦¤«¤¤?¤¬?¤ª¤ï¤ê?¤Ç¤¹?¤«¤é¡¢¤³¤Î?¤µ¤­?¤«¤é?Ó¢ÕZ?¤Ç¤¹¡£¤´¤á¤ó?¤Í¤¨¡£¡£¡£
  • 11. £¨¤³¤ÎÏȤ«¤éÓ¢Óï¤Ç¤¹¡£¤´¤á¤ó¤Í£© Feels Like Ruby 9 ¤¸¤³¤·¤ç¤¦¤«¤¤?¤¬?¤ª¤ï¤ê?¤Ç¤¹?¤«¤é¡¢¤³¤Î?¤µ¤­?¤«¤é?Ó¢ÕZ?¤Ç¤¹¡£¤´¤á¤ó?¤Í¤¨¡£¡£¡£
  • 12. £¨¤³¤ÎÏȤ«¤éÓ¢Óï¤Ç¤¹¡£¤´¤á¤ó¤Í£© Feels Like Ruby Making JavaScript a Real Language 9 ¤¸¤³¤·¤ç¤¦¤«¤¤?¤¬?¤ª¤ï¤ê?¤Ç¤¹?¤«¤é¡¢¤³¤Î?¤µ¤­?¤«¤é?Ó¢ÕZ?¤Ç¤¹¡£¤´¤á¤ó?¤Í¤¨¡£¡£¡£
  • 13. What I like about Ruby 10 I want to set the stage by talking about what I enjoy about Ruby. First and foremost, it¡¯s the language itself. I love the expressiveness and accessibility. But as a working developer, what I appreciate the most is the ability to test-drive everything. Between rspec, test::unit, cucumber, capybara, webrat, shoulda, steak....I can pick the most appropriate test tools for each project.
  • 14. What I like about Ruby ? The language itself. 10 I want to set the stage by talking about what I enjoy about Ruby. First and foremost, it¡¯s the language itself. I love the expressiveness and accessibility. But as a working developer, what I appreciate the most is the ability to test-drive everything. Between rspec, test::unit, cucumber, capybara, webrat, shoulda, steak....I can pick the most appropriate test tools for each project.
  • 15. What I like about Ruby ? The language itself. ? I can test-drive everything. 10 I want to set the stage by talking about what I enjoy about Ruby. First and foremost, it¡¯s the language itself. I love the expressiveness and accessibility. But as a working developer, what I appreciate the most is the ability to test-drive everything. Between rspec, test::unit, cucumber, capybara, webrat, shoulda, steak....I can pick the most appropriate test tools for each project.
  • 16. What I like about Ruby ? The language itself. JavaScript ? I can test-drive everything. JavaScript 11 So let¡¯s see how these options stack up in JavaScript. The language itself...?
  • 17. What I like about Ruby ? The language itself. X JavaScript ? I can test-drive everything. JavaScript 12 Well, I¡¯m never going to be able to change that. I don¡¯t dislike JavaScript, but Ruby ?ts my personality better. But what about test-driving?
  • 18. What I like about Ruby ? The language itself. X JavaScript O ? I can test-drive everything. JavaScript 13 We should be able to test-drive JavaScript. But until earlier this year, I didn¡¯t. And some of the best Rails developers I know, who test-drive all their Ruby code, still don¡¯t test-drive their JavaScript. So the question is...
  • 19. http://www.?ickr.com/photos/petereed/496392956 14 Why not? Why do we make JavaScript development so painful? I think part of it is a philosophical mismatch in the way Rails treats JavaScript.
  • 20. 15 Web applications are fundamentally written in multiple languages. Here¡¯s a very generic representation of a Ruby web application. On the front end, there¡¯s HTML, CSS, and Javascript, and on the back end there is one (or more!) server languages, plus SQL. And of course, normal applications have other front-end languages too, like ActionScript or Objective-J, and other back-end languages interacting with Ruby, and usually additional data stores as well. I can¡¯t remember the last time I worked on an application that had ONLY a relational database for storage. But traditionally, in this setup, programming languages sit here in the middle. But frameworks try to extend on either side. That¡¯s what Rails does.
  • 21. ActiveRecord Rails 16 For example, Rails extends this direction, and abstracts away SQL with ActiveRecord. It¡¯s pretty successful with that. Even on really complex applications, I can get away with the generated SQL most of the time.
  • 22. erb sass rjs ActiveRecord Rails/gems Rails 17 But it also extends the way with erb and rjs to generate HTML and JavaScript. And with other gems, you can generate CSS too. And although this was a good idea with SQL and ActiveRecord, and it works decently well with HTML and CSS, RJS is kind of a disaster. I¡¯ll show you an example in moment. But ?rst I want to point out that this is not a failing of Rails, or of the people who wrote it. This is the nature of Javascript. It might be the one language whose use is evolving even faster than Ruby. SQL has an ISO standard and a committee, and if they make any signi?cant changes, we¡¯ll have ?ve years¡¯ notice. Moreover, the database products that implement SQL have committees, so as SQL end-users, we¡¯re exceedingly well-insulated from any language churn.
  • 23. JavaScript Developers http://www.?ickr.com/photos/gem66/387400306 18 But Javascript...is the wild west. It¡¯s the frontier. While the language itself is relatively stable, its libraries and usage patterns are changing faster than that of Ruby, or of Rails, or of any other framework. What does that mean for us as web application developers, as multi-lingual programmers?
  • 24. erb sass rjs ActiveRecord Rails/gems Rails 19 It means we need to opt-out, to make an exception, and to handle JavaScript differently. Now, that¡¯s the philosophical reason - now I want to show you the practical reason.
  • 25. 20 In Rails 2, using RJS means you have little bits of Javascript all over your code. Here¡¯s an example I adapted from the ¡°complex forms¡± railscast. This is a to-do list app, in which you can create a project with any number of tasks attached. This is the new project form. It has 3 task text?elds, but you can add another one by clicking ¡°Add a task¡±, and you can delete one by clicking the ¡°remove¡± link that¡¯s next to it. Both of these are implemented as simple JavaScript that modi?ed the DOM. For the purposes of this example, we¡¯re going to focus on the remove link. So let¡¯s look at how that¡¯s implemented.
  • 26. 21 This is the partial that is rendered for each task. The important part is the link_to_function, where when we click remove, it calls that little piece of javascript.
  • 27. 22 Here¡¯s what the rendered HTML looks like. You¡¯ve got that little bit of inline javascript, which on its own, is fairly simple. But it¡¯s not really testable. Sure, you could write a selenium test that tests that it actually removes the dom element. However, Selenium is slow. This is one simple interaction - in a typical modern web application, there¡¯s likely to be dozens of these on one page, if not hundreds. Testing them all with Selenium would mean you¡¯d have a test suite that never stopped running. Plus, Selenium is an integration test. If all you¡¯re doing is integration tests, you¡¯re doing it wrong. You need both unit tests and integration tests to probe all the behavior of your code. So how can we re-do this in a way that is testable and repeatable?
  • 28. A different approach 23 1. Forget RJS - it gets in your way once you do anything beyond the very simple. 2. Put your JS in classes - of course JS is a prototype-based language instead of an inheritance-based language, but you can still organize your functions into sets. I¡¯ll show you what that looks like in a moment. 3. Organize your JS by behavior, and by location. 4. Test-drive all your JS.
  • 29. A different approach ? Forget RJS - write functions 23 1. Forget RJS - it gets in your way once you do anything beyond the very simple. 2. Put your JS in classes - of course JS is a prototype-based language instead of an inheritance-based language, but you can still organize your functions into sets. I¡¯ll show you what that looks like in a moment. 3. Organize your JS by behavior, and by location. 4. Test-drive all your JS.
  • 30. A different approach ? Forget RJS - write functions ? Put your functions in classes 23 1. Forget RJS - it gets in your way once you do anything beyond the very simple. 2. Put your JS in classes - of course JS is a prototype-based language instead of an inheritance-based language, but you can still organize your functions into sets. I¡¯ll show you what that looks like in a moment. 3. Organize your JS by behavior, and by location. 4. Test-drive all your JS.
  • 31. A different approach ? Forget RJS - write functions ? Put your functions in classes ? Organize classes by behavior and location 23 1. Forget RJS - it gets in your way once you do anything beyond the very simple. 2. Put your JS in classes - of course JS is a prototype-based language instead of an inheritance-based language, but you can still organize your functions into sets. I¡¯ll show you what that looks like in a moment. 3. Organize your JS by behavior, and by location. 4. Test-drive all your JS.
  • 32. A different approach ? Forget RJS - write functions ? Put your functions in classes ? Organize classes by behavior and location ? Test-drive your classes. 23 1. Forget RJS - it gets in your way once you do anything beyond the very simple. 2. Put your JS in classes - of course JS is a prototype-based language instead of an inheritance-based language, but you can still organize your functions into sets. I¡¯ll show you what that looks like in a moment. 3. Organize your JS by behavior, and by location. 4. Test-drive all your JS.
  • 33. A different approach Unobtrusive JavaScript ? Forget RJS - write functions ? Put your functions in classes ? Organize classes by behavior and location ? Test-drive your classes. 24 These ?rst two techniques are collectively known as ¡°unobtrusive JavaScript.¡± I am happy to report that Rails 3 is moving to unobtrusive JavaScript. So Rails 3 will help this problem quite a bit, but, as you can see, there is more to do beyond ¡°unobtrusiveizing.¡± Let¡¯s look at how we¡¯d do it.
  • 34. 25 Here¡¯s our original example, with the remove link. How we could re-implement it? Before, the code looked like this...
  • 35. 26 But we can simplify this now. All we really have here is a link that we want to add a behavior to.
  • 36. 27 So in the erb template, we¡¯ll just make a link with a class on it. No reference to JavaScript anywhere here. And here is...
  • 37. 28 ...the HTML it generates. So. If we don¡¯t put our Javascript in the HTML...where should we put it?
  • 38. public/javascripts ! 29 In general, I keep my JavaScript ?les in public/javascripts. So we¡¯ll create a ?le in public/ javascripts called project_form.js
  • 39. project_form.js 30 In this ?le, we create a class called RubyKaigi.ProjectForm, and we put in two functions. The two functions are initialize and remove_task.
  • 40. project_form.js 31 In initialize, we add a click event to all links with class .remove-link. When that link is clicked, our other function is called.
  • 41. project_form.js 32 That function, remove_task, actually removes the element with class ¡®task¡¯ from the DOM.
  • 42. project_form.js 33 The last thing that¡¯s important here is the document.ready, which calls initialize, which sets up the click events, once the page is loaded.
  • 43. 34 So I¡¯ve created a JavaScript class that encapsulates the behavior of the project form. Once I implement the ¡°Add a task¡± link, that behavior will go in the same class.
  • 44. 35 Then it might look something like this, with a new function at the bottom, and some extra initialization at the top.
  • 45. Organizing by page What if you use the ProjectForm JS on more than one page? 36
  • 46. Organizing by page What if you use the ProjectForm JS on more than one page? Move ProjectForm.initialize to a page class initializer 36
  • 47. 37
  • 50. http://www.?ickr.com/photos/uwehermann/132244826 40 So that¡¯s how you organize your JavaScript so it¡¯s all in one place. But what about testing?
  • 51. Jasmine @jasminebdd 41 That¡¯s where Jasmine comes in. Jasmine is Pivotal¡¯s open-source JavaScript testing framework. It lets you do ¡°rspec-style¡± testing. The philosophy of Jasmine is that JavaScript should be tested with JavaScript. There are gems out there such as harmony that let you test JavaScript with Ruby, but that just adds another layer of indirection. JavaScript should be a ?rst-class language in web applications. Also, it is not dependent on the DOM, so it can be used to test other types of JavaScript, such as for WebOS.
  • 52. Jasmine ? Open-source JavaScript test framework from Pivotal @jasminebdd 41 That¡¯s where Jasmine comes in. Jasmine is Pivotal¡¯s open-source JavaScript testing framework. It lets you do ¡°rspec-style¡± testing. The philosophy of Jasmine is that JavaScript should be tested with JavaScript. There are gems out there such as harmony that let you test JavaScript with Ruby, but that just adds another layer of indirection. JavaScript should be a ?rst-class language in web applications. Also, it is not dependent on the DOM, so it can be used to test other types of JavaScript, such as for WebOS.
  • 53. Jasmine ? Open-source JavaScript test framework from Pivotal ? BDD in the style of rspec @jasminebdd 41 That¡¯s where Jasmine comes in. Jasmine is Pivotal¡¯s open-source JavaScript testing framework. It lets you do ¡°rspec-style¡± testing. The philosophy of Jasmine is that JavaScript should be tested with JavaScript. There are gems out there such as harmony that let you test JavaScript with Ruby, but that just adds another layer of indirection. JavaScript should be a ?rst-class language in web applications. Also, it is not dependent on the DOM, so it can be used to test other types of JavaScript, such as for WebOS.
  • 54. Jasmine ? Open-source JavaScript test framework from Pivotal ? BDD in the style of rspec ? Philosophy: @jasminebdd 41 That¡¯s where Jasmine comes in. Jasmine is Pivotal¡¯s open-source JavaScript testing framework. It lets you do ¡°rspec-style¡± testing. The philosophy of Jasmine is that JavaScript should be tested with JavaScript. There are gems out there such as harmony that let you test JavaScript with Ruby, but that just adds another layer of indirection. JavaScript should be a ?rst-class language in web applications. Also, it is not dependent on the DOM, so it can be used to test other types of JavaScript, such as for WebOS.
  • 55. Jasmine ? Open-source JavaScript test framework from Pivotal ? BDD in the style of rspec ? Philosophy: ? Test JavaScript with JavaScript @jasminebdd 41 That¡¯s where Jasmine comes in. Jasmine is Pivotal¡¯s open-source JavaScript testing framework. It lets you do ¡°rspec-style¡± testing. The philosophy of Jasmine is that JavaScript should be tested with JavaScript. There are gems out there such as harmony that let you test JavaScript with Ruby, but that just adds another layer of indirection. JavaScript should be a ?rst-class language in web applications. Also, it is not dependent on the DOM, so it can be used to test other types of JavaScript, such as for WebOS.
  • 56. Jasmine ? Open-source JavaScript test framework from Pivotal ? BDD in the style of rspec ? Philosophy: ? Test JavaScript with JavaScript ? No DOM dependency @jasminebdd 41 That¡¯s where Jasmine comes in. Jasmine is Pivotal¡¯s open-source JavaScript testing framework. It lets you do ¡°rspec-style¡± testing. The philosophy of Jasmine is that JavaScript should be tested with JavaScript. There are gems out there such as harmony that let you test JavaScript with Ruby, but that just adds another layer of indirection. JavaScript should be a ?rst-class language in web applications. Also, it is not dependent on the DOM, so it can be used to test other types of JavaScript, such as for WebOS.
  • 58. 43 When you install jasmine, it comes with a rake task that creates a javascripts directory under spec.
  • 59. 44 I create a new ?le called project_form_spec.js. In this ?le I describe the behavior of the functions in the RubyKaigi.ProjectForm class. Here¡¯s the spec for removeTask.
  • 60. 45 It starts with a ¡°describe¡±, like rspec, and the describe contains an ¡°it¡± block. Both the describe and the it take a string describing the behavior you¡¯re expecting. Jasmine comes with a small set of matchers - here you see expect().toEqual(). You can also write custom matchers, as in rspec.
  • 61. 46 It comes with a little server built in, that starts on port 8888 by default, where you can run your specs in a browser. To re-run them, just reload the page. Jasmine also comes with a continuous integration task that runs the specs in a browser and uses selenium to determine whether they pass or fail. There are plug-ins that run your specs headlessly.
  • 62. Summary ? Respect your JavaScript! ? Test-drive 47
  • 65. Questions? Ù|†–¤¬¤¢¤ê¤Þ¤¹¤«¡£ @jasminebdd @sarahmei sarah@pivotallabs.com http://www.?ickr.com/photos/rappensuncle/248625025 50 ¤¸¤ã¡¢¤³¤ì¤Ï?¤¤¤¸¤ç¤¦?¤Ç¤¹¡£