Ruby – Kernel

Posted on March 7, 2008. Filed under: Uncategorized |

Kernel is one of the important and most used module in Ruby since it is part of the Object class through mixin.The object includes Kernel module in its class .So all its methods are available for calling within any object in Ruby because they are all instance methods and once within other class by using the include “MYModule Name” it is almost like writing the all the code/method again into this class.so we can call them without any prefixes.so you get a lot of methods for free like p/puts

global_variables/local_variables: gives the entire list of $ variables that is available every where..gives the list of local variables.

p/puts/print : puts the parameters on the screen / $stdout….which is already an intialized/instantiated object of class IO with a line return /newline. whereas print just outputs without any new line.This would print from the buffer of the stdout without giving any parameter atall.Also this takes into consideration the field separator $, and record separator $\ if it is not nil they would be added to the output.

gets: get the input from the screen / $stdin …or if you intialize the ARGV then it would pop this array to be used for reading one line from that file mentioned .

`:backtick : used for executing a os commands .this is a method in RUBY…

4 interesting methods : Array,Integer,String,Float,URI :would try to convert the object into the mentioned class. String(obj) : would get the string of the object.y : to_yaml : converts a object to yaml format y obj ->

eval: this is a kernel method which mean you call without any object as a prefix and evaluate the string as a code.

exit: intiates exit for the current ruby script by a raising a SystemExit exception .this can be caught just like anyother exception by using rescue…and just before termination at_exit block get executed if there was any…

loop : This is a endless loop and can be broken by a break statement.loop do print “Input:” ; line = gets; break if line =~ /qQ/; end

require/load : load loads a ruby program as many times as it is written.require just loads it once after which the statement doesn’t do anything.Another intelligent thing about require is it would put extension .rb or what required for the file whenever needed .when the class are defined in different file and you need those classes you can say require “filename”. and $: contains the file path that ruby would be looking to get the file and load it into memory.another similar method is require_library_or_gem this will load the library other wise will load the gem.The loaded program would not be part of current name space.the local variables will performed in annoymous module.

method_missing: if you implement this method then you will never get the NoMethodError.when ever there is a method call that is missing then this method will be called .it takes one parameter..

sleep: sleep followed by number of seconds…

readlines/readline:would read the all the lines and pass it back as an array.It reads from $_

scan/scanf : This the block and non block version for scanning for a particular pattern and returning as an array. The string used for scanning $_ scan(/o.*/) with $_ = “now only” would return ["ow only"].Each pattern even if it is star is scanned till blank space.

printf/sprintf

srand/rand:produces a rand number so easy…rand(100) produce a random number between 0 and 100 …srand is used to remember the random number .this is done by calling before the calling rand and then agani when we want the same number…

raise:AnyBody can raise the exception by passing the class name of the exception for Example Exception,NameError and the text to be seen.just passing the text alone would cause ruby look in $! for the Exception Name.The situation can rescued by rescue statement and

/throw/catch:throw take the control to different place where a catch with the same symbol is waiting .If there is no catch then it would raise NameError.catch would executes it block and so if a symbol is thrown and catch having the same keyword will handle it for eg.. catch( :done) { do_routine } def do_routine throw ( :done ) end

sub/gsub:This is nothing but substitute and global substitute based on pattern match on $_ .This method exits in string as well . so $_.sub(pattern,replacement) or block…

proc/lambda: a block statement can be converted into nameless function / method and its pointer is in the variable . you can either use lambda or Proc.new

open:Open the file name given as the first parameter.If the first character is a pipe then the opened file object can be used to write to standard input ,read from standard output

block_given?: checks to see if the variable has a block { } associated with it when called.if so then it will return true .you can yield to it / pass on the control to the block by calling yield( I think this is a keyword in the language itself).

gem: will load the gem given in the parameter.returns true if it succeeds else raises error..It must be used before the require statement.

exec: executes os command but this would finish the current process…ruby

chop/chomp:chop will chop off the last character.Chomp will chomp off the new line / will take any parameter that you pass to the chomp and chomp that off from the $_ but this should be at the end. what ever there is $_ will be chomped/chopped….it will be better use the String.chop/chomp

binding: gets the context at the time of call for eg if it is in a method call then it would have the instance variable values in it.so this binding context can be used in the eval statement so that the string will be evaluated in that context..

?: returns the ascii number for the alphabet.

Test: This is a test on file.to compare files between files,whether they exist and lot other check…useful for file…

Make a Comment

Make a Comment: ( None so far )

blockquote and a tags work here.

  •  

    March 2008
    M T W T F S S
    « Feb   Apr »
     12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31  
  • My Fav

Liked it here?
Why not try sites on the blogroll...