Tag Archives: webdev
When appending to a string in Ruby use the << method instead of +=
Link
When appending to a string in Ruby use the << method instead of +=
If you come from another language, you might be tempted to use the += operator when appending to a string.
- my_str = “Marco ”
- my_str += “Polo”
It works… but there is a better way to do it : the « method.
- my_str = “Marco ”
- my_str « “Polo”
#UPDATE
I have removed my initial claim about operator precedence as it isn’t really accurate. There is a much better reason to use « instead of += when appending to a string… something I didn’t even realize (Thanks to Gregory). += will create a new String instance and will assign it to your left object. On the other hand, « will append the new string directly into your already existing left object. One could argue that += is only different than « and not better, but to me « is what you’ll want to use 99% of the time (if not 100%) when appending to a String. I have yet to see a real case where one would want to “lose” it’s existing String instance just to get a new one containing the exact same value.
You can also use the « method with arrays :
- [1,2,3] « 4 #gives [1,2,3,4]
Be careful however when using « with Fixnum/Bignum instances. With these objects, the « method will shift the bits of your integer to the left. If you want to do a summation (append style that is), you will have to use +=
Recompiling PHP5 With Bundled Support for GD on Ubuntu – CuMu.Li
Link
Recompiling PHP5 With Bundled Support for GD on Ubuntu – CuMu.Li
This helped me resolve the Drupal error when setting up a local development environment.
Showcase of Drupal sites based on the Zen Theme – groups.drupal.org
Link
Ubuntu Linux vsftpd ftp service / server install, configuration howto
Link
Setting Up name based virtual hosting with Apache
Link
Preserve Rails Ferret Index with a Symlink and Capistrano
Link
Preserve Rails Ferret Index with a Symlink and Capistrano
First, deploy your app and build your index. Once that’s done, copy your index folder to the shared folder:
cp -R /path/to/your/app/current/index /path/to/your/app/shared/
Lastly, in your deploy.rb file that you use to control your deployments, tell capistrano to create a symlink to the shared directory for your ferret index:
desc "Preserve ferret index"
task :after_update_code, :roles => [:web] do
run <<-EOF
ln -s #{shared_path}/index #{latest_release}/index
EOF
end
Ruby Accessors attr_writer, attr_reader, attr_accessor defined with examples.
Link
Ruby Accessors attr_writer, attr_reader, attr_accessor defined with examples.
Shortcut Effect attr_reader :v def v; @v; end attr_writer :v def v=(value); @v=value; end attr_accessor :v attr_reader :v; attr_writer :v attr_accessor :v, :w attr_accessor :v; attr_accessor :w
SWFUpload v2.2.0.1 Documentation
Link
Simple static pages in ruby on rails.
Link
Simple static pages in ruby on rails.
— April 2, 2008 at 09:38 PDT
Simple things should be simple, complex things should be possible. — Alan Kay
Here’s a tiny little tip for handling those boiler-plate pages that aren’t part of your app’s functionality but you usually need anyway. It’s good for setting up about, contact, copyright, etc. You can always throw those pages into /public as static html files, but if you want them to get styled with layouts, they need to be rendered as actions. This is a way to do that simply. It’s not rocket science, but I haven’t done a noob post in ages and I’m getting over a cold and I haven’t posted in too long so gimme a break.
Say you want to have a simple landing page and a few typical boiler-plate pages. Let’s start with the routes.
In config/routes.rb
map.root :controller => 'home'
map.home ':page', :controller => 'home', :action => 'show', :page => /about|contact/
In app/controllers/home_controller.rb
def index
# render the landing page
end
def show
render :action => params[:page]
end
Throw your about.html or about.html.erb and other pages into app/views/home and you’re good to go. If you’ve set up page caching, this won’t even slow your app down.
The :page => /.../
bit in the route constrains it to match only those specific urls. If you want, you can change that to a constant, like HomeController::
PAGES, so it’s easier to manage.
If you want to link to those pages, you can use the route helper methods, home_path
and home_url
link_to 'About', home_path('about')
You could always unroll the routes and have a separate route for each page, but I find this way a bit drier. But if you’d rather have a specific named route helper for each page, that’s an okay way to go. Either way, you get to use layouts in your pages, and have a nice simple way to get them rendered.