Send to KindleQuestions on Ruby? Ask An Expert at RubyLearning
RubyLearning is happy to announce the start of a series of blog posts titled “Ask An Expert”. We are assembling experts in various areas of Ruby programming who will answer your questions as a blog post here.
To begin with, some of the experts who have agreed to answer your questions are:
- Gautam Rege on Ruby with MongoDB, Mongo, MongoMapper and Mongoid
- Gonçalo Silva on Ruby/Rails performance
- Sau Sheong Chang on Sinatra
- Sethupathi Asokan on the Ruby devise gem
- Staffan Nöteberg on Ruby regex
- Steve Klabnik on Testing in Ruby
I am sure there are more Ruby experts to follow.
Please feel free to ask your questions on the topics mentioned above, as comments to this blog post. We will put up these questions to the experts who will answer them here as a separate blog post, provided there are at least 7-8 questions per topic. If not, they would answer your questions directly as comments.
Here’s some information about our experts:
Gautam Rege is a Director and Co-founder of Josh Software Private Limited a Pune based software company that specializes in Ruby related services and product development. He is currently busy writing the book “Ruby and MongoDB Web Development Beginners Guide”.
Gonçalo Silva, is a full-time Ruby on Rails developer at escolinhas.pt and has participated in the Ruby Summer of Code 2010. He loves and contributes to many open-source projects, being a fan of Linux, Ruby and Android. He likes to call himself a hacker, but that’s just an excuse for being in front of the computer all the time. Oh, and he tweets at @goncalossilva.
Sau Sheong Chang, is currently the Director of the Applied Cloud Computing Lab, in HP Labs Singapore. Prior to this he was the CTO of Garena Online, one of the largest game publishing provider in Southeast Asia and before that, the Engineering Director for Yahoo! Southeast Asia. All in all, he has more than 15 years of application development experience, mostly web application and in the past 5 years, mostly in Ruby. He published a book Ruby on Rails Web Mashups in 2008 and recently published a new book Cloning Internet applications with Ruby in August 2010. He is active in the Ruby community in Singapore, where he is currently living, being one of the pioneers in Singapore Ruby Brigade.
Sethupathi Asokan is a Director and Co-founder of Josh Software Private Limited a Pune based software company that specializes in Ruby related services and product development.
Staffan Nöteberg wrote the critically acclaimed productivity book Pomodoro Technique Illustrated. He knows the regex (regular expressions) stack all the way from the automata theory base up to the newest regex idioms. And he’s proficient in Oniguruma – the Ruby regex engine.
Steve Klabnik, is a software craftsman, writer, and former startup CTO. Steve tries to keep his Ruby consulting hours down so that he can focus on maintaining Hackety Hack and being a core member of Team Shoes, as well as writing regularly for multiple blogs.
Please support RubyLearning: Buy the Kindle version of the The Ultimate Guide to Ruby Programming eBook on Amazon.
Technorati Tags: Ruby Gurus, Ruby, Programming, Ruby Newbies, Ruby Experts
Posted by Satish Talim
{ 37 comments… read them below or add one }
Gautam, I have two questions:
1. I have a lot of problems installing some of the gems on my Windows box. Is there a solution for us “poor Windows users”?
2. MongoDB has so many Ruby drivers – mongo, mongomapper, mongoid – where to use which driver and why?
Rohit maybe this is what you need for your first question. Thanks to Luis Lavena.
I’ll let Gautam answer your second question in a separate blog post.
Just to add to the list there is also mongo-odm and mongomatic
Remember this is open-source world – so there is no particular ‘mapper’ which one can recommend strongly. MongoMapper was the first and very popular. I prefer Mongoid because of its very robust and has *excellent* documentation. Choice of mappers is only about ease of use and your comfort level.
Do read this post from Peter Cooper: http://www.rubyinside.com/mongoid-vs-mongomapper-two-great-mongodb-libraries-for-ruby-3432.html
Sau, are there any other resources like Heroku where I can host my Sinatra app for free?
Engine Yard has a free trial: http://www.engineyard.com/
Cloud Foundry Beta is free: http://cloudfoundry.com/
DotCloud has a free option: https://www.dotcloud.com/
Konstantin, just bought your “Sinatra Up and Running” book. Looking forward to finding some more insights.
Konstantin answered it pretty well. I hardly use anything else other than Heroku because of the convenience but the ones that Konstantin are great.
Great! I hope you’ll like it. Really looking forward for your feedback.
Steve, Unit Test or MiniTest and why? What’s the difference?
Staffan, I always have had problems using regex. What’s the best way to learn and use regex?
Having problems while learning regex is a GOOD problem to have
The more you struggle, the more you learn.
I love http://rubular.com – its a Ruby regex site, it has a reference sheet, it has a very neat interface to check and test your regex.
I typically use these 2 examples to learn most aspects of regex:
+ Validate an email address format
+ Validate an IP address format
You learn almost ALL the regex aspects with these 2 examples.
I prefer MiniTest. Test::Unit is only still around because it used to be the standard framework, but it’s pretty vestigial at this point. MiniTest is an attempt to implement the same
MiniTest’s code is also incredibly small and readable. You’re also able to mix and match more advanced features too; one of my favorites is to use minitest/spec for the organization of my code, and use the assert style matchers rather than .should.
The only downside to MiniTest is mocks; I much prefer rspec’s mocks, and use them even when I’m using MiniTest to test code.
You should take regex learning as seriously as learning any other programming language, e.g. Ruby. Regex is declarative (not imperative like Ruby), it’s a domain specific language (not multi-purpose like Ruby) and it has no delimiters or extra whitespace. If you’re not familiar with languages with these traits, it’s even more important that you put effort in learning regex extensively. If you really want to learn regex, you should do most of the following activities:
Read a regex book. My favorite is Regular Expressions Cookbook by Goyvaerts/Levithan http://www.amazon.com/dp/0596520689
Take a full day regex course. I give a course like that in Europe (http://bitly.com/regexday), and there’re probably similar courses in Asia and North America.
Answer at least 50 regex questions at Stackoverflow. It doesn’t matter if your answer is upvoted, you should try to solve these problems to increase your own proficiency. http://stackoverflow.com/questions/tagged/regex
Use test-driven development (TDD) when you develop regexes, in order to get immediate feedback. It’s hard to learn without feedback.
If you want to master regex, you should also read a book on automata theory. My favorite is Introduction to Automata Theory, Languages, and Computation by Hopcroft/Motwani/Ullman http://www.amazon.com/dp/0321455363
I agree with Gautam that rubular.com is a great tool and can be used as a test bench. However, be aware of that rubular.com is (right now) based on Ruby 1.8.7. Ruby 1.9 has a new regex engine – called Oniguruma – with changed behaivour.
A question for Gautam. There are so many NoSQL databases available, how does MongoDB stand up compared to them?
Most NoSQL databases serve some purpose – some look at durability, consistence, scalability, speed etc. All NoSQL are BASE databases and not ACID. Basically Available, Soft state and Consistent – in-short, eventually consistent and scalable.
MongoDB is typically used for scale and data with loosely defined structure. The name itself is derived from “huMONGOous data”. Similarly Redis is for speed (in-memory database) but not suitable for large data, after all how much memory can you have? Riak is a fault tolerance distributed database. Cassandra is good combination speed and scale. Hadoop (HBase), BigTable are all used for storing vast amounts of data (and typically for map-reduce operations).
MongoDB differs from others that it saves data as BSON (Binary JSON), functions can be written in Javascript. Being able to serve web-requets via JSON, you ‘could’ actually hit the database from a web-page, get JSON results and render. (MongoDB also serve jsonp requests).
A Ruby example that accesses a MongoDB database created on MongoHQ and MongoLab would help. Gautam, which out of the two would you recommend?
Have a look at kipwiki – it was a hackathon project we created in the Pune Rails Meetup! Its hosted on Heroku and uses MongoLabs add-on. We used mongoid mapper for this.
Source : https://github.com/punerb/kipwiki
http://kipwiki.heroku.com/
Both MongoHQ and MongoLabs are good. Both are hosted on Amazon EC2. MongoHQ is older than MongoLabs but MongoLabs gives more free space! So pick anyone.
How do you make exe files with Ruby?
I guess you mean standalone exe-files for Windows environment.
You can use Ocra for this.
To install ocra open a command window and enter:
To build an exe file type:
Ocra has a lot of parameters that you can add when you build your exe file, look at
https://github.com/larsch/ocra
Hi Suraj, you can try this -> http://www.erikveen.dds.nl/rubyscript2exe
It’s rather old but the last time I used it, it was pretty solid.
Gonçalo, I have a question on rails -3 performance. I was using Rails 2.3.8. Recently I migrated to Rails 3.1.0. After the migration I noticed that my custom rake task which is using some ActiveRecord queries is slower now. What is the real cause of this? Is there any work around for this?
This is a generic query. How did you figure out that activerecord is the bottleneck? Anyway, check out the following things:
1. Are you using correct/latest version for database adapter gem?
2. Check that you are using appropriate clause (‘where’, ‘order’)
3. Are there any indexing constraints causing the issue?
4. Check if you are using ‘includes’ if you are retrieving the associated objects.
Once you post your findings, stats and log, it would help us in analyzing the issue.
I have one question: I want MySQL database as a backend to ROR. How do I link these two? I changed the database.yml file (for MySQL) what else do I need to change?
You need to have the MySQL gem and the necessary MySQL drivers.
Hi,
Well you asked, and I am stuck on this problem at the moment… my rails app is giving me a 406 Not acceptable from my action – its for specific function type actions and then it does a redirect_to the “show” action. Its not doing the redirect call, but instead gives the 406 error. It is doing the first part of the action, pre the redirect… See, for example, the method next_ball in this controller – https://github.com/kimptoc/Bingo-Caller/blob/master/app/controllers/games_controller.rb
Thanks for any tips/pointers – chris.
Fixed it
– I had an action named “status” and that seemed to confuse Rails. Renaming it fixed my problem. Would you consider that a bug in Rails?
Yeah! I was trying to remember where I have seen this problem. In that case it was ‘action’ being passed a GET parameter — this overrides the default :action parameter in the route and returned a 406 error. http://guides.rubyonrails.org/action_controller_overview.html#routing-parameters
The status method is defined in the action_controller/metal.rb and is responsible for sending back HTTP status code. So, you sent back incorrect status when you defined status as your method in the controller and overwrote the default status method
This is not a bug in Rails – this is a case of RTFM (no offense meant). http://guides.rubyonrails.org/action_controller_overview.html#the-response-object
Do you think shoes can replace gtk-ruby, qt-ruby and tk-ruby? What are the advantages and / or disadvantages?
It won’t ever be a _replacement_, but I do think it’ll have its place.
The biggest advantage of Shoes is that it feels like actual Ruby. The use of blocks is really great, and makes callbacks super simple. The other libraries are simple wrappers over the C code, and so feel like C libraries. Very odd.
Shoes is also way simpler. Shoes’ ‘background’ command looks like this:
background “#000″…”#FFF”
That’ll give you a background gradient from black to white. Implementing that same method in QTRuby is 14 lines: https://github.com/hacketyhack/blue_shoes/blob/master/lib/blue_shoes/pattern.rb#L39
Also, Shoes’s packager, while a bit buggy, is great for actually distributing apps. And while those all work on Windows or the Mac, it’s kinda rough getting GTK and QT to work, and they’re _massive_ dependencies. A Shoes app has about 30MB of overhead.
That said, the downsides of Shoes do exist. The biggest one is that the project is progressing really slowly; It’s me and two or three plucky other people; and we’re all part-time. So if Shoes doesn’t work for you or is missing a feature, I can’t guarantee things will be included in a reasonable time. Also, the packager is great, but has some bugs, and Shoes can get crashy sometimes.
I’m using Shoes extensively in my new book to run simulations. It’s a great toolkit and it’s a damned shame it hasn’t gotten too much usage or have too many people on it so kudos to Steve for being one of the few plucky people plugging at it.
I’d say one of the biggest downsides I faced so far is that the debugging capabilities are rather poor. I can’t output to a screen (except to the console in debug, but that’s not quite the same and it’s rather dicey in animation loops). Also, I can’t switch to a different Ruby version or even a different standard library. For example I was using the Vector from the Matrix package but the Ruby 1.9.1 included in the latest Shoes version doesn’t include Vector division and I can’t switch in Ruby 1.9.2. I ended up open classing Vector implementing my own Vector division.
What I really liked about Shoes is that it’s so easy to use and to read, something most other UI toolkits not only in Ruby but in most languages suffer from. So if you’re up to some quick prototyping and simple apps it’s an excellent choice.
I am a newbie to Ruby on Rails. What is the best way to master it? I was a Javaer.
You can check out Michael Hartl’s excellent online resource – “Ruby on Rails Tutorial“.
With Rails, how can I allow users to upload images in app/assets/images using FTP and get them processed by The Asset Pipeline in production. I enabled on-the-file compilation but it doesn’t seem to work.
I installed aptana rad rails in eclipse. While running newly created application via start server in debug mode it shows:
{C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:247:in `to_specs': Could not find ruby-debug-base19 (~> 0.11.20) amongst [actionmailer-3.1.3, actionmailer-2.3.5,................. (Gem::LoadError) }and I have ruby-debug-ide19 (0.4.12 ruby-debug19 (0.11.6) in my gem list..
(its work fine while starting server in normal mode)
What are the changes I should do for running application in debug mode?
My ruby version-ruby 1.9.3p0 (2011-10-30) [i386-mingw32]
rails -2.3.5
Sinatra (version 1.3.0 and up) supports content streaming from one’s application. A simple example for a newbie would help.
I just installed RoR and MySQL on my Lion Server. I used Homebrew to intall MySQL. After that, I typed “mysql -uroot” in terminal which shows a error message “ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’ (2)”. How can get through this? Thanks a lot:)
Just check the ‘mysqld’ (mysql server) is running.
I have one question
=> How to load a hash {“first” => “first”, “second” => “second”, “third” => “third”} into a constant Orders when Rails server started (values in the hash should be read from a configuration file for easy changing)
And
How to load an array [“first”, “second”, “third”] into a constant Orders when Rails server started (values in the hash should be read from a configuration file for easy changing)
{ 21 trackbacks }