Showing posts with label tips. Show all posts
Showing posts with label tips. Show all posts
Wednesday, January 8, 2020
Time adjustment for DigitalOcean droplets
Hello!
I have some droplets on DigitalOcean, but I noted that the system time (in all of them) is not correctly synced with NTP servers.
The solution that worked for me is to include the following line at the end of /etc/crontab:
* * * * * root /sbin/hwclock -s
That command instructs the system to copy time from hardware (which is more precise) every minute. That solved my problem.
Saturday, October 27, 2018
Net::HTTP error sending GET request with parameters
If you're getting trouble with Net::HTTP#send_request when sending a GET request with parameters, one possible cause is that parameters must be part of path as a query string, and not as data (3rd parameter of #send_request).
For example (assuming that parameters is not empty), instead of
http = Net::HTTP.new('server.com')
http.send_request('GET', path, parameters, headers)
try:
http = Net::HTTP.new('server.com')
http.send_request('GET', path + '?' + parameters, '', headers)
For example (assuming that parameters is not empty), instead of
http = Net::HTTP.new('server.com')
http.send_request('GET', path, parameters, headers)
try:
http = Net::HTTP.new('server.com')
http.send_request('GET', path + '?' + parameters, '', headers)
Net::HTTP error 400 (bad request) with HTTPS
Hello, friends!
Yesterday I got stuck when trying to connect to an HTTPS site using Net::HTTP:
http = Net::HTTP.new('server.com', 443)
response = http.send_request('GET', path, parameters, headers)
I was getting error 400 in response.code.
What is not well documented is that when accessing a server via HTTPS protocol it's not enough to set port to 443. You must also set use_ssl to true:
Yesterday I got stuck when trying to connect to an HTTPS site using Net::HTTP:
http = Net::HTTP.new('server.com', 443)
response = http.send_request('GET', path, parameters, headers)
I was getting error 400 in response.code.
What is not well documented is that when accessing a server via HTTPS protocol it's not enough to set port to 443. You must also set use_ssl to true:
http = Net::HTTP.new('server.com', 443)
http.use_ssl = true
response = http.send_request('GET', path, parameters, headers)
I found that by seeing code at http://www.rubyinside.com/nethttp-cheat-sheet-2940.html.
http.use_ssl = true
response = http.send_request('GET', path, parameters, headers)
I found that by seeing code at http://www.rubyinside.com/nethttp-cheat-sheet-2940.html.
Wednesday, August 31, 2016
How to install mysql2 gem on Ubuntu
If you are having trouble to install the mysql2 gem, try the following:
# apt-get install build-essential ruby-dev libmysqlclient-dev
Friday, March 7, 2014
LOAD DATA INFILE tips
If you're having troubles with LOAD DATA INFILE ("Errcode 13"):
If you're having troubles with LOAD DATA LOCAL INFILE ("The used command is not allowed with this MySQL version"):
Note: LOAD DATA LOCAL INFILE uses IGNORE. If you want to know why some records were skipped, you must use LOAD DATA INFILE (without LOCAL). (Reference)
- copy the CSV file to the DB folder (/var/lib/mysql/DATABASE), and
- use the filename only, not the full path.
If you're having troubles with LOAD DATA LOCAL INFILE ("The used command is not allowed with this MySQL version"):
- temporary solution: mysql -uUSER -p --local-infile DATABASE
- permanent solution: sudo nano /etc/mysql/my.cnf and insert local-infile under [mysql]; save the file, close the editor and type sudo service mysql restart.
Note: LOAD DATA LOCAL INFILE uses IGNORE. If you want to know why some records were skipped, you must use LOAD DATA INFILE (without LOCAL). (Reference)
Sunday, September 29, 2013
Best way to install MongoDB in Ubuntu
Refer to http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/.
That's the best way.
If you try to install only thru apt-get, you can get a deprecated version.
If you try by downloading the binaries, it won't install useful things like user and service.
That's the best way.
If you try to install only thru apt-get, you can get a deprecated version.
If you try by downloading the binaries, it won't install useful things like user and service.
Wednesday, February 27, 2013
SQLite3 in Ruby 1.9: be sure your Ruby data is UTF-8
SQLite3 is not finding your records? SQLite3 is inserting binary data instead of plain-text strings? If you're using Ruby 1.9 or later, you must be sure your Ruby data is encoded as UTF-8 before submit it to SQLite3.
Text data in SQLite is UTF-8. Before Ruby 1.9, Strings had no encoding; so that was not a issue; all data get same handling. From Ruby 1.9, Strings must be encoded as UTF-8 to be compatible with SQLite data; otherwise your text will be considered binary content to SQLite, and binary content won't match plain-text content, even when they are the very same bytes.
Maybe the source of your text data encodes them as other than UTF-8. To make sure your data is UTF-8, try using String#encode or String#force_encoding before submit it to SQLite 3. Cheers!
Text data in SQLite is UTF-8. Before Ruby 1.9, Strings had no encoding; so that was not a issue; all data get same handling. From Ruby 1.9, Strings must be encoded as UTF-8 to be compatible with SQLite data; otherwise your text will be considered binary content to SQLite, and binary content won't match plain-text content, even when they are the very same bytes.
Maybe the source of your text data encodes them as other than UTF-8. To make sure your data is UTF-8, try using String#encode or String#force_encoding before submit it to SQLite 3. Cheers!
Thursday, January 31, 2013
Sequel blocking SQLite3?
I was often getting SQLite::BusyException (reraised as Sequel::DatabaseError) or Sequel::PoolTimeout exceptions when using Sequel to handle SQLite3 databases in a program with little concurrency, in a way that that exceptions are not expected (specially on reads). I rewrote my code to use the SQLite3 gem instead of Sequel, just for test, and I had no more exceptions.
There are things to be investigated, but this tip can be useful to someone as a quick (temporary?) solution.
There are things to be investigated, but this tip can be useful to someone as a quick (temporary?) solution.
Monday, July 23, 2012
Problems with permission in Apache
If you're getting Error 403 Forbidden in your site, and the owner and the filesystem's permissions of your files are correctly set, maybe you must check your Apache configuration files to see whether your DirectoryIndex directive includes the file that you want as index, and whether the Directory directive of your project's path is allowing the requests.
Labels:
apache,
permissions,
solved,
tips
Saturday, July 14, 2012
RVM + Apache + CGI Scripts
Hello!
I've configured a new server on Ubuntu 12.04 and I started to use RVM, an excellent version manager which permits to have multiple versions of Ruby installed on a single server (and many versions of the gems - see gemsets), and it makes easy to switch among them.
I've installed RVM under my user (as myself, not as root with sudo) by following the Ryan Bigg's guide, with no previous system-wide installed Ruby. So, I didn't have any Ruby under /usr/bin. My first task then was to replace the shebang line of all my CGI scripts, from
to
(The second line is needed to define the encoding of the string literals in my code, for Ruby 1.9.)
However my scripts didn't run under Apache. In the terminal I could run them (by typing ./index.cgi, for example), but not over a browser. A relevant note: in both the user is the same, i.e., the Apache user is the same as the one logged on terminal. Through php tests, I've checked the RVM enviroment was not loaded under Apache. (If anyone can solve that, please let me know.)
I saw this tip for running CGI scripts with RVM, which suggests to put the complete path of specific version of Ruby in the shebang line. That can be useful if you have scripts which run on different versions of Ruby. But that solution doesn't work for me, because my scripts must run on different machines, with different users, different ruby versions and different paths.
The solution which works for me is to put a symlink of the desired Ruby version under /usr/bin:
(Notes: sony is my username and I chose 1.8.7 because by now my scripts aren't 1.9-compliant yet.)
Therefore I didn't need to have changed the shebang lines. :) But I guess that will be useful in the future.
I've configured a new server on Ubuntu 12.04 and I started to use RVM, an excellent version manager which permits to have multiple versions of Ruby installed on a single server (and many versions of the gems - see gemsets), and it makes easy to switch among them.
I've installed RVM under my user (as myself, not as root with sudo) by following the Ryan Bigg's guide, with no previous system-wide installed Ruby. So, I didn't have any Ruby under /usr/bin. My first task then was to replace the shebang line of all my CGI scripts, from
!#/usr/bin/ruby
to
!#/usr/bin/env ruby # encoding: utf-8
(The second line is needed to define the encoding of the string literals in my code, for Ruby 1.9.)
However my scripts didn't run under Apache. In the terminal I could run them (by typing ./index.cgi, for example), but not over a browser. A relevant note: in both the user is the same, i.e., the Apache user is the same as the one logged on terminal. Through php tests, I've checked the RVM enviroment was not loaded under Apache. (If anyone can solve that, please let me know.)
I saw this tip for running CGI scripts with RVM, which suggests to put the complete path of specific version of Ruby in the shebang line. That can be useful if you have scripts which run on different versions of Ruby. But that solution doesn't work for me, because my scripts must run on different machines, with different users, different ruby versions and different paths.
The solution which works for me is to put a symlink of the desired Ruby version under /usr/bin:
sudo ln -s /home/sony/.rvm/rubies/ruby-1.8.7-p370/bin/ruby /usr/bin/ruby
(Notes: sony is my username and I chose 1.8.7 because by now my scripts aren't 1.9-compliant yet.)
Therefore I didn't need to have changed the shebang lines. :) But I guess that will be useful in the future.
Tuesday, May 22, 2012
How to UPDATE a table using data from another row
The Problem: I have a table with name, num, and diff. The unusual case here is that the diff must be updated with the difference between two sibling nums for the same name. So I'll need to know the num of the next row (for the same name) to update the current one. AND I want to do that in SQL with a single UPDATE.
Here I'll assume the rows must be sorted by num, but it would be sorted by another field, like id or timestamp.
The solution in MySQL is to use a inline temporary table to get the num of next row and associate it with current id, and use that table in the UPDATE statement.
Here is the code. Enjoy!
See this post in portuguese.
Here I'll assume the rows must be sorted by num, but it would be sorted by another field, like id or timestamp.
The solution in MySQL is to use a inline temporary table to get the num of next row and associate it with current id, and use that table in the UPDATE statement.
Here is the code. Enjoy!
-- the table used for test (MySQL syntax) CREATE TABLE `test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(30) NOT NULL, `num` int(11) NOT NULL, `diff` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ; -- some values to play on INSERT INTO `test` (`id`, `name`, `num`, `diff`) VALUES (1, 'a', 10, NULL), (2, 'b', 8, NULL), (3, 'a', 18, NULL), (4, 'a', 21, NULL), (5, 'b', 14, NULL), (6, 'a', 32, NULL), (7, 'b', 20, NULL), (8, 'b', 21, NULL); -- a select to test the ability to retrieve the desired diff value select id, name, num, (select min(num) from test where name = t1.name and num > t1.num)-num from test t1 where diff is null order by name, num; -- updating test with the calculated diff using another row on same table. update test t2, ( select id, (select min(num) from test where name = t1.name and num > t1.num)-num as diff from test t1 ) t3 set t2.diff = t3.diff where t2.id = t3.id and t2.diff is null
-- an alternative query to get more than one column from t2
select t1.id, t1.name, t1.num, t2.name, t2.num from test t1, test t2 where t2.id = (select id from test where name = t1.aome and num > t1.num order by num limit 1) order by t1.name, t1.num
See this post in portuguese.
Tuesday, March 13, 2012
Capitalized methods names
You can define methods with capitalized names:
At call time, to avoid Ruby to interpret them as constants, you must make clear that you are using them as functions, by using parenthesis or parameters:
Sequel uses this feature to define methods named String, Integer, etc. for creating/altering tables.
#!/usr/bin/ruby def Foo puts 'foo' end def Bar x puts x end
At call time, to avoid Ruby to interpret them as constants, you must make clear that you are using them as functions, by using parenthesis or parameters:
Foo() #=> 'foo' Bar 3 #=> 3 Foo #=> Error: not initialized constant
Sequel uses this feature to define methods named String, Integer, etc. for creating/altering tables.
Tuesday, March 6, 2012
How to install sqlite3-ruby gem on linux
If you are having trouble to install the sqlite3-ruby gem, try the following:
In systems with apt-get:
In systems with yum:
In systems with apt-get:
# apt-get install libsqlite3-devIn systems with yum:
# yum install sqlite-devel
How to install bson_ext gem on linux
MongoDB requires the bson_ext gem to increase performance. However, it's common to be not ready to install it.
In ubuntu (10.04 32bit) I had to run:
In systems with yum I had to run:
Finally in both systems I could run, with no errors:
In ubuntu (10.04 32bit) I had to run:
# apt-get install ruby1.8-devIn systems with yum I had to run:
# yum install gcc
# yum install make
# yum install ruby-devel
Finally in both systems I could run, with no errors:
# gem install bson_ext
A little bit on require behavior
require filename will:- return true when it finds filename;
- return false when it already loaded filename;
- raise LoadError when it doesn't find filename.
Friday, February 17, 2012
RewriteRule running twice
Sometimes it seems that
The truth is: it really runs twice (or even more times)! But only when URL is changed.
The [L] flag stops the running of rules following it, but if URL is changed, the new URL will be parsed again from the beginning.
There are many solutions to that (e.g., by using
RewriteRule is running twice, even we use the [L] flag.The truth is: it really runs twice (or even more times)! But only when URL is changed.
The [L] flag stops the running of rules following it, but if URL is changed, the new URL will be parsed again from the beginning.
There are many solutions to that (e.g., by using
RewriteCond), but the one I used is to put, as first rule, one to tell the RewriteEngine to do nothing if the URL is what I want:
RewriteEngine on
# index is the last rule - is what I want, so doesn't change anything
# and go to it (thank's to [L])!
RewriteRule ^index.php$ - [L,QSA]
# get user id - URL changed, so [L] will cause the new URL to be reparsed
# - and so it will be matched on the above rule.
RewriteRule ^user/(.*)$ index.php?user=$1 [L,QSA]
# in case of user/..., following rules don't apply,
# since the above rule has [L]
# ...
See this post in Portuguese.
Labels:
.htaccess,
mod_rewrite,
tips
Tuesday, March 29, 2011
Two ways to simplify Array blocks in Ruby
1. If you have a block of the form
That works for other Array methods, like
2. If you have a block of the form
See more on it.
{ |v| v.mtd }:ary.each { |v| v.mtd } # normal way
ary.each(&:mtd) # simpler way
# Example:
ary = %w(abc def ghi jkl)
ary.each(&:upcase!) # same as ary.each { |v| v.upcase! }
That works for other Array methods, like
map. More on it.2. If you have a block of the form
{ |v| mtd v }, and the method mtd does the same thing with all parameters:ary.each { |v| mtd v } # normal way
mtd *ary # simpler way
#Example:
ary = %w(abc def ghi jkl)
puts *ary # same as ary.each { |v| puts v }
See more on it.
Subscribe to:
Posts (Atom)
