Interchange characters in Ruby string


Hi friends,

In ruby we can interchange characters in a string easily by using “.tr” methods this is a good useful method to perform some replace actions on strings.

Returns a copy of str with the characters in from_str replaced by the corresponding characters in to_str.

Here are few examples on this method:

This is the Example for Exchange the strings..tr ‘aeiou’, ‘_
=> “Th_s _s th_ Ex_mpl_ f_r Exch_ng_ th_ str_ngs.
‘This is the Example for Exchange the strings.’.tr ‘a-k’, ‘*’
=> “T**s *s t** Ex*mpl* *or Ex***n** t** str*n*s.”

‘This is the Example for Exchange the strings.’.tr ‘a-z’, ‘s’
=> “Tsss ss sss Essssss sss Esssssss sss sssssss.”

‘This is the Example for Exchange the strings.’.tr ‘aeiou’, ‘5’
=> “Th5s 5s th5 Ex5mpl5 f5r Exch5ng5 th5 str5ngs.”

Advertisements

தமிழ்நாடு அரசு வழங்கும் இலவச கணினியில் லினக்ஸ் (அ) விண்டாஸ்? அரசு இதை கவனிக்குமா???


வணக்கம்,

பள்ளி மற்றும் கல்லூரி மாணவர்களுக்கு தமிழ்நாடு அரசு இலவசம் மடிக்கணினி வழங்கவிருப்பது பெரும் மகிழ்ச்சி அளிக்கும் செய்தி. அரசின் இந்த திட்டத்தின் மூலம் மாணவர்கள் தகவல் தொழில்நுட்பம் மற்றும் அதன் சம்பத்தப்பட்ட வளர்ந்துவரும் புது தொழில்நுட்பங்களை கற்றுத்தேற எதுவாக இருக்கும். அதே சமயம் இது கணிணி அறிவை மாணவர்கள் மூலம் மக்களுக்கும் ஏற்படுத்தும் என்பது மேலும் போற்ற கூடிய ஒன்றாகும்.

Read more >>

Find Web browser user agent using rails 3


Hi friends,

In rails 3 there is a easy way to Detect the web browser user agent . 🙂

By adding the one line in the controller page, we can get the UA.(@user_agent = request.env[‘HTTP_USER_AGENT’])

In /app/controllers/users_controller.rb

def index
 @users = User.all
  @user_agent = request.env['HTTP_USER_AGENT']

In /app/views/users/index.html.erb

<%= @user_agent %>

That’s All !!!  🙂 🙂 🙂

GitHub Link: https://github.com/thasuresh/find_user_agent

Live Example On Heroku: http://findua.heroku.com/

Ex snaps for various Web browsers:

This slideshow requires JavaScript.

Multiple File uploading in Rails3 Application using “Papercilp”


Hi friends,

Lets see how to upload multiple files in a rails 3 application.

First create a new rails project and go inside of the project directory.

$ rails new fileuploading
$ cd fileuploading

Add the Gms in  following “Gemfile”

gem ‘nifty-generators’
gem ‘paperclip’


source 'http://rubygems.org'

gem 'rails', '3.0.3'
gem 'nifty-generators'
gem 'paperclip'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'.....

And  bundle install it.

Now we have to create a scaffold. Do a following commands.

 $ rails g nifty:scaffold Post title:string
 $ rails g nifty:layout

It asks a Permission to Overwrite the layout file. just press “y”.  It shoes like,
Overwrite /home/suresh/deal_projects/paperclip-test/app/views/layouts/application.html.erb? (enter “h” for help) [Ynaqdh]

Again migrate with db.

 $ rake db:migrate
 $ rm public/index.html
Then, Add the following line in  your /config/routes.rb , root :to => “posts#index”
Then, create a model called “Asset”
$ rails g model Asset asset_file_name:string asset_content_type:string asset_file_size:integer asset_upload_at:datetime post_id:integer
$ rake db:migrate

Now in model folder, there is a file called “asset.rb”. Add the following lines into this file.

class Asset < ActiveRecord::Base
belongs_to  :post
has_attached_file  :asset, :styles => {  :large => "640*480",  :medium => "300*300>",  :thumb => "100*100>" }

end

And add the following lines into “posts.rb” file

class Post < ActiveRecord::Base
attr_accessible :title, :content,  :assets_attributes
has_many  :assets
accepts_nested_attributes_for  :assets,  :allow_destroy  => true
end

Edit app/controller/posts_controller.rb (Note the new and edit part)


def new
@post = Post.new
5.times { @post.assets.build }

end

def edit
@post = Post.find(params[:id])
5.times { @post.assets.build }
end

Edit and add the following lines in your app/view/post/_form.html.erb

<%= form_for @post, :html => { :multipart => true } do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>

<% f.fields_for :assets do |asset_fields| %>

<% if asset_fields.object.new_record? %>
<p>
<%= asset_fields.file_field :asset %>
</p>

<% end %>
<% end %>

<% f.fields_for :assets do |asset_fields| %>
<%unless asset_fields.object.new_record? %>

<p>

<%= link_to image_tag(asset_fields.object.asset.url(:thumb)), asset_fields.object.asset.url(:original) %>

<%= asset_fields.check_box :_destroy %>
</p>

<% end %>
<% end %>

<p><%= f.submit %></p>
<% end %>

That’s All .

In tour terminal run ” rails s” command and open the “http://localhost:3000” in your browser. 🙂

Selecting the files

Uploaded into project 🙂

GitHub Link:  https://github.com/thasuresh/fileuploading
 
My hearty thanks to: http://www.emersonlackey.com/article/paperclip-with-rails-3
 
 

Linux 20-years celebration video in tamil


Hi friends,

At 1991, Linus Torvalds, released his Kernel as open source. After that, the Linux Revolution happened.

To celebrate the 20 years of Linux, The Linux Foundation, is doing various activities.
Explore it here. http://www.linuxfoundation.org/20th
It released an animation video.

My Brother T.Shrinivasan wanted to translate the video in tamil. So he asked his friend to transcribe the audio as English text. He then also asked me to  convert the English text into Tamil text.

Here the original English version video:

http://www.youtube.com/watch?v=5ocq6_3-nEw

Here the tamil version of the above video:
http://www.youtube.com/watch?v=gZKI0u18iLQ

More info: http://goinggnu.wordpress.com/2011/05/14/celebrating-20-years-of-linux-a-tamil-video/

🙂 🙂 🙂

Google maps in Rails3 Application — Sample Application


Hi friends,

(New update: For new version(0.9.1) check Jay’s comment below)

(Update: this tutorial works only on gem version 0.7.7 and won’t work on latest gem version 0.8.7)

Using a gmaps4rails gem to display a map in our application

create a rails application using,

$ rails new gmaps

$ cd gmaps

Add this Gem in your Gemfile

gem ‘gmaps4rails’, ‘0.7.7’

Now bundle it ($ bundle install )and migrate with db ( $ rake db:migrate)

Create a scaffold using,

$ rails g scaffold Location name:string address:string longitude:float latitude:float

remove the longitude and latitude fields in _form.html.erb

After that, add this in app/view/layout

<%= yield :head %> (in your header) <%= yield :scripts %> (in your footer)

/app/views/layouts/application.html.erb

<html>
<head>
  <title>Googlemap</title>
  <%= stylesheet_link_tag :all %>
  <%= javascript_include_tag :defaults %>
  <%= csrf_meta_tag %>
  <%= yield :head %>
</head>
<body>
<%= yield %>
<%= yield :scripts %>
</body>
</html>

In model /app/models/location.rb to add

class Location < ActiveRecord::Base
 acts_as_gmappable
      def gmaps4rails_address
          address
      end
       def gmaps4rails_infowindow
         "<h4>#{name}</h4>" << "<h4>#{address}</h4>"
     end
end

add the line in controller /app/controllers/locations_controller.rb

def index
    @locations = Location.all
    @json = Location.all.to_gmaps4rails
    ..........

In your view app/views/locations/index.html.erb to add a last line as

<%= gmaps4rails(@json) %>

then start the server(http://localhost:3000/locations) to put the name and address, longitude and latitude as set automatically, then will be show the markers as corresponding address and click the marker icon.it will shows infowindow as name and address

That’s it!! If you have any questions please feel free to contact me. I will be happy to help where I can.  🙂

Map View

My Sample Application:

http://gmaps4rails.heroku.com

GitHub:

https://github.com/thasuresh/gmaps_project

Ref Link:

http://www.youtube.com/user/TheApneadiving