As you know everything in Ruby is made up of classes.And there is so much of inheritance involved in the internal hierarchy of Ruby.The base most class is BasicObject class in Ruby1.9.0.In the older version it was the Object class.The Basicobject class doesn't have any methods at all.During computations ,any error doesn't occur when you try to do any operation between a Float class member and a Fixnum class member.It is because the hierarchy is as follows and both come under the Numeric class.
Float-->Numeric
Fixnum-->Integer-->Numeric.
Numeric forms the base class for both the cases.
Arrays,Strings and Hashes come under the Object class.
Blocks:
Block commands are those which start with a specific element and ends with an "end".Some of the block commands or methods in ruby are
"each-do"
"while"
"do while"
Even the classes and the definitions end with "end" keyword.While this exists there are certain cases where you dont use it.
They are
"each","collect","map","select" etc.
Blocks using "yield":
They are like proc ans lambda to pass code.
During function calls you can include a block of code after the function call and can access it via the "yield" keyword inside the function.
eg
def fn1
puts"hello"
yield
end
fn1{puts "im in the block"}
output:
Hello
im in the block
You can also access the blocks from the function using arguments.The arguments are given in the yield in the form of arguments.One such example with block is
Examples on Proc and Lambda:
a=Proc.new{|a| a%2==0}
b=Lambda{|a| a%2!=0}
fn(a);
fn(b);
fn(code)
{
code.call
}
This directly assigns "a"a r "b" to code and enables easy function call.The main difference between Proc and Lambda is the flexibility of Proc in accepting less number of arguments.
def f1
a=Proc.new{|q,w| return}
b=lambda{|q,w| return}
puts "before"
a.call(3,4)
b.call(2,6)
puts "after"
end
f1
Now that you know about Proc and Lambdas what will you do if you create a Proc and call it as a block or vice versa???
At that point "&" operator comes into play.
a=Proc.new{puts "passing as a proc instead of block"}
def run_code
{
yield
}
run_code &a
Now how to pass a block accepting a proc...
def run_code(&code)
code.call
end
run_code{puts"passing a block accepting a proc"}
IRB:
-->This is Interactive Ruby.you can type in the ruby commands and you can check the output if its the correct one.
-->It is an interface for instant experimentation.
-->You start it with an "irb" command in the command prompt.
-->You can use this each time you learn something new and check for the return values.
Float-->Numeric
Fixnum-->Integer-->Numeric.
Numeric forms the base class for both the cases.
Arrays,Strings and Hashes come under the Object class.
Blocks:
Block commands are those which start with a specific element and ends with an "end".Some of the block commands or methods in ruby are
"each-do"
"while"
"do while"
Even the classes and the definitions end with "end" keyword.While this exists there are certain cases where you dont use it.
They are
"each","collect","map","select" etc.
Blocks using "yield":
They are like proc ans lambda to pass code.
During function calls you can include a block of code after the function call and can access it via the "yield" keyword inside the function.
eg
def fn1
puts"hello"
yield
end
fn1{puts "im in the block"}
output:
Hello
im in the block
You can also access the blocks from the function using arguments.The arguments are given in the yield in the form of arguments.One such example with block is
Here the class Array is opened accessed and then we can add the definition for "_all?" method.Very similar to the above method you can also define the "any?" method.class Arraydef _all?self.each do |e|return false if not yield eendreturn trueendendputs [2, 4, 6]._all? { |e| e%2 == 0 }
Examples on Proc and Lambda:
a=Proc.new{|a| a%2==0}
b=Lambda{|a| a%2!=0}
fn(a);
fn(b);
fn(code)
{
code.call
}
This directly assigns "a"a r "b" to code and enables easy function call.The main difference between Proc and Lambda is the flexibility of Proc in accepting less number of arguments.
def f1
a=Proc.new{|q,w| return}
b=lambda{|q,w| return}
puts "before"
a.call(3,4)
b.call(2,6)
puts "after"
end
f1
Now that you know about Proc and Lambdas what will you do if you create a Proc and call it as a block or vice versa???
At that point "&" operator comes into play.
a=Proc.new{puts "passing as a proc instead of block"}
def run_code
{
yield
}
run_code &a
Now how to pass a block accepting a proc...
def run_code(&code)
code.call
end
run_code{puts"passing a block accepting a proc"}
IRB:
-->This is Interactive Ruby.you can type in the ruby commands and you can check the output if its the correct one.
-->It is an interface for instant experimentation.
-->You start it with an "irb" command in the command prompt.
-->You can use this each time you learn something new and check for the return values.
No comments:
Post a Comment