Should I be using a framework for Ruby? - ruby

I have once again fleshed out Ruby, after two years of not touching it, and am considering programming for the web with Ruby. However, I have found that the Ruby on Rails framework is just too large and bloated for my taste. It is usually a matter of preference, but in my case, I just want to be able to program on the web without having to worry about structuring my code with a framework similar to RoR.
When programming for the web with Ruby, should I be using a framework? Is it recommended? If you could recommend a simple one, which would it be?
Thanks.

The simple and minimal framework for ruby is sinatra

IMO, using a framework is good idea (don't reinvent the wheel etc). Just find one that suits your needs.
Apart from Rails, I've heard about Merb, Camping, Sinatra and Ramaze. For a quick comparison, you might want to read this post. For some other ideas, check 10 Alternative Ruby Web Frameworks.
EDIT: As pointed out in a comment, Merb and Rails are merging and according to Merb creator Ezra Zygmuntowicz (see this blog post), "Merb is Rails and Rails is Merb" so I guess Merb isn't that light.
EDIT2: As pointed out in another comment, the goal of this merge is to make Rails malleable so that you don't need to use all of Rails if you don't want to.

Sinatra is a very good choice

Even if you use no framework or a very light weight one, you will notice that when your application grows, you'll try to replicate existing features of Rails or other MVC frameworks to solve your challenges. By the end of the day, you'll still end up with a heavy weight framework whichever path you take.

Related

Sample Ruby Application

I'm going to learn Ruby to use Ruby on Rails in future to look for new horizons. I've read a lot about Ruby, but... I doesn't found any real applications that are built with Ruby? For Mac I've found only CLI apps.
Does anyone have use cases for pure (and popular) Ruby applications?
Try googling for "ruby code for applications"
I found this:
http://www.fincher.org/tips/Languages/Ruby/
If your ultimate goal is Rails applications, I would learn that at the same time (that search is easy too. Many folks start with the famous hartl tutorials and Ryan Bates Railscasts), particularly to get experience with the miriad conventions that you need to know to write good rails apps. If you got good at ruby first you could end up writing a lot of code in rails apps... that could be written for you via rails conventions and external gems.
I would advise you to learn both stacks simultaneously. The ruby you need for bread and butter work on Rails is not that complicated. Furthermore Rails is very centered on the framework itself. Unless you need to write your own GEMs (plugins) you will not dive too deeply into the ruby part.
A good start for Rails are the courses mentioned here:
http://rubyonrails.org/screencasts
For specialized knowledge I can only emphasize Ryan's Railscasts listed there.
For books I recommend the Galileo Computing "Ruby on Rails" books. For Rails 2 there is a free version online. For 3.x, which you prpbably want to use, you have to buy it.
I would recomend Ruby Best Practices, good guide how to build applications.

Can you use ruby for web pages other than ruby on rails?

Is Ruby primarily only used in ruby on rails? Is it used on the server side for general work like php is? Also, I haven't seen a lot of hype about rails anymore. Is Ruby and/or RoR dead or fading away?
I ask because I was interested in RhoMobile for building mobile apps, but I didn't want to get into using an antiquated language.
Thanks.
edit: Can i use Ruby for web pages if I don't want to use rails? (I do not mean another framework. I mean like php.)
Regarding your question about Ruby and/or RoR dead or fading away, look at the job trends
There are many web-frameworks for Ruby, not just Rails, Sinatra being one of them.
You shouldn't be deciding to use a language or technology, because there is or there is not a hype around it.
If a product is able to solve your problems, then you should use it. I know people building stuff in Smalltalk nowadays (who would have thought, right?), because it's great and it works.
Take a look at Sinatra, for example.
Also there is a lot of tools written entirely in ruby.

Framework for non-web Ruby project

I'm looking for a simple Ruby framework for a non-web project. I'm very familiar with Rails, so when I write pure Ruby I miss:
The different environments (development, test, production)
The console rails c
And many other utilities provided by rake or rails
I know I can require ActiveSupport and friends, but it's not what I need. It's mostly the development framework that I miss.
One thing I investigated is to do my project as a gem (even if I don't need a gem at the end). Using jeweler for instance provides versioning. I'm sure there are better ways but I can't find any. What would you use ?
Alright, that is somewhat of a three-part question. A threstion, or triesti... actually that doesn't work. Threstion it is then.
One thing to understand about Rails is that it isn't magical. It's code, written by mortal humans who have faults (such as oversized-egos) but who are also incredibly damn smart. Therefore, we can understand it after perhaps a little bit of studying their work.
1. Rails environments are lovely
The code behind the Rails environment management is actually quite simple. You have a default environment which is set to development by this line in railties/lib/rails/commands/server.rb. You could do much the same thing in your application. Run a bit of initialization code that defines YourAwesomeThing.environment = ENV["AWESOME_ENV"] || "development". Then it's a matter of requiring config/environments/#{YourAwesomeThing.environment}.rb where you need it. Then it's a matter of having configuration in those files that modify how your library works depending on the environment.
I couldn't find the line that does that in 3-1-stable (it's gone walkies), so here's one that I think does it in the 3-0-stable branch..
As for the lovely methods such as Rails.env.production?, they are provided by the ActiveSupport::StringInquirer class.
2. Rails console is the Second Coming
I think rails console is amazing. I don't want to create a new file just to manually test some code like I would have done back in the Black Days of PHP. I can jump into the console and try it out there and then type exit when I'm done. Amazing. I think Rails developers don't appreciate the console nearly enough.
Anyway, this is just an irb session that has the Rails environment loaded before it! I'll restate it again: Rails is not magical. If you typed irb and then inside that irb session typed require 'config/environment' you would get (at least as far as I am aware...) an identical thing to the Rails console!
This is due to the magic that goes on in the initialization part of Rails which is almost magic, but not and I've explained it in the Initialization Guide Which Will Be Finished And Updated For Rails 3.1 Real Soon I Promise Cross My Heart And Hope to Die If Only I Had The Time!
You could probably learn a lot by reading that text seemingly of a similar length to a book written by George R. R. Martin or Robert Jordan. I definitely learned a lot writing it!
3. The Others
This is where I falter. What other utilities do you need from Rake / Rails? It's all there, you just need to pick out the parts you need which is actually quite simple... but I can't tell you how to do that unless you go ahead and paint those targets.
Anyway, this is the kind of question that is exciting for me to answer. Thanks for giving me that bit of entertainment :)
Edit
I just noticed the BONUS fourth question at the end of your question. What would be the point of versioning your project? Why not use a version control system such as Git to do this and just git tag versions as you see fit? I think that would make the most sense.
First of all, Rails goal as a web framework is to let you mostly forget about the low-level details of a web based application (interacting with a web server, a database, etc.) and provide you with high-level abstractions which allow you to concentrate yourself on the actual job: Getting your application done.
There is such a huge diversity of non-web applications, that it is simply impossible to have a framework that "makes them all".
It is useful to understand what a framework is, and why and when you should use it.
Let's say you would have to build a dog house. You would buy some wood, nails and a hammer to build it the way you want from scratch, or you could go to Walmart and buy a DIY doghouse kit.
You will get a manual with detailed instructions on how to build your dog's "dream doghouse", and you will get it faster done than when making it from scratch, but you will be limited to the wood that comes with it and may be forced to use their nails or screws, and in some cases you would also need to buy a special screw driver.
The DIY kit is your framework. Some smart guys already provided you with the basic tools of making your doghouse, you only had to figure out (or not, if you had a manual) how to do it given their "framework".
Now, let's say you would build a skyscraper. The materials, tools and conventions (the framework) used would probably not be the same as they would when building a doghouse, and it would be over-kill to use a "skyscraper framework" to build a doghouse.
You wouldn't write Twitter client with the same set of tools and conventions as you would when writing a banking application with a billion concurrent users.
Rails is just one framework which provides you with a set of many, many good (and useful) conventions to write a web application, but that doesn't necessarily mean that it's the ultimate way to go. You can choose another framework, and maybe you will like the conventions for writing your application even more than the ones provided by Rails; or you could write all from scratch which would not limit you to what is provided by a specific framework and maybe make it even better.
To answer your question: Think of what you want to write and don't let yourself be narrowed down by the set of tools and conventions provided by a framework, as they will surely be if you choose this path.
Buy yourself some books about Design Patterns and read some great software, and then you may understand why the authors did not choose an application framework. Indeed, they've developed their own conventions and defined the behavior of what it should do; and once again, the "Rails way" isn't the only way, which doesn't make it the bad way. :)
I would appreciate constructive criticism as I really tried to provide a good answer.
Have you looked at Monkeybars? It's a Rails-inspired MVC framework that runs on top of JRuby and Swing to build desktop applications. I like it a lot.

web development with ruby without rails?

For reasons beyond my control, I'm being tasked with Ruby web development that does NOT use Rails or really any other heavyweight framework such as Merb. I'm wondering if anybody else has encountered this and can recommend best practices or resources that describe best practices for such a problem. I'd like to avoid the dreaded
out.print('<td class="foo">'+some_data+'</td>')
style of web development. A coworker has suggested Rack as a light framework but the documentation is sketchy and it seems unproven in the market.
Take a look at Sinatra. It's a framework, but not that heavy.
Look at Rack and Sinatra or Waves. Rack is not unproven in the market. Rails is built on top of rack these days! It also isn't a framework, it allows frameworks to focus on their differentiator rather than the low level busy work.
You can also use jruby and the java servlet api directly. btw, all the rack based frameworks can easily be run on jruby.
Rack isn't an application framework so much as a server interface. You will probably want to use it for this project, but it won't solve your problem of wanting a framework without using a framework. At any rate, if you want to "avoid the dreaded out.print('<td class="foo">'+some_data+'</td>')", you're going to have to use a template system at the very least. There are many available, such as ERb (which Rails uses by default) and Haml.
Go with Ramaze. I'm using it in production, running on JRuby and Glassfish.
Ease of development, rock-solid community support. No cruft. Like Ruby, it gets out of your way.
I can join in with everyone who's recommend Sinatra. It's compact, a joy to code with, and it deploys on Rack, which means you really get it running in whatever stack you fancy most (Mongrel, FCGI, Thin, Passenger, etc.)
I tried out Sinatra when I needed to get a project up fast and it's scaled & performed so well that I'm still using it today to handle over 80,000 heavy requests a day.
As always good ol' cgi.rb is available.
Have you had a look at Sinatra? It is a framework, but not as heavy as Rails. I haven't tested it out myself, but it seems to be very easy to work with.
Also, Ramaze seems to be nice and modular, not sure if it is what you look for though.
I don't know how easy it is to use Ruby directly as a cgi, but it should be quite possible to separate layout from code with that as well. There are quite a few Gems that can do that without using a framework.
My personal choice and recommendation is Ramaze -- it's as simple as possible, but no simpler. Clean and concise without sacrificing power. Dances well with your choice of JS lib (jQuery, Prototype, Mootools) or ORM (Sequel, Datamapper, M4DBI) or templating engine. Also, don't forget about static generators like nanoc.
It's very possible, my entire website runs on pure ruby.
I haven't encountered any problem. I begin my application with
require "mysql" # module | mysql
require "cgi" # module | cgi
require "date"

Sinatra success stories [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
Have you used Sinatra successfully? What kind of a project was it? In what situations would you recommend using Sinatra instead of Rails or Merb?
I've dabbled with Sinatra, but haven't really written anything serious with it.
As you said above, there's a list at http://www.sinatrarb.com/wild.html, although a lot of the applications listed there seem to link to GitHub pages, which I assume are often people experimenting with Sinatra and publishing their results online. Then, there's also the Sinatra mailing list, where you might find links to some interesting projects.[*]
As for your question on when to use Sinatra, I personally would answer "for smaller projects." When you want something up and running very quickly, it seems like Sinatra is an excellent choice. It's also great for people who like Ruby. With that I mean, when you're doing something in Rails, you have to do it "The Rails Way". Rails is the framework upon which you're building your application, and you have to adhere to its customs and conventions. Sinatra, on the other hand, feels like a library. You feel like you're writing Ruby, if you want to connect to a database, you use the library you like/think is appropriate for the job, if you want to output HTML you choose the templating library you like, and if you want a simple web framework, you choose Sinatra. Sinatra is not something upon which you build your whole application, it's something you use beside the rest of your application.
So, as you may have gathered, I'm quite fond of Sinatra, and I would use it for personal (or small-scale) projects. It's easy to set up and easy to use, as long as you know what you're doing. Looking through http://www.sinatrarb.com/wild.html, it seems like that's what most people are using it for, see for example Is Lost on yet? and Calendar About Nothing.
[*] Edit: I found a thread here, with people linking to their projects. There seem both larger and smaller projects. Very interesting stuff.
There is a list at http://www.sinatrarb.com/wild.html.
Still, I'd like to hear a bit more about them. I also suspect that there are lots of successful Sinatra projects outside that list.
I just released TweepDiff (http://tweepdiff.com) written in Sinatra. Anything else would have been overkill but I would use Sinatra for bigger projects too.
I think sinatra is best suited for micro-applications development (no big surprise, it's a micro-framework)
Sinatra provides you with a sufficient level of abstraction to build almost everything you want quickly. And what I like about sinatra is that the framework gives you tight control over what your app is actually doing, you can really "feel" what you are writing. So, I would say Sinatra is a subtly balanced framework.
I also think Sinatra is attractive for "people who likes writing Ruby" as said before. You start writing ruby class before implementing a sinatra interface. (good exemple of this kind of workflow here : http://dev-logger.blogspot.com/2009/01/ric-rac-roe-in-soup-of-technologies.html)
I wanted to be able to run succinct self-contained Ruby scripts from my web server. As far as functionality goes, it would be similar to a CGI library. Sinatra was perfect. One sinatra app later and I have a collection of scripts accessed through simple sinatra style routes.
Whenever I need to write a web application that doesn't depend on a database, I suspect sinatra will be my first port of call.
I'd have to echo most of the above comments. We're in the process of implementing a Sinatra/HAML stack for browsing and basic reporting on data.
I really like the combination of simplicity and direct connection to Ruby. If something isn't working in the browser, it's pretty easy to port it out to a straight Ruby program for debugging.
There's definitely a feeling of building a dog house with Sinatra versus the Taj Mahal with Rails. It's easy to comprehend (it's also helping me understand the need for MVC and Rails).
Altogether very cool stuff and very fast. I haven't stressed it yet, so I can't say how well it'll behave in the real world.
I made http://istay.com using sinatra, all I can say is that it is a fantastic little framework for any site that doesn't directly use or doesn't have a database.
Though I do feel sinatra has reach it's limit with the current site, and any extensions will be written using Rails or other framework.
I use Sinatra for small 'one function' kinds of apps (My current blogging engine might be a good example). I think the simplicity of Sinatra works best for little utilities and basic API front-ends (Twitter apps, etc).
For larger scale apps there is even the Sinatra-based Padrino Framework with some of the niceties that you would usually reach for Rails to provide.

Resources