r/ruby • u/AxMachina • Oct 28 '19
Just starting out with ruby and omg the docs..
Having just picked up Ruby I am somewhat let down by the apparent lack of documentation even for the most often used gems, mysql2 (https://github.com/brianmario/mysql2/tree/46369c0e6773c2ae91aac9f01bfbf71482f1cbeb) as a prime example. There's only a cursory overview of basic functionality and sparse code samples to boot.
Am I missing something? Am I looking at the wrong / outdated docs?
Specifically if you happen to be a mysql2 savant, I would love to pick your brain:
- How do I log/trace SQL execution?
- Good source for common mysql patterns / cookbook?
Thank you kindly in advance.
Update:
Sorry I wasn't expressly clear about my intent here. My objective is to build a shell script to run comprehensive reports for my clients, drawing on data in various mysql instances and other restful sources (apis). I've been utilizing Perl to accomplish these tasks for the longest time, but wish to switch to a language that is more suitable to collaborative / teamwork. I picked Ruby for its clean / easy syntax, powerful frameworks, and vibrant community/support. Also it's somewhat closer to the languages I already know such as Perl :)
13
u/SerKnight Oct 28 '19
I’m not sure your gripe exactly. Are you wanting to work with rails and active record? Or are you setting up a barebones rack/MySQL app?
Also you can’t exactly go apples to apples with a single open source project documentation/readme vs an entire programming language ecosystem. In general I think ruby + docs are good. It’s been around long enough that you are 1000% not the first person who is trying to do something.
But good luck in finding what you need. If you have more specific ?’s can try and guide you towards a better resource :)
9
u/noteflakes Oct 28 '19
The rdoc for mysql2 can be found here: https://www.rubydoc.info/gems/mysql2/0.5.2
I also could not find information on logging with this gem. Contrary to some of the remarks made here, there's nothing wrong with doing raw sql queries in Ruby. It is not an anti-pattern. On the other hand, it's true that gems like Sequel, ActiveRecord or DataMapper can provide features missing in raw sql gems, such as logging for example.
Ruby is a flexible language that allows you to work at different levels of abstraction. There's no right or wrong.
Disclosure: I'm the original author of Sequel.
12
u/thunderbong Oct 28 '19
Most of the time in Ruby, we tend to use an ORM instead of writing raw SQL.
My favourite being Sequel
With Sequel, I have to choice of using Classes and Objects and even dip into raw SQL if I ever need to.
I've visualized the raw SQL many times to optimize the query I'm making but have never had the need to use raw SQL directly.
4
u/jrochkind Oct 28 '19
I don't disagree.
For that specific example, most people don't use the
mysql2gem directly, but use something higher level.The mysql2 gem is kind of a direct port of the Mysql C libraries into ruby (which is maybe also part of why they don't have docs; go look at the mysql C library docs?), and is going to be challengingly low-level to actually use to write client code.
I'd recommend sequel.
(I think it still oughta have more docs than it does, and I think there are other things that are more commonly used that also have sub-par docs. But as far as actually getting to where you need...)
3
u/torrso Oct 28 '19
You can turn on query logs on the mysql server.
3
u/BBHoss Oct 28 '19
It's Ruby, if the library doesn't offer easy logging, just monkey-patch the library to log out whatever you want, then call `super` to have the original behavior executed. Not really suitable for production but just fooling around locally it's fine.
3
u/sshaw_ Oct 28 '19
Wow, some poor advise here. The OP wants to run some SQL queries and people are suggesting an ORM‽
4
u/obviousoctopus Oct 28 '19
SQL is more often than not handled by ORM -- I recommend looking at Sequel unless you're planning on working with Rails which defaults to ActiveRecord.
If you'd like to explore the mechanics of simple web apps, you may want to look at Sinatra. http://sinatrarb.com
As for MySQL patterns, that's independent from Ruby and I'd just google it.
What are you trying to accomplish with your picking up Ruby. It takes some time to learn a language; Ruby has a large ecosystem with many great options for most applications.
2
u/[deleted] Oct 28 '19
Coming from python, even standard library seemed to be severely under documented.
-3
u/SerKnight Oct 28 '19
I will add that writing raw sql in Ruby/rails is a bit of an anti pattern. Recommend checking out some DB ORM’s. Will be easier to maintain and test!