Do they belong to you? Claim these comments.
ehsanul
Is this you? Claim Profile »
1 month ago
in Tests should reveal code intent on Lucas Húngaro on GitHub
Nice write up :)
BTW, I don't see blank lines between every line of code.
BTW, I don't see blank lines between every line of code.
1 reply
lucashungaro
Thanks! And I forgot to mention that I have replaced all code snipped by Gists. It solved the problem. :)
2 months ago
in Ruby Best Practices - The Universe between begin and end on Ruby Best Practices Blog
Nice article. :)
Of course you're right about ensure, I just hadn't had experience with it, hence my naive comment.
Of course you're right about ensure, I just hadn't had experience with it, hence my naive comment.
1 reply
Robert Klemme
No problem at all. I learned a few of those subtleties myself experimenting and meditating about this construct. It is so surprisingly easy to use yet has some hidden features. I believe it's important to know the tools at hand -- and it can also be great fun to go on such an exploration. :-)
2 months ago
in Ruby Best Practices - Blog on Ruby Best Practices Blog
Sorry, badly formatted code. Can't figure out how to format text here.
if block_given?
. . begin
. . . . yield tee
. . rescue
. . end
. . tee.close
else
.. tee
end
if block_given?
. . begin
. . . . yield tee
. . rescue
. . end
. . tee.close
else
.. tee
end
1 reply
sandal
Yeah, Disqus seems to be sucking for that. I've been using code tags + non-breaking spaces. I added a link in the sidebar to a tool that does the conversion for you, but it's only a stopgap. We need to do something better.
I re-formatted your original code sample.
I re-formatted your original code sample.
2 months ago
in Ruby Best Practices - Blog on Ruby Best Practices Blog
"Why not just put the contents of the else at the end of the begin block?"
Well, putting the contents of the else after the begin block's end has a different behavior compared to having it in the "else" section. Code put after the begin block will run whether or not there was an exception (just like an ensure?), whereas in the "else" section, the code runs only if there is no exception. A valid use-case of code inside the "else" would be giving a user positive feedback for some successful action.
@Robert Klemme
However, I have a similar question concerning "ensure". Code that runs under the ensure would also run whether or not there was an exception if put just outside the begin block, provided there's a rescue. The code could have just as easily read:
if block_given?
begin
yield tee
rescue
end
tee.close
else
tee
end
So how does using "ensure" help, besides (possibly) readability? I think in most cases where you have a begin block, you want to catch exceptions, so a rescue is going to almost always be there, right? In which case, I don't see how "ensure" would help. Maybe I'm missing something.
show all 5 replies
Well, putting the contents of the else after the begin block's end has a different behavior compared to having it in the "else" section. Code put after the begin block will run whether or not there was an exception (just like an ensure?), whereas in the "else" section, the code runs only if there is no exception. A valid use-case of code inside the "else" would be giving a user positive feedback for some successful action.
@Robert Klemme
However, I have a similar question concerning "ensure". Code that runs under the ensure would also run whether or not there was an exception if put just outside the begin block, provided there's a rescue. The code could have just as easily read:
if block_given?
begin
yield tee
rescue
end
tee.close
else
tee
end
So how does using "ensure" help, besides (possibly) readability? I think in most cases where you have a begin block, you want to catch exceptions, so a rescue is going to almost always be there, right? In which case, I don't see how "ensure" would help. Maybe I'm missing something.
5 replies
Pistos
You can't take what is in the ensure block and put it after the end of begin-end. This code example should elucidate:
Output:
begin
puts 1
raise "Oh no!"
rescue Exception => e
puts 2
raise e
ensure
puts 3
end
puts 4
Output:
1
2
3
-:3: Oh no! (RuntimeError)
Robert Klemme
In addition to what sandal and Pistos replied there are a few things to say about begin end blocks. I'll write about them in the next installment.
ehsanul, thanks for the inspiration!
ehsanul, thanks for the inspiration!
ehsanul
Sorry, badly formatted code. Can't figure out how to format text here.
if block_given?
. . begin
. . . . yield tee
. . rescue
. . end
. . tee.close
else
.. tee
end
if block_given?
. . begin
. . . . yield tee
. . rescue
. . end
. . tee.close
else
.. tee
end
Pistos
ehsanul: I think you misunderstood my question. :) Perhaps a code example will help. I was wondering why we need this:
When we can do this instead:
(Gah, can't format code blocks. Disqus: fail)
begin
do_work
something_at_end_of_begin_block
rescue Exception => e
else
something_in_else_block
end
When we can do this instead:
begin
do_work
something_at_end_of_begin_block
something_in_else_block
rescue Exception => e
end
(Gah, can't format code blocks. Disqus: fail)
sandal
@ehsanul
The code you wrote is not equivalent because you swallow the error.
With an ensure block, the error is still raised, but the code in the ensure is run anyway.
The code you wrote is not equivalent because you swallow the error.
With an ensure block, the error is still raised, but the code in the ensure is run anyway.
>> def foo
>> raise
>> ensure
?> puts "Got here"
>> end
=> nil
>> foo
Got here
RuntimeError:
from (irb):2:in `foo'
from (irb):6
2 months ago
in http://blog.rubybestpractices.com/posts/jamesbritt/2009-04-13-solving-the-problem.rc.html on Ruby Best Practices Blog
Oh wow, only just read the comment about where the name of this blog engine came from... Damn, that used to be one of my favorite foods, still like it quite a bit! My aunt and grandma still make it for me if we happen to be in the same city. Good stuff!
2 months ago
in http://blog.rubybestpractices.com/posts/jamesbritt/2009-04-13-solving-the-problem.rc.html on Ruby Best Practices Blog
You guys should be in charge of what goes into Rails 3. Nice write-up, loving the minimalism in coding.
2 replies
ehsanul
Oh wow, only just read the comment about where the name of this blog engine came from... Damn, that used to be one of my favorite foods, still like it quite a bit! My aunt and grandma still make it for me if we happen to be in the same city. Good stuff!
jamesbritt
One of the reasons I liked Nitro, and now Ramaze, is exactly that aesthetic of feature selection.
3 months ago
in Twitter quakes as San Francisco shakes on VentureBeat
Dan, I would've never known about it if it wasn't for twitter. I just click on trends, and instantly see what a hell lot of people are talking about. Found out about it fasttt... Surely that's somewhat useful, if not that useful.
7 months ago
in PostgreSQL, Ruby, and Rails: I Quit on Josh Kim dot Org
Thanks for the reply joshkim.
Well, all that's being done there in the gist to do with this is "sudo gem install postgres", which is sorta obvious. I don't think the problem is with the gem, though I've tried uninstalling and installing a few times, and tried the other ruby-pg gem too. Robby doesn't have the same error I do. The error shows up when doing a rake command to create the database, and occurs for others when trying a rake db migration command.
I think this error only shows up with the latest rails, because I've been using postgresql on my laptop with rails 2.1.1 for a while. But it messes up on my server, which has rails 2.2.2 installed. The problem doesn't seem to be with the gem, but with activerecord itself. I don't know nearly enough about rails to begin trying to solve it though.
I guess I may switch to mysql for now myself, just to get things working, but that isn't really a solution to me.
Well, all that's being done there in the gist to do with this is "sudo gem install postgres", which is sorta obvious. I don't think the problem is with the gem, though I've tried uninstalling and installing a few times, and tried the other ruby-pg gem too. Robby doesn't have the same error I do. The error shows up when doing a rake command to create the database, and occurs for others when trying a rake db migration command.
I think this error only shows up with the latest rails, because I've been using postgresql on my laptop with rails 2.1.1 for a while. But it messes up on my server, which has rails 2.2.2 installed. The problem doesn't seem to be with the gem, but with activerecord itself. I don't know nearly enough about rails to begin trying to solve it though.
I guess I may switch to mysql for now myself, just to get things working, but that isn't really a solution to me.
7 months ago
in PostgreSQL, Ruby, and Rails: I Quit on Josh Kim dot Org
Thanks for the reply joshkim.
Well, all that's being done there in the gist to do with this is "sudo gem install postgres", which is sorta obvious. I don't think the problem is with the gem, though I've tried uninstalling and installing a few times, and tried the other ruby-pg gem too. Robby doesn't have the same error I do. The error shows up when doing a rake command to create the database, and occurs for others when trying a rake db migration command.
I think this error only shows up with the latest rails, because I've been using postgresql on my laptop with rails 2.1.1 for a while. But it messes up on my server, which has rails 2.2.2 installed. The problem doesn't seem to be with the gem, but with activerecord itself. I don't know nearly enough about rails to begin trying to solve it though.
I guess I may switch to mysql for now myself, just to get things working, but that isn't really a solution to me.
Well, all that's being done there in the gist to do with this is "sudo gem install postgres", which is sorta obvious. I don't think the problem is with the gem, though I've tried uninstalling and installing a few times, and tried the other ruby-pg gem too. Robby doesn't have the same error I do. The error shows up when doing a rake command to create the database, and occurs for others when trying a rake db migration command.
I think this error only shows up with the latest rails, because I've been using postgresql on my laptop with rails 2.1.1 for a while. But it messes up on my server, which has rails 2.2.2 installed. The problem doesn't seem to be with the gem, but with activerecord itself. I don't know nearly enough about rails to begin trying to solve it though.
I guess I may switch to mysql for now myself, just to get things working, but that isn't really a solution to me.
1 reply
joshkim
Yeah, same here. I started poking around in rails to see how I could
fix it, but I gave up after gobs and gobs of time spent on it. I also
remember it working in 2.1, because I was working on another rails
project back then... but something must have changed with 2.2.
fix it, but I gave up after gobs and gobs of time spent on it. I also
remember it working in 2.1, because I was working on another rails
project back then... but something must have changed with 2.2.
7 months ago
in PostgreSQL, Ruby, and Rails: I Quit on Josh Kim dot Org
You're wrong Carl. I saw this "solution" as well online (I'm having the same problem), and I did originally have it set to "postgres", which was wrong. But even after changing it to "postgresql", I get the same type of error. Actually, with adapter set to postgres, I get this error:
"Please install the postgres adapter: `gem install activerecord-postgres-adapter` (no such file to load -- active_record/connection_adapters/postgres_adapter)"
When it's set correct to "postgresql", I get this error:
Please install the postgresql adapter: `gem install activerecord-postgresql-adapter` (no such file to load -- pg)
I've tried both the ruby-pg and postgres gems, to no avail. Any help would be appreciated.
"Please install the postgres adapter: `gem install activerecord-postgres-adapter` (no such file to load -- active_record/connection_adapters/postgres_adapter)"
When it's set correct to "postgresql", I get this error:
Please install the postgresql adapter: `gem install activerecord-postgresql-adapter` (no such file to load -- pg)
I've tried both the ruby-pg and postgres gems, to no avail. Any help would be appreciated.
7 months ago
in PostgreSQL, Ruby, and Rails: I Quit on Josh Kim dot Org
You're wrong Carl. I saw this "solution" as well online (I'm having the same problem), and I did originally have it set to "postgres", which was wrong. But even after changing it to "postgresql", I get the same type of error. Actually, with adapter set to postgres, I get this error:
"Please install the postgres adapter: `gem install activerecord-postgres-adapter` (no such file to load -- active_record/connection_adapters/postgres_adapter)"
When it's set correct to "postgresql", I get this error:
Please install the postgresql adapter: `gem install activerecord-postgresql-adapter` (no such file to load -- pg)
I've tried both the ruby-pg and postgres gems, to no avail. Any help would be appreciated.
"Please install the postgres adapter: `gem install activerecord-postgres-adapter` (no such file to load -- active_record/connection_adapters/postgres_adapter)"
When it's set correct to "postgresql", I get this error:
Please install the postgresql adapter: `gem install activerecord-postgresql-adapter` (no such file to load -- pg)
I've tried both the ruby-pg and postgres gems, to no avail. Any help would be appreciated.
1 reply
joshkim
Did you try the gist that robby posted? I haven't had a chance to, but
it looks like it *may* work.
My solution was just to go MySQL and worry about integration with
PostgreSQL later, or maybe have that magic happen on the production
server, which has Ubuntu installed.
it looks like it *may* work.
My solution was just to go MySQL and worry about integration with
PostgreSQL later, or maybe have that magic happen on the production
server, which has Ubuntu installed.