- “i got what i want”.count “i” #=> 2
- “i got what i want”.delete “i” #=> ” got what want”
- “hello”.gsub(/[aeiou]/, ‘*’) #=> “h*ll*”
- “hello”.sub(/[aeiou]/, ‘*’) #=> “h*llo”
- “hello”.tr(‘aeiou’, ‘*’) #=> “h*ll*”
- “hello”.index(‘e’) #=> 1
- “hello”.rindex(‘l’) #=> 3(last occurrence)
- “abcd”.insert(3, ‘X’) #=> “abcXd”
- ‘cat’.to_sym #=> :cat
- “hello”.ljust(20) #=> “hello “
- “hello”.ljust(20, ’1234′) #=> “hello123412341234123″
- “hello”.rjust(20, ’1234′) #=> “123412341234123hello”
- ” hello “.rstrip #=> ” hello”
- ” hello “.lstrip #=> “hello “
- ” hello “.strip #=> “hello”
- a = “cruel world”
- a.scan(/\w+/) #=> ["cruel", "world"]
- a.scan(/…/) #=> ["cru", "el ", "wor"]
- a.scan(/(…)/) #=> [["cru"], ["el "], ["wor"]]
- a.scan(/(..)(..)/) #=> [["cr", "ue"], ["l ", "wo"]]
- “1,2,,3,4,,”.split(‘,’) #=> ["1", "2", "", "3", "4"]
- “putters shoot balls”.squeeze(“m-z”) #=> “puters shot balls”
- “Finally, something useful!”.at(6) #=> “y”
- “Chris the Person”.from(6) #=> “the Person”
- “Chris the Person”.to(4) #=> “Chris”
- “Christmas Time”.first #=> “C”
- “Christmas Time”.first(5) #=> “Chris”
- “Christmas Time”.last #=> “e”
- “Christmas Time”.last(4) #=> “Time”
- “Snow”.each_char { |i| print i.upcase } #=>SNOW
- “Peanut Butter”.starts_with? ‘Peanut’ #=> true
- “Peanut Butter”.ends_with? ‘Nutter’ #=> false
- “1985-03-13″.to_time #=> Wed Mar 13 00:00:00 UTC 1985
- “1985-03-13″.to_date #=> #
- “stressed”.reverse #=> “desserts”
- “Hello”.swapcase #=> “hELLO”
- “hEllO”.upcase #=> “HELLO”
- “reindeer”.pluralize #=> “reindeers”
- “elves”.singularize #=> “elf”
- “christmas_carol”.camelize #=> “ChristmasCarol”
- “christmas_carol”.camelize(:lower) #=> “christmasCarol”
- “holiday_cheer”.titleize #=> “Holiday Cheer”
- “AdventCalendar-2006″.underscore #=> “advent_calendar_2006″
- “santa_Claus”.dasherize #=> “santa-Claus”
- “Holiday::December::Christmas”.demodulize #=> “Christmas”
- “SnowStorm”.tableize #=> “snow_storms”
- “snow_storms”.classify #=> “SnowStorm”
- “present_id”.humanize #=> “Present”
- “Present”.foreign_key #=> “present_id”
- “Cheer”.constantize #=> NameError: uninitialized constant Cheer
- “Christmas”.constantize #=> Christmas
Wednesday, 28 May 2008
Ruby String Functions
Monday, 21 January 2008
RoR Upload image in S3
Install gem aws s3
gem install aws-s3
gem install aws-s3
Before you can do anything, you must establish a connection using Base.establish_connection!. A basic connection would look something like this:
AWS::S3::Base.establish_connection!(:access_key_id => ‘abc’,
:secret_access_key => ’123′
)
:secret_access_key => ’123′
)
Buckets are containers for objects (the files you store on S3). To create a new bucket you just specify its name.
AWS::S3::Bucket.create(‘bucket_name’)
Picture upload view
<% form_tag ({:action => ‘create’},{:multipart => true}) do %>
<table>
<tr>
<td>Image </td>
<td> <%= file_field_tag “event_picture” %></td>
</tr>
</table>
<%= submit_tag “Create” %>
<% end %>
<table>
<tr>
<td>Image </td>
<td> <%= file_field_tag “event_picture” %></td>
</tr>
</table>
<%= submit_tag “Create” %>
<% end %>
Controller
include require ‘aws/s3′ in controller
include require ‘aws/s3′ in controller
def create
filename = @params['event_picture'].original_filename.gsub(/[^a-zA-Z0-9.]/,’_')
data = @params['event_picture'].read
AWS::S3::Base.establish_connection!(:access_key_id => ’123′,
:secret_access_key => ‘abc’)
AWS::S3::S3Object.store(filename,
data,
‘bucket_name’,
:content_type => ‘image/jpg’)
end
filename = @params['event_picture'].original_filename.gsub(/[^a-zA-Z0-9.]/,’_')
data = @params['event_picture'].read
AWS::S3::Base.establish_connection!(:access_key_id => ’123′,
:secret_access_key => ‘abc’)
AWS::S3::S3Object.store(filename,
data,
‘bucket_name’,
:content_type => ‘image/jpg’)
end
even you can change grants for image
policy = AWS::S3::S3Object.acl(filename, ‘bucket_name’)
policy.grants << AWS::S3::ACL::Grant.grant(:public_read)
policy.grants << AWS::S3::ACL::Grant.grant(:public_read_acp)
AWS::S3::S3Object.acl(filename, ‘bucket_name’, policy)
policy.grants << AWS::S3::ACL::Grant.grant(:public_read)
policy.grants << AWS::S3::ACL::Grant.grant(:public_read_acp)
AWS::S3::S3Object.acl(filename, ‘bucket_name’, policy)
Thursday, 13 December 2007
Display DateTime in client side Time zone
suppose you want to convert datetime in client side(browser) time zone and display it.
example take events, Events have date so that event date first you have to store it in database as GMT timezone
then write javascript function for onload event like this
example take events, Events have date so that event date first you have to store it in database as GMT timezone
then write javascript function for onload event like this
function events()
{
var currentDate = new Date();
var offset = currentDate.getTimezoneOffset().toString();
new Ajax.Updater(‘events’, ‘/events/load_events?offset=’ + offset, {asynchronous:true, evalScripts:true}); return false;
}
{
var currentDate = new Date();
var offset = currentDate.getTimezoneOffset().toString();
new Ajax.Updater(‘events’, ‘/events/load_events?offset=’ + offset, {asynchronous:true, evalScripts:true}); return false;
}
The events.rhtml will be like this
<h1>Events</h1>
<div id=”events”></div>
<div id=”events”></div>
In third line of javascript I am making an ajax call with updating “events” div tag.
This ajax will call the action “load_events” in controller “events”
This ajax will call the action “load_events” in controller “events”
events_controller.rb
def load_events
@events = Event.find(:all)
@offset = params[:offset].to_i
end
@events = Event.find(:all)
@offset = params[:offset].to_i
end
In view “load_evevnts.rhtml” calculate date with offset
<ul>
<% @events.each do |e| %>
<li><%= e.name %> on <%= (e.date – offset) %></li>
<% end %>
</ul>
<% @events.each do |e| %>
<li><%= e.name %> on <%= (e.date – offset) %></li>
<% end %>
</ul>
Wednesday, 12 December 2007
Formating date Time in Rails
You can format Date field by using strftime function
example :
Time.now()
=> Wed Dec 12 15:48:59 +0530 2007
=> Wed Dec 12 15:48:59 +0530 2007
Time.now().strftime("%d/%m/%y %H:%M")
=> "12/12/07 15:50"
=> "12/12/07 15:50"
Here's the (shortened) table for strftime.
%a weekday name.
%A weekday name (full).
%b month name.
%B month name (full).
%c date and time (locale)
%d day of month [01,31].
%H hour [00,23].
%I hour [01,12].
%j day of year [001,366].
%m month [01,12].
%M minute [00,59].
%p AM or PM
%S Second [00,61]
%U week of year (Sunday)[00,53].
w weekday [0(Sunday),6].
W week of year (Monday)[00,53].
x date (locale).
%X time (locale).
%y year [00,99].
%Y year [2000].
%Z timezone name.
%A weekday name (full).
%b month name.
%B month name (full).
%c date and time (locale)
%d day of month [01,31].
%H hour [00,23].
%I hour [01,12].
%j day of year [001,366].
%m month [01,12].
%M minute [00,59].
%p AM or PM
%S Second [00,61]
%U week of year (Sunday)[00,53].
w weekday [0(Sunday),6].
W week of year (Monday)[00,53].
x date (locale).
%X time (locale).
%y year [00,99].
%Y year [2000].
%Z timezone name.
Wednesday, 14 November 2007
HTML – Move to location(lable) in same page
suppose u have html page like this
<html>
<body>
<p>Name : Anand m Muranal</p>
<p>city : bangalore</p>
<p>state: karnataka</p>
<p>country : india</p>
</body>
</html>
<body>
<p>Name : Anand m Muranal</p>
<p>city : bangalore</p>
<p>state: karnataka</p>
<p>country : india</p>
</body>
</html>
now you want to some link which will go to city
then add some id to <p> tag of city and same id use for link, like this
then add some id to <p> tag of city and same id use for link, like this
<html>
<body>
<p>Name : Anand m Muranal</p>
<p id = “city”>city : bangalore</p>
<p>state: karnataka</p>
<p>country : india</p>
<a href=”#city”>Go to City</a>
</body>
</html>
<body>
<p>Name : Anand m Muranal</p>
<p id = “city”>city : bangalore</p>
<p>state: karnataka</p>
<p>country : india</p>
<a href=”#city”>Go to City</a>
</body>
</html>
Subscribe to:
Posts (Atom)