Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Thursday, July 10, 2014

Ruby exclamation mark method

In ruby, we see the methods with name ends with ?, ! Most of us know about the use of method name ends with ?. Ex: is_public? We are expecting the method to return result in boolean value(true/false). In exclamatory methods, usage is something different. See the following examples,
a = "A STRING"
puts a.downcase => a string
puts a => A String


b = "ANOTHER STRING"
puts b.downcase! => another string
puts b => another string
For no match case, gsub will return the actual string without any change in it
puts "ruby on rails".gsub("none","--") => ruby on rails
For no match case, gsub! will return nil
puts "ruby on rails".gsub!("none","--").inspect
People call methods ends with exclamation mark is dangerous to use/careful when using it. The reason is, in some cases it will throw exception instead of false. Ex:
save => return false if save fails
save! => raise an exception if save fails
We have a lot of exclamation mark methods in ruby, start looking on each one with their usage and start using it.
Hope it helps..

Sunday, November 15, 2009

ruby - 'yield' method

Hi Guys,

Hope most of our ROR developers are using 'yield' method in the layout file. In ruby we are not using the yield method(called from any method). I have come across the 'yield' method, its very interesting to know about the use of that method.

We can use 'yield' method inside any ruby method to execute the code inside the block.

example-1:
class Sample
def check_yield
yield
yield
yield
end
end

in the class 'Sample', i have called the yield method three times inside the method.

obj = Sample.new()
obj.check_yield{puts "hi am in method block"}

Output:
hi am in method block
hi am in method block
hi am in method block

Now i have created an object for the class 'Sample', and called the method 'check_yield' with the block({ }). When i executed the method it prints the results for three times. Because we are calling the yield for three times, the call to yield method executes the code inside the block({puts "hi am in method block"}). That's it.

We can also use the yield method with params.

example-2:
class Sample
def even_nos_upto(param)
for i in 0..param
yield(i) if i%2 == 0
end
end
end

obj = Sample.new()
obj.even_nos_upto(10){|x| puts x}

Output:
0
2
4
6
8
10

In Array, we are using the method 'collect', 'each' to get each value of the array.

[1,2,3,4,5].collect{|x| puts x}

Inside the collect method in Array class, the yield method called to return each value.

class Array
def my_collect
for i in 0..(self.length-1)
value = self[i]
yield(value)
end
end
def collect_greater_than(limit)
for i in 0..(self.length-1)
value = self[i]
yield(value) if value > 5
end
end
end

[1,2,3,4,5,6,7].collect{|x| puts x} # default method collect
[1,2,3,4,5,6,7].my_collect{|x| puts x} # method my_collect
[1,2,3,4,5,6,7].collect_greater_than(5){|x| puts x} # collect with condition

Output:
1
2
3
4
5
6
7
1
2
3
4
5
6
7
6
7

In the above sample, the method 'collect' & 'my_collect' works like a same. And the method collect_greater_than bit different from the first one, it returns the values based on the condition we have used.

Friday, October 30, 2009

has many polymorphic - double sided

I have used the following model relation to get the has many polymorphic double sided relation.

class Album < ActiveRecord::Base
has_many :album_files, :dependent => :destroy, :foreign_key => "album_id", :class_name => "AlbumFile"
has_many :photos, :source => :file, :through => :album_files, :source_type => "Photo", :class_name => "Photo"
has_many :videos, :source => :file, :through => :album_files, :source_type => "Viedo", :class_name => "Video"
end
class AlbumFile < ActiveRecord::Base
belongs_to :file, :polymorphic => true
end
class Photo < ActiveRecord::Base
has_many :album_files, :dependent => :destroy, :as => :file
has_many :albums, :source => :album, :foreign_key => "album_id", :through => :album_files, :class_name => "Album"
end
class Video < ActiveRecord::Base
has_many :album_files, :dependent => :destroy, :as => :file
has_many :albums, :source => :album, :foreign_key => "album_id", :through => :album_files, :class_name => "Album"
end


thats it..

Now we have associated Album with photos, videos through the mapping table album_files.

Following works for this

album = Album.create()
album.photos << Photo.create()
album.vidoes << Video.create()

album.album_files.count - returns the total count i.e, 2

album.photos - returns the count 1
album.vidoes - returns the count 1