Pages

Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Thursday, September 12, 2013

Ruby Application Server - Installation notes for Nginx and Passenger

Hi!

Here's how to install Passenger and Nginx. You will get a very fast Ruby application server.

Compile nginx :
tar -xzvf nginx-1.3.14.tar.gz
cd nginx-1.3.14
./configure --prefix=/etc/nginx --with-http_ssl_module

cd /etc/init.d/
ln -snf /etc/nginx/sbin/nginx

Compile passenger :

cd /usr/local/ruby/bin
./passenger-install-nginx-module

 1. Yes: download, compile and install Nginx for me. (recommended)
    The easiest way to get started. A stock Nginx 1.0.6 with Passenger
    support, but with no other additional third party modules, will be
    installed for you to a directory of your choice.

 2. No: I want to customize my Nginx installation. (for advanced users)
    Choose this if you want to compile Nginx with more third party modules
    besides Passenger, or if you need to pass additional options to Nginx's
    'configure' script. This installer will  1) ask you for the location of
    the Nginx source code,  2) run the 'configure' script according to your
    instructions, and  3) run 'make install'.

Whichever you choose, if you already have an existing Nginx configuration file,
then it will be preserved.

Enter your choice (1 or 2) or press Ctrl-C to abort: 2

The next step is to point passenger to the source directory. Is this example, the source is in my home folder so it’s -> /home/xxx/nginx-1.3.14.

Than point the installation of nginx to /etc/nginx

Passenger is now installed. Here’s what your nginx config should look like this:
____________________________________________
worker_processes  2;

error_log  logs/error.log  debug;

#pid        logs/nginx.pid;

events {
    worker_connections  2000;
}

http {
    passenger_root /usr/local/ruby-1.8.7-p352/lib/ruby/gems/1.8/gems/passenger-3.0.9;
    passenger_ruby /usr/local/ruby-1.8.7-p352/bin/ruby;
    passenger_max_pool_size 3;

    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

  server {
     listen 8666;
     server_name localhost;
    
     root /data/opt/local/ruby/myapp/public/;   # <--- be sure to point to 'public'!
     passenger_enabled on;
     passenger_use_global_queue on;

     auth_basic "myapp Access"; # <--- For password auth
     auth_basic_user_file /home/myapp/passwd;
    
   }

     #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
}
______________________________________________________
Start:
/etc/init.d/nginx

Stop :
/etc/init.d/nginx -s stop

Thursday, October 25, 2012

invalid multibyte escape (ArgumentError)

Hi! After updating from Ruby 1.8.7 to Ruby 1.9.3, I've got errors with the gem validate_as_email at line
pattern = /\A#{addr_spec}\z/
Error Message:
D:/Ruby193/lib/ruby/gems/1.9.1/gems/validates_as_email-0.5.1/lib/validates_as_email.rb:28:in `': invalid multibyte escape (ArgumentError)
 from D:/Ruby193/lib/ruby/gems/1.9.1/gems/validates_as_email-0.5.1/lib/validates_as_email.rb:13:in `'
 from D:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.2.1/lib/bundler/runtime.rb:68:in `require'
 from D:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.2.1/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
 from D:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.2.1/lib/bundler/runtime.rb:66:in `each'
 from D:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.2.1/lib/bundler/runtime.rb:66:in `block in require'
 from D:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.2.1/lib/bundler/runtime.rb:55:in `each'
 from D:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.2.1/lib/bundler/runtime.rb:55:in `require'
 from D:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.2.1/lib/bundler.rb:128:in `require'
 from H:/BRPB2B/config/application.rb:7:in `'
 from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands.rb:53:in `require'
 from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands.rb:53:in `block in '
 from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands.rb:50:in `tap'
 from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands.rb:50:in `'
 from H:/BRPB2B/script/rails:6:in `require'
 from H:/BRPB2B/script/rails:6:in `'
 from -e:1:in `load'
 from -e:1:in `
'
The problem is that using /my_regex/ is not valid anymore. You must use instead.
pattern =  Regexp.new "\A#{addr_spec}\z", nil, 'n'

Tuesday, October 16, 2012

Remove all Ruby Gems in one command


  • Open the command manager
  • Type powershell
  • Type gem list | %{$_.split(' ')[0]} | %{gem uninstall -Iax $_ }

Tuesday, December 6, 2011

Ruby on Rails -=- Formatting Rails text_field_tag

Hi!

If you want to format a Rails text_field tag, simply do the following:

   <%= f.text_field :my_var, :value=> my_function(f.object.my_var) %> 

Where my_var is my variable and where my_function is a function where I can apply a format. You can apply directly the format that you want in :value instead of using a function

Ruby on Rails -=- Removing trailing zero

Hi! With RoR I'm always getting trailing zero for numeric fields. Example, 23 will be 23.0 In order to round theses values, simply do the following
"%g" % my_value
Where my_value is my variable

Thursday, April 7, 2011

Ruby on Rails -=- EZ_Where plugin

Here's a good plugin used to build where conditions for Rails without SQL. It is called ez_where plugin for rails.

You can download it at https://rubyforge.org/projects/ez-where/

Here's the description of the project as shown on one of the creator's blog (see: http://brainspl.at/articles/tag/conditions)

This plugin is meant to be used as a nice ruby like syntax for creating the :conditions part of an ActiveRecord::Base.find. We also add the ActiveRecord::Base.ez_find method. This method takes a block to simplify single and multi table queries.

articles = Article.ez_find(:all, :include => :author) do |article, author|
  article.title =~ "%Foo Title%"
  author.any do
    name == 'Ezra'
    name == 'Fab'
  end 
end
This will produce :conditions => ["article.title LIKE ? AND
(authors.name = ? OR authors.name = ?)",
"%Foo Title%", "Ezra", "Fab"]

Basically here is the breakdown of how we map ruby operators
to SQL operators:

foo == 'bar'           #=> ["foo = ?", 'bar']
foo =~ '%bar'          #=> ["foo LIKE ?", '%bar']
foo <=> (1..5)         #=> ["foo BETWEEN ? AND ?", 1, 5]
id === [1, 2, 3, 5, 8] #=> ["id IN(?)", [1, 2, 3, 5, 8]]
<, >, >=, <= et all will just work like you expect.

There is also the ability to create the conditions in stages so
you can build up a query:

cond = Caboose::EZ::Condition.new do
  foo == 'bar'
  baz <=> (1..5)
  id === [1, 2, 3, 5, 8]
end
 
@result = Model.find(:all, :conditions=> cond.to_sql)


#=> ["foo = ? AND baz BETWEEN ? AND ? AND id IN (?)",
"bar", 1, 5, [1, 2, 3, 5, 8]]


You can even do nested sub conditions. condition will use AND
by default in the sub condition:
cond = Caboose::EZ::Condition.new :my_table do
  foo == 'bar'
  baz <=> (1..5)
  id === [1, 2, 3, 5, 8]
  condition :my_other_table do
    fiz =~ '%faz%'
  end
end

@result = Model.find(:all, :conditions=> cond.to_sql)

#=> ["my_table.foo = ? AND my_table.baz BETWEEN ? AND ?
AND my_table.id IN (?) AND (my_other_table.fiz LIKE ?)",
"bar", 1, 5, [1, 2, 3, 5, 8], "%faz%"]

You can also build multiple Condition objects and join
them together for one monster find:

cond_a = Caboose::EZ::Condition.new :my_table do
  foo == 'bar'
  condition :my_other_table do
    id === [1, 3, 8]
    foo == 'other bar'
    fiz =~ '%faz%'
  end
end

#=> ["my_table.foo = ? AND (my_other_table.id IN (?) AND my_other_table.foo = ?
AND my_other_table.fiz LIKE ?)", "bar", [1, 3, 8], "other bar", "%faz%"]

cond_b = Caboose::EZ::Condition.new :my_table do
  active == true
  archived == false
end

#=> ["my_table.active = ? AND my_table.archived = ?", true, false]

composed_cond = Caboose::EZ::Condition.new
composed_cond << cond_a
composed_cond << cond_b
composed_cond << 'fuzz IS NULL'

@result = Model.find(:all, :conditions => composed_cond.to_sql)

#=> ["(my_table.foo = ? AND (my_other_table.id IN (?) AND my_other_table.foo = ?
AND my_other_table.fiz LIKE ?)) AND (my_table.active = ? AND my_table.archived = ?)
AND fuzz IS NULL", "bar", [1, 3, 8], "other bar", "%faz%", true, false]

You can compose a new condition from different sources:

ar_instance = Author.find(1)

other_cond = Caboose::EZ::Condition.new :my_table do 
  foo == 'bar'; baz == 'buzz'
end

cond = Caboose::EZ::Condition.new
# another Condition
cond.append other_cond
# an array in AR condition format
cond.append ['baz = ? AND bar IS NOT NULL', 'fuzz'], :or
# a raw SQL string
cond.append 'biz IS NULL'
# an Active Record instance from DB or as Value Object
cond.append ar_instance

#(append is aliased to << because of syntax issues 
involving multiple args like :or)

@result = Model.find(:all, :conditions=> cond.to_sql)

#=> ["(my_table.foo = ? AND my_table.baz = ?) OR (baz = ? AND bar IS NOT NULL)
AND biz IS NULL AND authors.id = ?", "bar", "buzz", "fuzz", 1]

OK there is also other options for doing subconditions. OR is
aliased to any and any creates a subcondition that uses OR to
join the sub conditions:

cond = Caboose::EZ::Condition.new :my_table do
  foo == 'bar'
  any :my_other_table do
    baz === ['fizz', 'fuzz']
    biz == 'boz'
  end
end

@result = Model.find(:all, :conditions=> cond.to_sql)

#=> ["my_table.foo = ? AND (my_other_table.baz IN (?)
OR my_other_table.biz = ?)",
"bar", ["fizz", "fuzz"], "boz"]

OK lets look a bit more at ez_find with a few more complex queries:

# all articles written by Ezra. Here you can use a normal AR object
# in the conditions
# session[:user_id] = 2
ezra = Author.find(session[:user_id])    
@articles = Article.ez_find(:all, :include => :author) do |article, author|
  author << ezra # use AR instance to add condition; uses PK value if set: author.id = ezra.id
end 

#=>["(authors.id = ?)", 2]

# all articles written by Ezra, where he himself responds in comments
@articles = Article.ez_find(:all, :include => [:author, :comments]) do |article, author, comment|
  article.author_id == ezra.id
  comment.author_id == ezra.id   
end

#=>["(articles.author_id = ?) AND (comments.author_id = ?)", 2, 2]

# any articles written by Fab or Ezra
@articles = Article.ez_find(:all, :include => :author) do |article, author|
  author.name === ['Fab', 'Ezra']   
end
#=>["(authors.name IN (?))", ["Fab", "Ezra"]]

# any articles written by Fab or Ezra, using subcondition
@articles = Article.ez_find(:all, :include => :author) do |article, author|
  author.any do
    name == 'Ezra'
    name == 'Fab'
  end  
end

#=>["(authors.name = ? OR authors.name = ?)", "Ezra", "Fab"]

# any articles written by or commented on by Fab, using subcondition
@articles = Article.ez_find(:all, :include => [:author, :comments]) do |article, author, comment|
  article.sub { author_id == 1 }
  comment.outer = :or # set :outer for the comment condition, since it defaults to :and
  comment.sub { author_id == 1 }       
end

#=>["(articles.author_id = ?) OR (comments.author_id = ?)", 1, 1]

@articles = Article.ez_find(:all, :include => [:author, :comments],
                           :outer => { :comments => :or }, 
                           :inner => { :article => :or}) do |article, author, comment|
  article.sub { author_id == 1; author_id == 2 }
  comment.sub { author_id == 1 } 
end

["(articles.author_id = ? OR articles.author_id = ?) OR (comments.author_id = ?)", 1, 2, 1]

And finally you can use any and all with ez_condition like this:

cond = Article.ez_condition { active == true; archived == false }
cond.all { body =~ '%intro%'; body =~ '%demo%' }
cond.any { title =~ '%article%'; title =~ '%first%' }

#=> ["articles.active = ? AND articles.archived = ?
AND (articles.body LIKE ? AND articles.body LIKE ?)
AND (articles.title LIKE ? OR articles.title LIKE ?)",
true, false, "%intro%", "%demo%", "%article%", "%first%"]

Monday, April 4, 2011

Ruby -=- Using environment variable

Hi

With Ruby, You can use environment variable by using the function.

ENV['JAVA_HOME']

The variable must be defined in the operating system.


Ruby have many places to define environment variable within the code. You can put environment variable common across environment in [RubyProject]/config/environment.rb

Also, if you want to define an environment variable for a specific environment, you can set the variable in [RubyProject]/config/environments/production.rb or test.rb or development.rb...

The variable defined in the project can be use directly. For example, if I put MY_VAR = 3 in the file environment.rb, I will use the variable directly by calling MY_VAR in the code. For example


p MY_VAR

Monday, August 23, 2010

Ruby get time in milliseconds

Hi

If you want to get the current time in milliseconds in Ruby, type this following Ruby code.

Thanks to Antoine that show me the mistake ;-)

Time.now.to_f*1000

Tuesday, August 3, 2010

Ruby -=- undefined method `[]' for #Enumerable::Enumerator

Hi!

When I upgrade to Ruby 1.8.7 I've got some problems. I'm getting the error message undefined method `[]' for #Enumerable::Enumerator.

The problem occurs with the version 1.8.7 of Ruby and it's supposed to be corrected in the version 9. To fix this problem you can either downgrade to Ruby 1.8.6 OR put this piece of code in your environnement.rb within initializer block:

unless '1.9'.respond_to?(:force_encoding)
  String.class_eval do
    begin
      remove_method :chars
    rescue NameError
      # OK
    end
  end
end 

Friday, January 29, 2010

Ruby - Can't install gems on Leopard

After installing Mac OS X Leopard on my MacBook, I'm unable to install gems on my machine. I need to update the system.


sudo gem update --system


After this command, I'm able to install gems without errors.