Posts Tagged Ruby

Visibility of a method

By default methods within a class in Ruby is PUBLIC. But as we know, ruby supports meta programming and hence a new/updated code can be generated on fly during the runtime.
here is a way to make the public class methods to private.

class Myclass
  def mymethod
    puts "I am public and wishing to be private!"
  end
  private :mymethod
  # OR also can be written as
  # private_class_method :mymethod
end

obj = Myclass.new
obj.mymethod
# private method `mymethod' called for # (NoMethodError)

Similary public :mymethod or public_class_method :mymethod


Add comment July 3, 2008

Ruby security vulnerabilities

Multiple arbitrary code execution vulnerabilities in Ruby have been revealed by the Apple Product Security team which could lead to Denial of Service attacks. A total of five vulnerabilities have been reported, with versions impacted being:

1.8.4 and all prior versions
1.8.5-p230 and all prior versions
1.8.6-p229 and all prior versions
1.8.7-p21 and all prior versions
1.9.0-1 and all prior versions

Upgrading to either 1.8.5-p231, 1.8.6-p230, 1.8.7-p22 or 1.9.0-2 is recommended.

Details could be found at

http://weblog.rubyonrails.org/2008/6/21/multiple-ruby-security-vulnerabilities

http://www.ruby-lang.org/en/news/2008/06/20/arbitrary-code-execution-vulnerabilities


Add comment June 23, 2008

Generate Unique ID

Here is a very simple way to generate a unique id.
[Background support : Abhishek S.]

def generate_unique_id( len )
    chars_pattern = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
    unique_id = ""
    1.upto(len) {
			|i| unique_id << chars_pattern[rand(chars_pattern.size-1)]  }
    return unique_id
end
#generates a key of length 10
mykey = generate_unique_id(10)
print mykey # gives a result like "qeKX0myIQh"

Add comment June 23, 2008

Sorting an Array of hashes

We get a situation where we fetch some set of records from db as an array of hashes, and later we need to sort this array based on some field of DB.

Here is a very hand code to do so.

array_objs =[ {:title => "row1_title" ,:name => "Mumbai"},
              {:title => "row2_title" ,:name => "Delhi"},
              {:title => "row3_title" ,:name => "Bangalore"}  ]

array_objs .sort_by  {|array_objs|   array_objs [:name] }.each do |res|
  puts "#{res[:name] }"
end


O/P:
Bangalore
Delhi
Mumbai


Add comment May 13, 2008

Exception Handling in Ruby

>begin expression executes its body and returns the value of the last evaluated expression.
>Any error in begin part will be caught by rescue depending upon parameters
>ensure is the one which must be exectued irrespective of exception occured or not
>An error message caught by an exception can be accessed using $!

For Java Programmers [ begin => try ; rescue => catch; ensure => finally]

begin
 p "I am doing well"
 p "so well .. and well"
 a = 8/0
rescue
 p "Something went wrong => " + $!
ensure
 p "Oh Somehow I could finish my work"
end

O/P:

“I am doing well”
“so well .. and well”
“Something went wrong => divided by 0″
“Oh Somehow I could finish my work”

a = 8
b = 0
begin
 p "I am doing well"
 p "so well .. and well"
 if a==18
    p "I am happy with a as 8"
 elsif b == 0
    p "Lets say I dont want this"
    raise Exception
 else
    raise
 end

rescue
    p "Exception 1 caught here " + $!
rescue Exception
    p "Exception 2 caught here " +$!
ensure
 p "Oh Somehow I could finish my work"
end

O/P for [[ a = 8 and b = 0 ]]
“I am doing well”
“so well .. and well”
“Lets say I dont want this”
“Exception 2 caught here Exception”
“Oh Somehow I could finish my work”

O/P for [[ a = 8 and b = 1 ]]
“I am doing well”
“so well .. and well”
“Exception 1 caught here “
“Oh Somehow I could finish my work”


Add comment April 18, 2008

Ruby Hero Awards

Reasons to nominate someone

  • They write educational blog posts / tutorials
  • They contribute to a useful open source project
  • They help organize educational events
  • They give free support on the mailing list / IRC

Ruby Heroes was created to show some gratitude and give these people the recognition they deserve. Hopefully the type of recognition that keeps them doing what they’re doing, and continuing to make our community stronger.

Time has come to nominate such person for an honorary award.

Ruby Heroes is produced by the guys over at Rails Envy, and coded up by Brandon Beacher.

To nominate someone, click here.
Ruby Heroes


Add comment April 17, 2008

Previous Posts


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