This screencast mostly shows how to transition from JavaScript to CoffeeScript. However, CoffeeScript adds a ton of really helpful constructs beyond that which JS provides, cleanly and easily.
The thing I've found best about it is that it brings language features that I use all the time in Ruby (like unless, string interpolation using #{}, improved looping, etc.) ...Mix that all in with the fact that it's more readable and uses fewer characters, well, you have a giant win.
We live in a world where web development requires some fluency in multiple languages: HTML, CSS, JavaScript, Ruby, YAML. Providing ways to simplify (SCSS being identical to CSS, but adding new abilities that are easy to pick up and understand, for example) is key to speeding up development. CoffeeScript takes some getting used to, sure, but in the longer term it'll make more readable, easier to maintain and more enjoyable to write code.
As a side note Ryan, I've noticed our avatars are being cached here at Railscasts. I updated my account at Github months ago and I'm seeing old info here, is there a way we can force a refresh on our profile here at Railscasts?
I know I can edit my profile, but I can't change my Github account.
Thanks Ryan for a great intro to CoffeeScript - I've been eagerly anticipating an episode on this this.
And thank you to Trevor Burnham for your comments. I can see now why I should learn CoffeeScript. It didn't quite click that I'd be moving any syntax errors into compile time until I read your comment. That's a big plus in my opinion. I look forward to buying and reading your book.
As a side note, I've just taken a fresh look at HAML because I started playing around with ActiveAdmin. I must say I really must have been in a bad mood the last time I looked at it because now it really does seem appealing. I will have to play with it a little more too I think (even though it's not a default in the Rails stack)!
If nothing else Rails is teaching me to be more open-minded.
Hello guys,
I'm new in RoR and these screencasts are very very nice, thx at Ryan!!!
I saw a screencast about Jasmine and i want to use it. It's a cool GEM to test the JavaScript, but can i use CoffeeScript?
Well, having to go through a compile step is obviously a downside, though Rails makes this very transparent. (And in my experience, many of the things that you get as compile-time errors in CoffeeScript would have been run-time errors in JavaScript, where your typo would have gone unnoticed until you click a particular button... as I say in my book, "The compiler's here to help.")
Perhaps the biggest downside is that your code will become obsolete as new CoffeeScript versions are released. This is, of course, true of almost any language (see: Ruby), but if you're writing pure JavaScript, you can be sure that browsers will continue to run it for as long as the web as we know it exists. CoffeeScript has seen few changes since 1.0 came out in December (1.1.0/1.1.1 mostly just fixed bugs and improved the REPL), but it'll continue to evolve in the future, and that evolution will inevitably break backward compatibility.
Still, being able to write code more quickly and read it more easily is such a huge win that I wouldn't hesitate to use it on almost any project.
It's funny, this is the number one argument for justifying the change. However, if you are a working developer and work on different projects, chances are you will HAVE to learn it if the majority of apps use it.
If you look at most rails apps <= 2.3.x that are currently in production, they most certainly use rails standards of the time. e.g. rails 2.3.x apps use prototype. So, I would say just learn it. You may find you like it...
If you dont want a framework and community with a gold standard you are free to go back to Java or PHP. Like it or not, Rails came up HUGE (and drug Ruby along with it) because DHH, and later the core teams decided on opinionated software. The Golden Path is what separates good rails code from bad more times then not.
For instance, you can jump around just about any auth plugin or roll your own. But if you dont include common idioms for rails authentication it will be unusable by most coders or gems. Do I think current_user is the best helper method name for this? Maybe, maybe not. But I am not going to start rolling my auth modules with anything else. Thank god for Golden Paths.
Oh, and if you are still writing pure javascript I have high doubts that you are very efficient.
Ryan, you were at my talk at RailsConf, right? Thanks for finally giving the overview everyone wanted me to provide. :)
As to the discussion of whether learning/using CoffeeScript is worth it: Check out some of the blurbs on my CoffeeScript book, including one from Brendan Eich, the creator of JavaScript (who was kind enough to review a draft and even offered a few corrections).
Yes, there are downsides to using CoffeeScript rather than JavaScript, but the benefits
Who says you need to use Jquery? or Coffeescript? or Haml? or Sass? or Git? or Activerecord? Or ...?
Rails is modular, in fact, I think it is one of the most modular frameworks out there.
Yes, rails comes "packaged" (as in, it has a few extra lines in the Gemfile) with some defaults, it's an opinionated framework, and that's one of the reasons why it's gotten so big so fast, but all that does is make it easier for beginners to jump in and start coding.
Is it really that hard for you to type a few extra characters when you create a new app?
Rails new myapp = Jquery, Cofeescript, Sass, Erb, Activerecord, Test Unit, Git
Rails new myapp -OGJT = ... none of the above, go figure!
So all assets get packaged together and sent in one file? I really hope that I didn't understand that correctly.
Let's say they all pages share the jQuery library, linked directly from Google. Normally the jQuery library would be cached and not downloaded on each page. In fact, if downloaded from Google for another site, it wouldn't be pulled for my site at all. And for any one page, only the JS necessary for that page would be downloaded (never all of it at once). Plus with separate asset files, aren't they downloaded in parallel? And if I update JS on one page, only that updated JS would get downloaded when viewing that page in Rails 3.0, but in 3.1 it seems like ALL of my JS gets pulled down again when any one JS file gets updated.
Peepcode also has a screencast on Coffeescript for those wishing to see more in-depth examples of Coffeescript usage.
Thanks Ryan for another great screencast. I've already cleaned up a bunch of my javascript and my JS files just feel so much more manageable and readable now.
@Dmitri @Nicolas: Firstly, I find that learning a new syntax happens with very little effort. For CS, I read a few blog posts upon encountering it and this took perhaps an hour. Then I learn the syntax as I use it and this means I might occasionally have to look up the reference when I get a compile error and can't work out what I have done wrong. Initially this does add some time to development but only a very minimal 5 minutes here and there. Consequently, I don't buy the argument that the difficulty in learning CS is so great, it overwhelms any benefits.
Secondly, I see no reason to treat myself as a human compiler. If I am repeating the same stuff again and again, it is a sign that something could be done programmatically. Unfortunately, this is not always viable in an interpreted language where speed is such an important factor. Additionally, it is difficult to update a language on the client side so where clear improvements are identified, they remain unusable to the developer. CS solves both of these problems. For example, see destructured assignment.
Thirdly, I don't like CS just because it is "prettier". I can take or leave the new function syntax for example. What I do like are the abstractions that clearly make sense and are more readable than the JS alternative. Default arguments, variable safety, splats and comprehensions are enough reason for me to use CS. However, I do like all of the more sugary stuff as well.
Well, yeah, peoples opinions vary about haml. In the beginning i needed to get used to it too, but now it so much easier code views than it is with html + erb. By far the biggest benefits to me are not having to close tags, and not having to use the messy <%= %>.
@Nicolas: You don't have to use any of this stuff. You still can use plain JS. You don't have to use git and you still can use Prototype+Scriptaculous. You are not walled in, just because the defaults are different.
i have no idea why everybody is crying because of coffeescript.
thats 1 !!! line you have to comment out when starting a project in rails 3.1 if you don't want to use it.
@Nicolas: Well, its pretty simple, its nicer and cleaner to use... And you really don't have to use it if you don't want to. Well, and yes, there are choices made by the core rails developers... But Rails is an open project afaik, so you can contribute to those choices if you want. And if you don, fine, but then you might need to find some other tools to use if Rails doesn't suit you...
Dmitri: I can't understand either even after a look to this screencast. It just shows you can strip off "function" and things like that but do we need that ?
@Dmitri: JS is not bad at all, no, Coffe Script just makes it easier to write js code. I like it. Same goes for me with html and haml. I stopped using html in favour of haml, as its just cleaner and easier to write.
Ryan, it says 3 Comments, but there are 4 already...
In a very fast way you can use guard.
Then your coffeescript compiled in javascript every time you save a .coffee
I m not sure this is the perfect way but it's work and it's easy to implement in rails 3.0.x
Following the tutorial, and it's great. I have refactored quite a bit of code and made several adjustments for clarity sake. I am having one issue though, and it is regarding using any OpenID::Store::Filesystem based provider (google_apps, open_id). It seems that you cannot 'attach' them to an existing account, and they always want to create a new account even if you are logged in. Is this a known issue, and is there a work around?
In the cases where a server-side validation rule would not work on the client (i.e. conditional callbacks like :if, :unless) then do not attempt client side validations. Fall back to the server side validation.
At least this sheds some time on the conditional callback :unless. Any thoughts as to why :message => ... isn't supported?
I managed to get logins using Twitter, Facebook, Github, and LinkedIn working on a toy app (Ruby 1.9.2/Rails3/Heroku). Any idea how to do it with Google, Yahoo, or MS-Live?
in the index.js.erb file, it always expects a partial to be available in app/views/products/_product.html.erb. Why is that so and is there any way to skip this?
I'm trying to get the demoed callbacks to work. Do I need to update <%= javascript_include_tag :defaults, 'rails.validations', 'rails.validations.callbacks' %> to specify the .callbacks JS file as well?
I've updated my jquery-rails with the --ui tag yet, I'm getting 'easing' related errors in the jquery JS itself based on the example callback code.
Yes. The PeepCode screencast about CoffeeScript shows how to use it with Jasmine.
This screencast mostly shows how to transition from JavaScript to CoffeeScript. However, CoffeeScript adds a ton of really helpful constructs beyond that which JS provides, cleanly and easily.
The thing I've found best about it is that it brings language features that I use all the time in Ruby (like
unless
, string interpolation using#{}
, improved looping, etc.) ...Mix that all in with the fact that it's more readable and uses fewer characters, well, you have a giant win.We live in a world where web development requires some fluency in multiple languages: HTML, CSS, JavaScript, Ruby, YAML. Providing ways to simplify (SCSS being identical to CSS, but adding new abilities that are easy to pick up and understand, for example) is key to speeding up development. CoffeeScript takes some getting used to, sure, but in the longer term it'll make more readable, easier to maintain and more enjoyable to write code.
As a side note Ryan, I've noticed our avatars are being cached here at Railscasts. I updated my account at Github months ago and I'm seeing old info here, is there a way we can force a refresh on our profile here at Railscasts?
I know I can edit my profile, but I can't change my Github account.
Thanks Ryan for a great intro to CoffeeScript - I've been eagerly anticipating an episode on this this.
And thank you to Trevor Burnham for your comments. I can see now why I should learn CoffeeScript. It didn't quite click that I'd be moving any syntax errors into compile time until I read your comment. That's a big plus in my opinion. I look forward to buying and reading your book.
As a side note, I've just taken a fresh look at HAML because I started playing around with ActiveAdmin. I must say I really must have been in a bad mood the last time I looked at it because now it really does seem appealing. I will have to play with it a little more too I think (even though it's not a default in the Rails stack)!
If nothing else Rails is teaching me to be more open-minded.
Hello guys,
I'm new in RoR and these screencasts are very very nice, thx at Ryan!!!
I saw a screencast about Jasmine and i want to use it. It's a cool GEM to test the JavaScript, but can i use CoffeeScript?
Watching how you converted your JS to CS just makes sense. Before I was thinking I could not be bothered but now I think I maybe become a convert.
Thanks Ryan great cast.
Well, having to go through a compile step is obviously a downside, though Rails makes this very transparent. (And in my experience, many of the things that you get as compile-time errors in CoffeeScript would have been run-time errors in JavaScript, where your typo would have gone unnoticed until you click a particular button... as I say in my book, "The compiler's here to help.")
Perhaps the biggest downside is that your code will become obsolete as new CoffeeScript versions are released. This is, of course, true of almost any language (see: Ruby), but if you're writing pure JavaScript, you can be sure that browsers will continue to run it for as long as the web as we know it exists. CoffeeScript has seen few changes since 1.0 came out in December (1.1.0/1.1.1 mostly just fixed bugs and improved the REPL), but it'll continue to evolve in the future, and that evolution will inevitably break backward compatibility.
Still, being able to write code more quickly and read it more easily is such a huge win that I wouldn't hesitate to use it on almost any project.
Ryan, thanks for the intro to CF. I can see that this should lead to code that is quicker to write, and more easy to maintain than pure JS.
Yeah, too bad CodeRay doesn't include a scanner for it. Ping me on Twitter if anyone is interested in making one.
Is there an automated way to convert from javascript to coffeescript??
i agree!! i loved that screencast, only i use Guard-coffeescript for compiling, seems more straight forward to me.
It's funny, this is the number one argument for justifying the change. However, if you are a working developer and work on different projects, chances are you will HAVE to learn it if the majority of apps use it.
If you look at most rails apps <= 2.3.x that are currently in production, they most certainly use rails standards of the time. e.g. rails 2.3.x apps use prototype. So, I would say just learn it. You may find you like it...
If you dont want a framework and community with a gold standard you are free to go back to Java or PHP. Like it or not, Rails came up HUGE (and drug Ruby along with it) because DHH, and later the core teams decided on opinionated software. The Golden Path is what separates good rails code from bad more times then not.
For instance, you can jump around just about any auth plugin or roll your own. But if you dont include common idioms for rails authentication it will be unusable by most coders or gems. Do I think current_user is the best helper method name for this? Maybe, maybe not. But I am not going to start rolling my auth modules with anything else. Thank god for Golden Paths.
Oh, and if you are still writing pure javascript I have high doubts that you are very efficient.
If anyone really wants to get hit in the head with why CS is now the default, check out DHH's talk at RailsConf, http://www.youtube.com/watch?v=cGdCI2HhfAU
what exactly are the downsides? thanks for a example ;)
"<%= %>" oh my god. my fingers scream every time i hit this key combination (on mac keyboards) ;)
Great intro!
BTW I think we should start calling '->' 'dash rockets' as also done in PeepCode's CoffeeScript video: http://peepcode.com/products/coffeescript
Ryan, you were at my talk at RailsConf, right? Thanks for finally giving the overview everyone wanted me to provide. :)
As to the discussion of whether learning/using CoffeeScript is worth it: Check out some of the blurbs on my CoffeeScript book, including one from Brendan Eich, the creator of JavaScript (who was kind enough to review a draft and even offered a few corrections).
Yes, there are downsides to using CoffeeScript rather than JavaScript, but the benefits
Who says you need to use Jquery? or Coffeescript? or Haml? or Sass? or Git? or Activerecord? Or ...?
Rails is modular, in fact, I think it is one of the most modular frameworks out there.
Yes, rails comes "packaged" (as in, it has a few extra lines in the Gemfile) with some defaults, it's an opinionated framework, and that's one of the reasons why it's gotten so big so fast, but all that does is make it easier for beginners to jump in and start coding.
Is it really that hard for you to type a few extra characters when you create a new app?
Rails new myapp = Jquery, Cofeescript, Sass, Erb, Activerecord, Test Unit, Git
Rails new myapp -OGJT = ... none of the above, go figure!
So all assets get packaged together and sent in one file? I really hope that I didn't understand that correctly.
Let's say they all pages share the jQuery library, linked directly from Google. Normally the jQuery library would be cached and not downloaded on each page. In fact, if downloaded from Google for another site, it wouldn't be pulled for my site at all. And for any one page, only the JS necessary for that page would be downloaded (never all of it at once). Plus with separate asset files, aren't they downloaded in parallel? And if I update JS on one page, only that updated JS would get downloaded when viewing that page in Rails 3.0, but in 3.1 it seems like ALL of my JS gets pulled down again when any one JS file gets updated.
Peepcode also has a screencast on Coffeescript for those wishing to see more in-depth examples of Coffeescript usage.
Thanks Ryan for another great screencast. I've already cleaned up a bunch of my javascript and my JS files just feel so much more manageable and readable now.
@Dmitri @Nicolas: Firstly, I find that learning a new syntax happens with very little effort. For CS, I read a few blog posts upon encountering it and this took perhaps an hour. Then I learn the syntax as I use it and this means I might occasionally have to look up the reference when I get a compile error and can't work out what I have done wrong. Initially this does add some time to development but only a very minimal 5 minutes here and there. Consequently, I don't buy the argument that the difficulty in learning CS is so great, it overwhelms any benefits.
Secondly, I see no reason to treat myself as a human compiler. If I am repeating the same stuff again and again, it is a sign that something could be done programmatically. Unfortunately, this is not always viable in an interpreted language where speed is such an important factor. Additionally, it is difficult to update a language on the client side so where clear improvements are identified, they remain unusable to the developer. CS solves both of these problems. For example, see destructured assignment.
Thirdly, I don't like CS just because it is "prettier". I can take or leave the new function syntax for example. What I do like are the abstractions that clearly make sense and are more readable than the JS alternative. Default arguments, variable safety, splats and comprehensions are enough reason for me to use CS. However, I do like all of the more sugary stuff as well.
Well, yeah, peoples opinions vary about haml. In the beginning i needed to get used to it too, but now it so much easier code views than it is with html + erb. By far the biggest benefits to me are not having to close tags, and not having to use the messy <%= %>.
You should not. You just have the option if it seems worthwhile to you.
Same arguments apply to moving to any higher level language.
I like sass and scss, but my eyes got pain of haml and coffee. I prefer old good JS and HTML/Erb.
Barista
@Nicolas: You don't have to use any of this stuff. You still can use plain JS. You don't have to use git and you still can use Prototype+Scriptaculous. You are not walled in, just because the defaults are different.
totally agree with @nico (Ritsche).
i have no idea why everybody is crying because of coffeescript.
thats 1 !!! line you have to comment out when starting a project in rails 3.1 if you don't want to use it.
@Nicolas: Well, its pretty simple, its nicer and cleaner to use... And you really don't have to use it if you don't want to. Well, and yes, there are choices made by the core rails developers... But Rails is an open project afaik, so you can contribute to those choices if you want. And if you don, fine, but then you might need to find some other tools to use if Rails doesn't suit you...
Dmitri: I can't understand either even after a look to this screencast. It just shows you can strip off "function" and things like that but do we need that ?
@Dmitri: JS is not bad at all, no, Coffe Script just makes it easier to write js code. I like it. Same goes for me with html and haml. I stopped using html in favour of haml, as its just cleaner and easier to write.
Ryan, it says 3 Comments, but there are 4 already...
I still do not understand why I should learn 3rd and 4th level languages? Why common JavaScript is bad now?
hi Vladimir.
In a very fast way you can use guard.
Then your coffeescript compiled in javascript every time you save a .coffee
I m not sure this is the perfect way but it's work and it's easy to implement in rails 3.0.x
Ryan, thank you for this awaited episode. I would like to use CoffeeScript in Rails 3.0.7 app. Can anybody suggest how to integrate it?
no coffee color syntaxing yet in show notes :)
Following the tutorial, and it's great. I have refactored quite a bit of code and made several adjustments for clarity sake. I am having one issue though, and it is regarding using any OpenID::Store::Filesystem based provider (google_apps, open_id). It seems that you cannot 'attach' them to an existing account, and they always want to create a new account even if you are logged in. Is this a known issue, and is there a work around?
Great design, Ryan. I also like the Github authentication. Is that new too?
it's k now, but the broadcasting doesn't work - it doesn't reload messages inside chat window, when anoyher client adds something
why the respond from faye can not be understood by browser(the chat window and text field are not reloaded)?
Very nice.
At least this sheds some time on the conditional callback
:unless
. Any thoughts as to why:message => ...
isn't supported?I think, that cancan affords a more convenient way to describe roles.
this post on stackoverflow.com explains clean fix for this verification fail.
http://stackoverflow.com/questions/3977303/omniauth-facebook-certificate-verify-failed
thanks for the note.
Great screencast Ryan.
I managed to get logins using Twitter, Facebook, Github, and LinkedIn working on a toy app (Ruby 1.9.2/Rails3/Heroku). Any idea how to do it with Google, Yahoo, or MS-Live?
I take it you tried the following ?
Figured it out, solution is here.
Hi Ryan,
That is very helpful. Wasn't aware that integration of HTML 5 with Rails 3 will be so simple and doesn't require unnecessary lines of code.
One small doubt: When we write this code:
in the index.js.erb file, it always expects a partial to be available in app/views/products/_product.html.erb. Why is that so and is there any way to skip this?
Best Regards
Puneet
What about pagination? is the library compatible with it? I wouldn't like to get 900 nested comments in one single call.
Hi Brian,
I'm trying to get the demoed callbacks to work. Do I need to update
<%= javascript_include_tag :defaults, 'rails.validations', 'rails.validations.callbacks' %>
to specify the .callbacks JS file as well?I've updated my jquery-rails with the
--ui
tag yet, I'm getting 'easing' related errors in the jquery JS itself based on the example callback code.Thanks!
Mike.