Ruby's File class vs. Rails' FileUtils - ruby-on-rails

Now about 2 weeks into learning Ruby and Rails, I've found myself using the File class a lot for things like File.join, File.open, etc. Then I bumped into a need for File.copy only to find out that no such method exists. A little more looking uncovered Rails' FileUtils class and now I'm a little confused.
There are differences, of course, but there are also what appear to be redundancies. Is one preferred over the other where there are differences? Why do both exist (is it solely to handle the omissions in the Ruby core class)?
I'd just like to get a feel for how these things work together or conflict so I know how to move forward.
Thanks.

FileUtils is part of the Ruby Core API. It's not Rails specific. Also, it's a module, so you could mix in some of the abilities it has to offer into another class. Your best bet is to probably read the File RDoc, as well as the FileUtils RDoc. Hope this helps you out a bit. Cheers.
Edit:
Is one preferred over the other where there are differences?
I don't know if there is much consensus over which to use when the methods have the same ultimate end–result, such as File.makedirs vs. FileUtils.mkdir_p, but I often see the latter in other peoples code. I think you will almost always use the two (File & FileUtils) in conjunction.

Related

Rails: Place all models in one file

Hey so I realize this probably isn't good practice, but I was wondering if it's technically possible to combine the code from my various rails model files into one .rb file?
I have a lot of very short models, and when making changes to them it'd be quite helpful to be able to see them all in one place instead of having to open up numerous windows.
This goes against the rails philosophy of Convention over Configuration.
Ruby is a highly flexible language, so yes it would be possible to do that, but you would have to undo much of the auto-configuration that makes up the entire reason for using rails.

Which features are missing in Ruby on Rails

which features you would like to have in Ruby on Rails, or maybe which features you find incomplete or bugy?
It's honestly a pretty full-featured framework. After using it professionally for two years, I have never come across anything Rails couldn't handle. In fact, quite the contrary, I'm constantly finding more and more amazing features I wasn't previously aware of.
Plus with the concepts of gems and plugins, the sky is the limit.
The more you use it, the more you'll love it.
Some things are easier done in pure SQL than ActiveRecord, and some are just impossible without SQL. This breaks the abstraction somewhat.
Since the creation of Engines, modularity & reusability made a huge leap which is great.
I'm wondering whether we'll need another abstraction layer for rich js interfaces. To be a more constructive, I'd personally appreciate to organize my js file the same way I organize my helpers.
#RocketR remark is totally right, as well as tybro0103 conclusion :)

Do I need to know Ruby in order to learn Ruby on Rails? [duplicate]

This question already has answers here:
Closed 12 years ago.
I've read several other questions about material in order to learn RoR. But my question is can I start learning RoR without Ruby?
It's clear that the other way around is better, but I would rather try this way if it makes sense (somehow). Or learn both in parallel...
Well, given that Ruby on Rails is just Ruby written in a particular fashion (read: framework) yes you will need to learn Ruby.
You will need to learn these things about Ruby (amongst other things):
Everything Is An Object. Everything. Yes, even 2 and "foo"
Class Inheritance and What It Does(tm)
How to tell the difference between class and instance methods
Modules and how to use them correctly (class_eval Is Your Friend)
People write gems. Use them. Re-inventing the wheel is fine and dandy for learning but re-inventing it because you think your method is better is not.
And these things about Rails:
Do not fight conventions.
Do not fight conventions.
Model code goes in the model.
Controller code goes in the controller.
View code goes in the view.
Helper code goes in the helper.
Ask yourself "Does Rails have a helper for it?". If the answer is "No" ask somebody else. The answer is likely to be "Yes".
And these things in general about learning:
Google it.
Google it again.
If the answer you seek is not within Googling distance, formulate it in a way that is kind to those who may know the answer (such as you've done with this question!)
Appeal to people's greater motives. How can helping you, help them?
Try not to be too much of a help vampire.
If asking in an IRC channel, use a pastebin service such as http://pastebin.com, http://pastie.org or http://gist.github.com
Second rule about asking in IRC: Don't ask "any ideas?" after your question. Your question should already ask this by itself.
And finally: Love what you do.
People used to say that you could learn RoR without knowing Ruby (including DHH, if I am not wrong).
But in my opinion, if you also know Ruby, you can do a lot more stuff than without knowing it. And the knowledge would be very helpful when you debug or troubleshoot your programs.
So my suggestion is learn both in parallel.
You can. RoR is in a way a domain specific language, since a lot is defined for you and putting a simple application together can be like following a straightforward recipe. It would of course be better to learn the language properly, but many people don't.
The Ruby For Rails book tries to teach you both at the same time, so you might want to look into getting that one. I haven't read it but I hear it's pretty good.
Obviously :) ... if you really want to develop good web apps ...
But it will also be interesting to first get your hands wet with some RoR sample applications using inbuilt features that RoR provides... and then learn the details(which will require knowledge of Ruby)
If you avoid learning Ruby, (that many of the RoR developers do)... then you will find yourself stuck at position and going through an alien code. And eventually you might leave RoR out of frustration. I have seen many of such cases.
The question you ask is slightly ambiguous. Specifically, you could be asking "Can I learn Rails without first knowing Ruby?", or "Can I learn Rails without having to learn Ruby too?".
The answer to the later question is probably no, if you want to be particularly functional with it. Without knowing Ruby, it would be hard to take advantage of much of Rails' power.
The answer to the first question is most certainly yes. While knowing Ruby first would make learning Rails faster/easier, you can easily learn both at the same time. That's actually how I learned Ruby/Rails, by taking a site I had to implement first in Java and re-implementing it using Rails.
You need to know both, The RoR framework is an organizational and convenience system if you will, what it organizes is your ruby code.
If you have programmed before then here are most of your answers:
http://www.ruby-doc.org/core/
http://railsapi.com

Anybody using ruby without any framework?

I am experimenting with Ruby and Rails. I like Ruby, but not Rails. I have Java/PHP background, I have used some frameworks, but never totally liked any of them.
Anybody using Ruby to build web apps, but not any of the frameworks? (rails, merb etc). If yes, can you point me to some resources to learn it?
It might be wise to at least leverage Rack, and maybe Sinatra gets enough out of your way for you to feel comfortable. Sinatra isn't much more than a very small, simple wrapper around the rack handler afaik.
Doing Web apps in Ruby without using a framework is like cooking without heat. I question the sense of trying this.
That said, you could probably hook up Apache to call a Ruby program as a CGI. I suspect it would be dog slow, though.
Edit: Apparently you're not the only one crazy enough to attempt this, though: I found a tutorial on Simple Ruby CGI. The author claims as his rationale that "he has nothing better to do."
Your question looks quite similar to Ruby off the rails, so you probably should read the answers of that one.
I'm using ruby instead of bash and python on my ubuntu server for sysadmin tasks.
I've found Ruby a particularly clean and powerful replacement for the places I'd have previously used bash and python in sysadmin.
I personally found one thing for myself. When I prepare some interview or I am gonna build something from scratch, in most cases I need some basic features included in Rails. That usually happens when you build something not complex, but still non trivial. In 99% of cases I need ActiveRecord, ActiveSupport etc. It is easy to get those things in your Gemfile and play with them. One thing you will always repeat is basic application structure, easy console access with preloaded libs, rakes... I created minimal gem for myself to organise this process though.. :) If you read sources you'll see what I constantly do to start something new. It is not a big deal not to use any framework at all.
https://github.com/einzige/framework

Are there any potential disadvantages in using a Ruby framework other than Rails?

I would like to use a lighter framework than Rails (Sinatra/Ramaze/Camping) but am concerned that by doing so I will not be able to use a lot of shared libraries that have been tailored to Rails in the form of plugins. Is this a major concern or are most of these plugins usable across different Ruby frameworks?
Are there any other potential disadvantages in using a Ruby framework other than Rails?
You can still use gems in all of the frameworks you mentioned, so a ton of stuff is reusable. Want to swap in a new ORM, no problems. Want a fancy shmacy syntax highlighting, no problems. Rails has been making a huge push to move away from the old plugin model to use gems exclusively.
If one of the other frameworks fits your needs better use it. Keep in mind that when it comes to documentation and samples rails has more.
If I was learning Ruby and wanted to try out a web framework I would probably go with Rails not because its better, but because its got much better tooling and documentation.
Most Ruby modules used by Rails (even ActiveRecord) can be used without Rails. But then you lose the extra benefit of integration provided by Rails. You may have to work extra hard to glue Ruby modules to the framework of your choice. Please also note that most of the documentation about Ruby modules used by Rails tells you only how to use that module with Rails.
Network effects play a bit of a role.
One issue that comes up when you use other frameworks like sinatra, camping, etc is that rails gives you a proven structure for your files in the your application. Smaller frameworks are quite open and free.
This can be a downside when you are working with multiple developers as you need to have conversations about creating conventions rather then simply following them.
If you've been using Ruby for less than a year, stick to Rails, unless you have a very clear need that is better handled by one of the other frameworks.
The lighter frameworks, most notably Sinatra, tend to be popular with people who know exactly what they will need and can't afford to have any additional overhead from unused code. Essentially, you pick your toolchain, instead of generally being stuck with what Rails gives you. (Yes, in Rails, you can replace ActiveRecord, et al. with other libraries, but it's not exactly easy.) So the lighter frameworks give you significantly more freedom, but you also have quite a bit more work to do in many cases.
I think no rails plugin is going to work out of the box with any of the alternative frameworks, except for ActiveRecord plugins (such as acts_as_nested_set etc.) which are still going to need some plumbing work (setting $LOAD_PATH and requiring right files). I'd recommend DataMapper for ORM, not only it's way faster than ActiveRecord, but it's also very modularly built and plugins are actual gems that you can easily install. In difference, ActiveRecord plugins are mostly monkey-patches that tend to break with every new version.
Sinatra doesn't come with any "goodies", no Rakefiles, no skeletons, no script/generate, but actually that's what it's been written for. You can gradually "plumb in" all the extra stuff. There are also skeletons for sinatra apps that come with some basic layout and defaults, you may find these useful.

Resources