Rails String Shortcuts and Methods

at (String)

>> "Finally, something useful!".at(6)
=> "y" 
from and to (String)

>> "Chris the Person".from(6)
=> "the Person" 
>> "Chris the Person".to(4)
=> "Chris" 

first and last (String) 


>> "Christmas Time".first
=> "C" 
>> "Christmas Time".first(5)
=> "Chris" 
>> "Christmas Time".last
=> "e" 
>> "Christmas Time".last(4)
=> "Time" 
 
 
each_char (String)

>> "Snow".each_char { |i| print i.upcase }
SNOW


starts_with? and ends_with? (String)

>> "Peanut Butter".starts_with? 'Peanut'
=> true
>> "Peanut Butter".ends_with? 'Nutter'
=> false

to_time and to_date (String)
 
>> "1985-03-13".to_time
=> Wed Mar 13 00:00:00 UTC 1985
>> "1985-03-13".to_date
=> #<Date: 4892275/2,0,2299161>

transformations! (String)

>> "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

Comments

Popular posts from this blog

Inserting and Moving elements inside Ruby Array

Interview Question on Ruby on Rails