thejavajar

Tag: Ruby

Ruby Proc Fun

by RJ Salicco on Aug.31, 2009, under Development

I am fairly new to Ruby but I do have experience with Groovy so I have found learning and experimenting with Ruby somewhat of an easy effort. The Ruby community is pretty big so there are plenty of blog posts and other Web sites that are available, via a Google search, to help understand some problems when I get stuck. I started playing around with Ruby Proc (procedures), if/elsif/else and case/switch statements and here is what I am having fun with:

# define our apple Proc
apple = proc { |t| puts "my apple" + t }

Above we have our executable (proc) code assigned to the ‘apple’ variable.

# doIt1 with if statements
def doIt1(c, t)
  if t.respond_to?("call")
	puts "-- callable"
    c.call " is awesome"
	t.call " is really awesome"
  elsif t.kind_of?(Array)
	puts "-- not callable Array"
	t.each { |t1| c.call t1 }
  else
	puts "-- not callable String"
	c.call t
  end
end

Then we have a method ‘doIt1(c,t)’. This method expects 2 arguments; the first argument, ‘c’, I expect is a Proc and the second argument, ‘t’, I expect to be a Proc, Array or String.

if t.respond_to?("call")

The ‘if’ statement checks to see if ‘t’ has a method named ‘call’ that it will respond to. I think the ‘respond_to’ method is great for dynamic languages. I could have just checked to see if ‘t’ is a Proc as well, but then that limits me to one Object type, right?

t.kind_of?(Array)

The ‘elsif’ statement checks to see if ‘t’ is of type Array. Like instanceof in Java.

# doIt2 with case/switch block
def doIt2(c, t)
	case t
	  when Proc
	    puts "-- callable"
		c.call " is awesome"
		t.call " is really awesome"
	  when Array
	    puts "-- not callable Array"
		t.each { |t1| c.call t1 }
	  when String
	    puts "-- not callable String"
		c.call t
	  else
	    puts "not valid"
	end
end

Then I created a method just like ‘doIt1′ called ‘doIt2′. In ‘doIt2′, I used a case/switch statement and handled things similarly to ‘doIt1′. In the second method, I check ‘t’ to be of type String and have a default case for when ‘t’ is not a Proc, Array or String.

# doIt1 tests
doIt1 apple, apple
doIt1 apple, " works good"
doIt1 apple, " works hard"
doIt1 apple, [" looks good", " looks elagant", " looks shiny"]

# *'s used to separate method tests
puts "*" * 40
puts "*" * 40

# doIt2 tests
doIt2 apple, apple
doIt2 apple, " works good"
doIt2 apple, " works hard"
doIt2 apple, [" looks good", " looks elagant", " looks shiny"]

Then I wrote a few ‘tests’ to see what would happen. Ruby is fun and I definitely enjoy working with Ruby and Groovy. Here is the full script file: appleProc.rb

Leave a Comment :, more...

Getting Familiar with Ruby on Rails

by RJ Salicco on Aug.03, 2009, under Development

Ruby on Rails is something that I have been meaning to look into for a while. I have been working with Java for most of my professional IT career and I work with Groovy on Grails whenever I have it my way. I even work with PHP and Python when working with some clients or when I feel like doing something different. So, I owed it to myself to give Ruby on Rails a good look. Why? Because there are a lot of people who are passionate about RoR and Rails is what inspired Grails after all.

I have been working with a pretty simple Web application that requires simple security, data access and minimal navigation. I will post more about the application once I have it done, but I can see why people are passionate about RoR just like I am passionate about GoG. Ruby is elegant, no doubt about it and Rails fits in to bring it all together for Web developers who want to focus on solving the problem at hand rather than fighting the technology stack. I am enjoying my experience with RoR thus far and would recommend that other Web developers who have not yet tinkered with Ruby on Rails give it a try for a few hours a night for a couple of days. Check http://rubyonrails.org/ out. There is a plenty of documentation and links to get you started.

Leave a Comment :, more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!