Posts Tagged local

variables in Ruby

-Ruby has three kinds of variables:

  • Global variables
  • Instance variables
  • Local variable

-Constant
e.g GVAL = “9.8′

-And two pseudo-variables.

  1. self
  2. nil

self, which always refers to the currently executing object, and nil, which is the meaningless value assigned to uninitialized variables. Both are named as if they are local variables, but self is a global variable maintained by the interpreter, and nil is really a constant.

<<<<<<<<<<< GLOBAL VARIABLES >>>>>>>>>>>

1. A global variable has a name beginning with $.
2. It can be referred to from anywhere in a program.
3. Before initialization, a global variable has the special value nil.
4. Prayas ke style me bole to :These are prasadam(God’s) & can be shared by anybody.

class Myclass
  $global_var = 99;
  def method1
     p "In method1"
     p "Global var = #{$global_var}"
  end

  def method2
     $global_var += 1;
     p "In method2"
     p "Modified global var = #{$global_var}"
  end
  p "Inside the class global var = #{$global_var}"
end

o1 = Myclass.new
o1.method1
o1.method2
p "Outside the class #{$global_var}"

o/p:
"Inside the class global var = 99"
"In method1"
"Global var = 99"

 <<<<<<<<<<< INSTANCE VARIABLES >>>>>>>>>>>

1. Starts with @. Instance variables is bounded to an object. So as many methods are associated with that object, are gonna share a single copy of it.
2. Means any change will be reflected to remainins methods.But when you call a method with a new object (o2) it re-initializes the value of instance variables.
3. Prays ke style me bole to : I say these are PRIVATE to object not to method. [same family(Indian) can share a meal in a single plate; but they feel shy to offer to a nonmember of that family ]

class Myclass
  def method1
     @instance_var = 99;
     p "In method1"
     p "Instance var = #{@instance_var}"
  end

  def method2
     @instance_var  += 1;
     p "In method2"
     p "Instance var = #{@instance_var}"
  end

  def method3(parameter1)
	p "Instance var without declaration, directly use it."
	p "And value is @para1 = #{parameter1}"
  end
end

o1 = Myclass.new
o1.method1
o1.method2
o2 = Myclass.new
o2.method1
o2.method3(999)

o/p::
"In method1"
"Instance var = 99"
"In method2"
"Instance var = 100"
"In method1"
"Instance var = 99"
"Instance var without declaration, directly use it."
"And value is @para1 = 999"

Q. Can a metod access an instance varibale used in other method?
A. Yes provided for the same object call. [@instance_var in method1 can be accessed in method2]

Q. Is instance variables are same like local variable?
A. NO, local varibales are diff and they do exist in ruby.

<<<<<<<<<<< LOCAL VARIABLES >>>>>>>>>>>

1. starts with no $ or no @, yes they must start with eith lower case letter or an underscore (_mylocalvar)
2. must initialize befor you use else error
3. Prayas ke style me bole to: These are private to methods (Chats of bed room cant be shared in dining room). Generally, the scope of a local variable is one of

* proc{ … }
* loop{ … }
* def … end
* class … end
* module … end
* the entire script (unless one of the above applies)

class Myclass
  def method1
     local_var = 99;
     p "In method1"
     p "Local var = #{local_var}"
     p defined?(local_var)
  end

  def method2
    # local_var  += 21; You must initialize b4 it is used.
     local_var_other = 55
     p "In method2"
     p "Local var = #{local_var_other}"
     p defined?(local_var)
     p defined?(local_var_other)
  end
end

o1 = Myclass.new
o1.method1

o/p::
"In method1"
"Local var = 99"
"local-variable"
"In method2"
"Local var = 55"
nil
"local-variable"

Add comment March 22, 2008

Mumbai Meri Jaan

Wo Bhajan gane ki aawaj aa rahi thi,
Uncle jee busy the shabdpat (crossword game) solve karne me,
Bhai Saab was pretending to be asleep when I asked him “Bhaiya thori jagah do na!”
This gentleman was stick to Business Times,
The other lean chap in glass was balancing the laptop over his poor shoulder.
Meanwhile somebody kicked me from back and started singing a song: 10rupye me 12 santre lo – Khatta Mittha.

Finally I took a corner, plugged my iPod, clamped my hands blessed by those hathkadi (hanging iron bars)…thanked GOD, I could reach Vashi Railway Station safely.

This was a scenario for me; I never felt it would be a never forgettable login form for everyday similar to our orbit login@v2.

Yes-dear friends—truly, this is the most vibrant artifact LOCAL TRAIN in Mumbai.
In fact I was quite excited about my coming from Bangalore to this mind-blowing metro, but the crowd realized my
GK that why is it known as THE CITY OF INDIA’S ECONOMY. Well there is a lot about this urbanized domain, but LT(short for Local train…) is one, which pays your attention quickly. Damn!! The way people get on and get down ………

 

The most amazing fact is, every compartment will be THRICE overloaded, but still those uncles will be complaining about their boss, examinees are inhaling their notes, Radiocity is live with that funk, couples have got a good hold on each other & communicating in 143 binary codes (Oops!! you and I cant break that cipher), Hmm… he could ignore that stinking guy in the same shirt since last Diwali, you got a phone call – No problem except that sometimes poor signals make you a center of attraction in the crowd.

 

More to say here… AD companies don’t need an advertisement board. Bring your posters and train is the best place for its agile publicity. You want PAN card agent, Shaktibhog aata, jobs@home, hair plus-plus, obesity minus-minus, movie@theater, Maa Shakti ka Pravachan or stuff may take care of your health. Everything is in your dubba. Just keep on your eyes on what are you looking for?

 

I loved it…. I really liked the way Government is managing this bulky hardware and moreover the people are managing them into this complex clustered architecture. One thing I started not believing & it’s my wristwatch; coz digital watch on the platform looks greater efficient and more useful than watch in my hand.

 

Probably Mumbai LT is the best example of a human life, which may begin with so much struggle & kind of adjustment we do in between, manage the people/belongings around us & finally get down at your destination with a great contentment.

 

So what is there to learn from this? Yes it is the TIME FACTOR.

 

Despite the crowds, shouting, struggles and adjustments, train reaches to a destination at right time. Hardly we find any fluctuation of over a minute or two. So why can’t we be like that?

 

Why do we always blame others?
Why do we always put others in question escaping ourselves?
Why do we always disrespect a system?

 

Thanks to those daily passengers admitted their gratitude towards LT by putting posters in few bogies & stating: “Happy DurgaPooja from 8:40 group”. I feel great about them, I feel great about LT who promised to be always on track@right time. Plunge forward! I say to them.

 

Hence take a lesson from LT and always deliver things@right time in your life despite your problems.
Be punctual & be fast, cause..

Gadi bula rahi hai, siti baja rahi ha
Chalna hi zindgi hi, chalti hi ja rahi ha……………………..


Add comment March 22, 2008


Categories

posts[:recent]

episodes[:recycled]

@@name = PRAYAS

Step down at my blog with your ideas,comments,suggestions on Ruby,RoR,Ajax or Web2.0.You may reach me at
infostall@gmail.com

find_by_tags

Links

visitors[:since_Mar'08]

free web counter

Spam Blocked

Feeds

Meta

RSS Prayas here