Ruby Basic


String Method in Rails

To change case:

capitalize – first character to upper, rest to lower
downcase – all to lower case
swapcase – changes the case of all letters
upcase – all to upper case

To rejustify:

center – add white space padding to center string
ljust – pads string, left justified
rjust – pads string, right justified

To trim:

chop – remove last character
chomp – remove trailing line separators
squeeze – reduces successive equal characters to singles
strip – deletes leading and trailing white space

To examine:

count – return a count of matches
empty? – returns true if empty
include? – is a specified target string present in the source?
index – return the position of one string in another
length or size – return the length of a string
rindex – returns the last position of one string in another
slice – returns a partial string

To encode and alter:

crypt – password encryption
delete – delete an intersection
dump – adds extra \ characters to escape specials
hex – takes string as hex digits and returns number
next or succ – successive or next string (eg ba -> bb)
oct – take string as octal digits and returns number
replace – replace one string with another
reverse – turns the string around
slice! – DELETES a partial string and returns the part deleted
split – returns an array of partial strings exploded at separator
sum – returns a checksum of the string
to_f and to_i – return string converted to float and integer
tr – to map all occurrences of specified char(s) to other char(s)
tr_s – as tr, then squeeze out resultant duplicates
unpack – to extract from a string into an array using a template

To iterate:

each – process each character in turn
each_line – process each line in a string
each_byte – process each byte in turn
upto – iterate through successive strings


Basic Ruby Example


This is an example for Yield condition
def test
   puts "You are in the method"
   yield
   puts "You are again back to the method"
   yield
end
test {puts "You are in the block"}
#~ def test
   #~ yield 5
   #~ puts "You are in the method test"
   #~ yield 100
#~ end
#~ test {|i| puts "You are in the block #{i}"}



This is an example for While condition
$i = 0;
$num = 5;
while $i < $num  do
   puts("Inside the loop i = #$i" );
   $i +=1;
end



This is an example for Until condition
$i = 0;
$num = 5;
until $i > $num  do
   puts("Inside the loop i = #$i" );
   $i +=1;
end
#~ If an until modifier follows a begin statement with no rescue or ensure clauses, code is executed once before conditional is evaluated.


This is an example for unless condition
x=1
unless x>2
   puts "x is less than 2"
 else
  puts "x is greater than 2"
end



This is an example for retry condition
#~ begin
   #~ do_something # exception raised
#~ rescue
   #~ # handles error
   #~ retry  # restart from beginning
#~ end
for i in 1..5
   retry if  i > 2
   puts "Value of local variable is #{i}"
end



This is an example for Redo condition
for i in 0..5
   if i < 2 then
      puts "Value of local variable is #{i}"
      redo
   end
end
#~ Restarts this iteration of the most internal loop, without checking loop condition. Restarts yield or call if called within a block



This is an example for Next condition
for i in 0..5
   if i < 2 then
      next
   end
   puts "Value of local variable is #{i}"
end



This is an example for FOR condition
for i in 0..5
   puts "Value of local variable is #{i}"
end



This is an example for do with while condition
$i = 0;
$num = 5;
begin
   puts("Inside the loop i = #$i" );
   $i +=1;
end while $i < $num



This is an example for do with until condition
$i = 0;
$num = 5;
begin
   puts("Inside the loop i = #$i" );
   $i +=1;
end until $i > $num
 #~ If an until modifier follows a begin statement with no rescue or ensure clauses, code is executed once before conditional is evaluated.



This is an example for def method with return
for def test
i = 100
j = 200
k = 300
return i, j, k
end
var = test
puts var


This is an example for def method with parameter
def sample (*test)
puts "The number of parameters is #{test.length}"
for i in 0...test.length
puts "The parameters are #{test[i]}"
end
end
sample "Zara", "6", "F"
sample "Mac", "36", "M", "MCA"


This is an example for def method
def test(a1="Ruby", a2="Perl")
puts "The programming language is #{a1}"
puts "The programming language is #{a2}"
end
test "C", "C++"
test


This is an example for case statement
$age = 5
case $age
when 0 .. 2
puts "baby"
when 3 .. 6
puts "little child"
when 7 .. 12
puts "child"
when 13 .. 18
puts "youth"
else
puts "adult"
end


This is an example for break statement
for i in 0..5
if i > 2 then
break
end
puts "Value of local variable is #{i}"
end


This is an example for IF statement
x=1
if x > 2
puts "x is greater than 2"
elsif x <= 2 and x!=0
puts "x is 1"
else
puts "I can't guess the number"
end

FIXNUM Documentation in RUBY
Modulus
Puts 5%3 gives you the reminder.
Result =2.
Puts 3&5
&- Binary and operator 3-011 & 5-101
Result = 1-001
Puts 5*3 gives product values 15
** puts 5**3 result will be power values 5^3=125
+ for addition
-For Subraction
-@ negative fix
/ for division
for less than
<< for shift fix left register location puts 2<<1
Then 2=0010 shift register 0100 result is 4
Puts 5<<2, 5=101 result 10100=20
>> for shift fix right register location puts 2<<1
Then 2=0010 shift register 0001 result is 1
Puts 5>>1, 5=0101 result 0010=2
__id__  => This helps you shift register bits. Puts 5.__id__ => 11 & puts 3.__id__ => 7
 In the above example 5=0101 for 11=>1011 in the above example, add a bit “1” at the end. Then the value changes to 11 for 5.

<=    puts 3<=5 => true   &   <=     puts 5<=3 => false
<=>puts 5 <=> 3 => 1 & puts 5 <=> 5 => 0 & puts 5 <=> 7 => -1
== provides equal values puts 5==3 => false & puts 5==5 => true
===  puts 5===3 => false & puts 5===5 => true
=~  This is for Pattern matching (Pattern Match—Overridden by descendents (notably Regexp and String) to provide meaningful pattern-match semantics.)
>   greater than symbol puts 5>3 => true puts 3>5 => False which is similar to >= symbol also
[]  Bit Reference—Returns the nth bit in the binary representation of fix, where fix[0] is the least significant bit.
puts 5^3 => 6Bitwise EXCLUSIVE OR.  5= 0101 + 3=0011 then make Or operation add 1 for the output.
_ _send_     => class Klass   def hello(*args)   “Hello ” + args.join(‘ ‘) end  end
k = Klass.new k.send :hello, “gentle”, “readers” #=> “Hello gentle readers”
abs puts 5.abs => 5   puts -5.abs => 5  It gives the value without consider about plus or minus
between?    puts 5.between?(3,8) => true,,  puts 5.between?(3,4) => false,, 5 is placed between 3 to 8,   5 is not placed in between 3 to 4.

.eql?  1 == 1.0 #=> true, 1.eql?(1.0) #=> false, (1.0).eql?(1.0) #=> true     Returns true if num and numeric are the same type and have equal values.
Extend one module with another module
Floor
String: Changing a section of string.
A= “Puts jegan”……. a.gsub(“jeg”, “Raj”)………….result => a=> “Puts Rajan”
String for the delimiter can be used as
A= “asdfASDF” or A=%{asdfasdfas} or a=<<JEG asdfasdf JEG
myString = "Welcome to PHP Essentials!"
=> "Welcome to PHP Essentials!"
myString.gsub("PHP", "Ruby")
=> "Welcome to Ruby Essentials!"
myString.replace "Goodbye to PHP!"
=> "Goodbye to PHP!"

No comments:

Post a Comment