Posts Tagged objects

Ruby nil is kicking you

Ruby is said to be a genuine Object Oriented Language. Everything is an object; even a class and moreover even nil.
Basically nil tells you about an absence of a value (or an otherwise meaningless value). n/a is a good candidate for a nil value.
The nil value is distinct from all other values. However, when we force Ruby to evaluate nil as a Boolean, it evaluates to false.

“Prayas” if nil
=> nil

“Prayas” if !nil
=> Prayas

The only values that evaluate to false Booleans are nil and false. Everything other than nil or false evaluates to true when forced into a Boolean. Zero is also evaluated as true(not as false) great!!!!

“Prayas” if false
=> nil

“Prayas” if nil
=> nil

“Prayas” if -5
=> Prayas

“Prayas” if 0
=> Prayas

nil.methods # gives you all methos

But here I am interested for methods which starts with to_. Lets gather those.

nil.methods.grep(/^to_.$/)
[”to_f”, “to_a”, “to_s”, “to_i”]

And what are they giving to me:

nil.methods.grep(/^to_.$/).each do |method_name|
puts “nil.#{method_name} :: #{nil.send(method_name).inspect}”
end

o/p
nil.to_f :: 0.0
nil.to_a :: []
nil.to_s :: “”
nil.to_i :: 0

Convert a nil value to 0 in order to avoid crash.
a = 25
b = nil
c = nil
d = “mystring”
p a||=0 # returns 25 as a is not nil
p b||=0 # returns 0 as b is nil
p c.to_i # returns 0 as nil is casted to int ,i.e other way
p d||=0 # returns “mystring” as d is not nil

so we got TWO ways to convert nil value to 0
1. using myvar ||= 0
2. using myvar.to_i OR (to_f, to_a, to_s, to_i )

How to handle when you get a crash because of nil?

def mymethod(para1)
p para1.to_s.upcase
#p para1.upcase
end

mymethod(”Prayas”)
mymethod(”Attempting”)
mymethod(nil)

o/p:
PRAYAS
ATTEMPTING
“”
The .to_s allows nil to be passed in without failure.
Without the .to_s you would receive this:(i.e if you use para1.upcase)
undefined method `upcase’ for nil:NilClass (NoMethodError)

In your views you may use something like to avoid crash
<%= @instance_name.variable_name rescue nil %>

Add comment March 22, 2008

Variable vs Objects

person = “Tim”
p person.object_id
p person.class

p person
#~ 22901710
#~ String

#~ “Tim”

- Ruby creates a new String object with the value “Tim”
- A reference to this object is placed in the local variable person.
- The o/p revels this variable(person) has indeed taken on the personality of a string, with an object id, a type, and a value.

So, is a variable an object?
NO.
- A variable is simply a reference to an object.
- Objects float around in a big pool somewhere (the heap, most of the time) and are pointed to by variables.

person1 = “Tim”
person2 = person1
person2[0] = “J”

p person1.object_id
p person2.object_id
p person1

p person2

#22899620
#22899620
#”Jim”
#”Jim”

variables hold references to objects, not the objects themselves.
- person2 = person1 :: doesn’t create any new objects;
- it simply copies person1’s object reference to person2, so that both
person1 and person2 refer to the same object.

So = symbol here works as an aliasing object >> Gives you multiple variables
And all these variables reference to the same object.

Then how can we create a duplicate object instead a reference variable ?

Using dup method:
person1 = “Tim”
person2 = person1.dup
person1[0] = “J”
p person1.object_id
p person2.object_id
p person1
p person2

#~ 22901200
#~ 22901190
#~ “Jim”
#~ “Tim”

variables vs objects

But what if you want to prevent from any modifications to the object?
person1 = “Tim”
person2 = person1
person1.freeze # prevent modifications to the object
person2[0] = “J” # can’t modify frozen string (TypeError)

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

1st April 37signals 360 2008 aamir aamir new look abdul abhinay access active active record aishwarya AJAX AMC amul and bollywood class facebook ghajini google hosting India instance local mozilla myspace nil NY object objects olympic plugin prayas programming Rails RoR Ruby security talent variables web web2.0 windows yahoo

Blogroll

visitors[:since_Mar'08]

free web counter

Spam Blocked

Feeds

Meta

RSS Prayas here