Hello friends Ruby is very easy and intelligent language when you do all operation easily. All thing in Ruby is considered as Class and object. Ruby is pure object oriented language. As you write addition operation in ruby it gives you result directly.
Example:
write 1+100 you will get 101 on next line
irb(main):001:0> 1+100
=> 101
Here in above example 1 and 100 are treated as object and + is a method which is used to deal with this two objects.
1 and 100 are treated as an object of class Fixnum in ruby. there is a .class method in ruby which return class of an object so you can see it.
As you can see this example in Interactive Ruby tutorial.and find out class of ruby.
irb(main):001:0> "shree" + "ram"
=> "shreeram"
Example:
write 1+100 you will get 101 on next line
irb(main):001:0> 1+100
=> 101
Here in above example 1 and 100 are treated as object and + is a method which is used to deal with this two objects.
1 and 100 are treated as an object of class Fixnum in ruby. there is a .class method in ruby which return class of an object so you can see it.
As you can see this example in Interactive Ruby tutorial.and find out class of ruby.
Now do you know how ruby deals with string and how it is responding when you put + between two strings.Example
irb(main):001:0> "shree" + "ram"
=> "shreeram"
Here you can see adding two sting "shree" and "ram" you get concatenation of this two strings. but when you add a string and number ruby interpreter gives you error for implicit conversion.
This clearly state that you can either add two numbers or concatenate 2 strings.
No comments:
Post a Comment