# Is my code using the Ruby way?

**URL:** <https://www.ruby-forum.com/t/is-my-code-using-the-ruby-way/83819>\
**Category:** Ruby\
**Created:** [January 29, 2007, 11:54pm UTC](https://www.ruby-forum.com/t/is-my-code-using-the-ruby-way/83819 "2007-01-29T23:54:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 29, 2007, 11:54pm UTC](https://www.ruby-forum.com/t/is-my-code-using-the-ruby-way/83819/1 "2007-01-29T23:54:06Z")

</div>

As I’m a beginner in Ruby, and just (re)started working on real code,  
instead of working through various tutorials, I’m asking myself: Is that  
the Ruby way?

Snippet:

unless File.exists?(‘data/finance.db’) == true  
setup = Database.new  
setup.new\_database  
else  
puts ‘Database already exists!’  
end

The code is tested and working as expected (Database is my own class,  
which creates the database in the first place), and I don’t think it  
could be (much) shorter.

-Phill

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 30, 2007, 1:01am UTC](https://www.ruby-forum.com/t/is-my-code-using-the-ruby-way/83819/2 "2007-01-30T01:01:17Z")

</div>

On Jan 29, 8:52 pm, removed\_email\_address@domain.invalid wrote:

> ```
> puts 'Database already exists!'
> 
> ```
> 
> end
> 
> The code is tested and working as expected (Database is my own class,  
> which creates the database in the first place), and I don’t think it  
> could be (much) shorter.

I’m a ruby-nuby as well, but for one thing I think you don’t need that  
“== true” after the condition. If the method ‘exists?’ returns true,  
it is already taken as the value of the expression ‘unless’ needs to  
evaluate, so there’s no need for the comparison.

Cheers,

Helder

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 30, 2007, 1:27am UTC](https://www.ruby-forum.com/t/is-my-code-using-the-ruby-way/83819/3 "2007-01-30T01:27:20Z")

</div>

Also, I’d put the ‘else’ on the same level of indentation as the unless.  
As  
Helder wrote, you don’t need the == true bit, Ruby’s all about natural  
code.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 30, 2007, 1:38am UTC](https://www.ruby-forum.com/t/is-my-code-using-the-ruby-way/83819/4 "2007-01-30T01:38:22Z")

</div>

Tamreen Khan wrote:

> Also, I’d put the ‘else’ on the same level of indentation as the unless. As  
> Helder wrote, you don’t need the == true bit, Ruby’s all about natural  
> code.

Yeah, I did the former already (I like it better that way). And omitting  
the ==true part makes the code more readable.

Thanks for the feedback, folks.

-Phill

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 30, 2007, 2:24am UTC](https://www.ruby-forum.com/t/is-my-code-using-the-ruby-way/83819/5 "2007-01-30T02:24:26Z")

</div>

On 1/30/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> puts 'Database already exists!'
> 
> ```
> 
> end

class Database  
def self.create file = ‘data/finance.db’  
if File.exist?(file)  
puts ‘Database already exists!’  
else  
new file  
end  
end

Database.create

> The code is tested and working as expected (Database is my own class,  
> which creates the database in the first place), and I don’t think it  
> could be (much) shorter.

This is just to show that Database itself should take responsibility  
over creation/checking. It’s just a small example but should give you  
an idea.  
Tell, don’t do 🙂

^ manveru

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 30, 2007, 4:24am UTC](https://www.ruby-forum.com/t/is-my-code-using-the-ruby-way/83819/6 "2007-01-30T04:24:44Z")

</div>

On Tue, 30 Jan 2007 removed\_email\_address@domain.invalid wrote:

> already existing, and if not, to have a set of SQLite statements create it,  
> which I probably place in their own class, as the SQL can change, and this  
> would provide easier maintenance, and a better re-usability of the code,  
> too).

hopefully you realize this code contains a race condition. if you use a  
two  
step process to check that the database is created and, if not, to  
create it -  
you risk that another process might create it in between those two  
steps. a  
cleaner way to do it is to use execptions:

harp:~ \> cat a.rb  
require ‘sqlite’

class Database  
SCHEMA = \<\<-SCHEMA  
create table t(  
id int,  
data string  
);  
SCHEMA

```
 def initialize path
   @path = path
   @db = SQLite::Database.new @path
   setup
 end

 def setup
   @db.execute SCHEMA rescue nil
 ensure
   validate_schema
 end

 def validate_schema
   @db.execute 'select * from t limit 1'
 end

```

end

Database.new ‘db’

here, the db is always created and initialized with the SCHEMA.  
normally  
trying to create the table twice would throw an error, which is ingored.  
because ignoring this error might mask a real failure to set the db a  
simplistic method, in this case, is used to make sure the database looks  
like  
it’s supposed to. note that this whole thing is based on the knowledge  
that  
sqlite uses it’s own locking to ensure atmoicity.

in any case, the pattern of

“try it and recover if it blows up”

is often a good solution where testing a condition and then acting on it  
cannot be done in one step to avoid a race condition.

btw. sqlite and ruby are a good combination - i use them heavily in my  
own  
work.

kind regards.

-a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 30, 2007, 3:22am UTC](https://www.ruby-forum.com/t/is-my-code-using-the-ruby-way/83819/7 "2007-01-30T03:22:33Z")

</div>

Michael F. wrote:

> This is just to show that Database itself should take responsibility  
> over creation/checking. It’s just a small example but should give you  
> an idea.  
> Tell, don’t do 🙂

Ah, yes, that makes sense. I had to adapt your example to my needs, but  
that is only a minor thing (I only use File to check if my SQLite  
database is already existing, and if not, to have a set of SQLite  
statements create it, which I probably place in their own class, as the  
SQL can change, and this would provide easier maintenance, and a better  
re-usability of the code, too).

Although this raises another question for me: Is it better to keep  
functionality (i.e. everything that handles a database) into a class, or  
to group functions into a class? The former keeps everything neatly in  
one place, while the latter makes inheritance easier.

I lean towards a case-by-case basis, depending on the curcumstances of a  
project, but to keep to stick with one option or the other (Phill’s  
PLOS, so to speak 😉

## -Phill

Over by the window, lie the raiment and the weapons  
That we need to take into this world today  
Armoured by opinion, with statistic and schoolboy’s charm  
We take our place amongst the rank and file

Skyclad - A Survival Campaign

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 30, 2007, 4:31am UTC](https://www.ruby-forum.com/t/is-my-code-using-the-ruby-way/83819/8 "2007-01-30T04:31:50Z")

</div>

On 1/29/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> require ‘sqlite’  
> @path = path  
> def validate\_schema  
> like
> 
> btw. sqlite and ruby are a good combination - i use them heavily in my  
> own  
> work.

Just a general thank you for this detailed explanation - now I  
understand  
better how to avoid race conditions (and I’m much more certain of what  
exactly they are now).

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 30, 2007, 8:51am UTC](https://www.ruby-forum.com/t/is-my-code-using-the-ruby-way/83819/9 "2007-01-30T08:51:11Z")

</div>

On 30.01.2007 01:37, removed\_email\_address@domain.invalid wrote:

> Tamreen Khan wrote:
> 
> > Also, I’d put the ‘else’ on the same level of indentation as the  
> > unless. As  
> > Helder wrote, you don’t need the == true bit, Ruby’s all about natural  
> > code.
> 
> Yeah, I did the former already (I like it better that way). And omitting  
> the ==true part makes the code more readable.

It actually also makes to code more correct. In Ruby only nil and false  
are treated as false and everything else is true. So “if expr” and “if  
expr == true” are likely to behave very differently.

With regard to your original piece of code, since you are using both  
branches (true and false) I would prefer to use “if” as it makes things  
a bit more readable IMHO:

if File.exists? ‘data/finance.db’  
puts ‘Database already exists!’  
else  
setup = Database.new  
setup.new\_database  
end

Kind regards

robert
