Monday, 13 August 2012

Few Other Ruby examples!:)

In case you have a function inside another function then when you call the uter function the inner function is defined.Again when you call the inner function it gets invoked.If both the inner and outer function has the same name,then all calls other than the first call points to the second inner function.


def one
    puts "one"
    def one
        puts "two"
    end
end

one
one
one
op:one two two two....

This is another function with define_method inside a "def" and allows dynamic multiplication..

class Mul
def self.fn(other)
    define_method "mul_#{other}" do
         puts 5*other
    end
end
end
val=3

m=Mul.new
Mul::fn(3)----------------------**
puts "before"
m.mul_3--------------------------*
puts "after"

op:
before
15
after

In the above example you would have noted that though it is dynamic you would hve to specify the changed value in * and **.To avoid this you create all instance of function from mul_1 to mul_10..Check the foll pgm ...



class Multiplier
  def self.fn(n)
    define_method "mul_#{n}" do |x|
    puts  x*n
  end
end
  
for i in (1..10)
fn(i)
end

end


m = Multiplier.new


 m.mul_3(5)----*
 m.mul_7(5)
Here in each case u need to call only once.
There is another important function "method_missing" that is called when undefined methods are called.You can override this method to perform  the requires task you want.Now we will override the method_missing function and perform the same above task.

class Check

def method_missing(name,*args)
    puts "im n missing method function"
    self.class.send(:define_method ,name) do |*args|
        puts "im inside define_method"
        #name1=name[name.length-1]
         name1=name.to_s
         
          name2=name1.split("_")
           value=name2[1]         
          return value.to_i*args[0]
          

    end

self.send(name,*args)
end

end

c1=Check.new
puts c1.mul_4(10)
puts c1.mul_4(6)
puts c1.mul_2(60)

In the above method we have a great advantage.You define only the function that is called.Not all the function as in the previous case.

Inject Function:
     puts   [1, 2, 3, 4].inject { |result,a|; result +a }
This returns the sum of the values of the array.


Defining and Using the attr-accessor:

class Animal
  


    define_method attr_name do
      instance_variable_get("@#{attr_name}")
    end
    
    define_method "#{attr_name}=" do |new_val|
      instance_variable_set("@#{attr_name}", new_val)
    end
    
  end
  my_attr_accessor :name
  my_attr_accessor :age
end

a = Animal.new
a.name = "Rona"
a.age=100
puts a.name
puts a.age



op.:
Rona
100


Code for sending mail using "gmail" in ruby:


require 'net/smtp'

YourDomain = 'smtp.gmail.com'
YourAccountName = 'from####@gmail.com'
YourPassword = '######'

FromAddress = 'from###@gmail.com'
ToAddress = 'to####@gmail.com'


@msg = "Subject: Hi There!\nThis is the body.  Tested on ruby 1.8.7"

smtp = Net::SMTP.new 'smtp.gmail.com', 587
smtp.enable_starttls
smtp.start(YourDomain, YourAccountName, YourPassword, :login) do
  smtp.send_message(@msg, FromAddress, ToAddress)
  puts 'Mail sent'
end

This code facilitates you to first login and authenticate your account and the n send your mail.You have to install the gem "tlsmail" for this to execute.
-->587 is the post number through which the connection will be established.

What is aliasing??How is it done??
   Alias_ method is used for aliasing functions.At a particular instant when alias_method function is encountered the new function points to the old function at that instant.For instance consider the example..
Syntax: alias_method :newfn :oldfn

You can 

class Alias_test
def alias1
    puts "This is the first function"
end
alias-method :alias_dup :alias1

def alias1
    puts "This is the second function"
end
end


Alias_test a1
a1.alias_dup
a1.alias1

Output:
This is first function
This is second function



No comments:

Post a Comment