Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Is my code using the Ruby way?

Reply
Thread Tools

Is my code using the Ruby way?

 
 
cmdjackryan@googlemail.com
Guest
Posts: n/a
 
      01-29-2007
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

 
Reply With Quote
 
 
 
 
Helder Ribeiro
Guest
Posts: n/a
 
      01-29-2007
On Jan 29, 8:52 pm, cmdjackr...@googlemail.com wrote:
> 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.


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

 
Reply With Quote
 
 
 
 
cmdjackryan@googlemail.com
Guest
Posts: n/a
 
      01-30-2007
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

 
Reply With Quote
 
Michael Fellinger
Guest
Posts: n/a
 
      01-30-2007
On 1/30/07, <> wrote:
> 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
>


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
>
> -Phill


 
Reply With Quote
 
cmdjackryan@googlemail.com
Guest
Posts: n/a
 
      01-30-2007
Michael Fellinger 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

 
Reply With Quote
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      01-30-2007
On Tue, 30 Jan 2007 wrote:

> Michael Fellinger 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).


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:atabase.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
--
we can deny everything, except that we have the possibility of being better.
simply reflect on that.
- the dalai lama

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      01-30-2007
On 30.01.2007 01:37, 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using ruby-openid/ruby-yadis w/o ruby gems George Moschovitis Ruby 1 03-11-2007 08:24 AM
#!/usr/bin/ruby , #!/usr/bin/ruby -w , #!/usr/bin/ruby -T?, #!/usr/bin/ruby -T1... anne001 Ruby 1 04-23-2006 03:02 PM
what is the difference between code inside a <script> tag and code in the code-behind file? keithb ASP .Net 1 03-29-2006 01:00 AM
my ruby code won't go as fast as my perl code Dave Burt Ruby 11 07-16-2004 02:35 PM
Fire Code behind code AND Javascript code associated to a Button Click Event =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?= ASP .Net 4 02-11-2004 07:31 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57