Hi, this blog is no longer maintained, my new blog is here

Ruby On Rails and a Conning Israeli entrepreneur

Showing posts with label console. Show all posts
Showing posts with label console. Show all posts

Ruby For bloggers

There are 2 main issues to deal with when posting a technial post on a blog:

1. Syntax Highlighting
There is a wonderful service at The Complex which exports a beautiful Ruby (and other) code, as you see in this blog also.

2. Exampling

You want to example a piece of code you did, how to do it?
try this line:

  1. def show(&block)
  2. printf("%-25s>> %s\n", expr = block.call, eval(expr, block.binding).inspect)
  3. end

for example:
  1. show {%{ a = [1,2,3] }} ; show {%{ a.slice(1,2) }} ; show {%{ a.map { |x| x**3 } }}  

will result in:
  1. a = [1,2,3]                 >>  [1, 2, 3]  
  2. a.slice(1,2)                 >>  [2, 3]  
  3. a.map { |x| x**3 }      >>  [1, 8, 27]  

Consult the Console

Day to day life-shortening experiences
My current project is the biggest thing i had ever did, concerning web applications of course (i leave out my origami career).
We have two dedicated servers, 95 models, 45 high powered DRY controllers , a half-operational BackgroundDRb actions, a whole galaxy of multi-asserted tests, test-cases and a functional hell.
A pack of gems, half a dozen plugins and pack of nead to death programmers.
Not that we needed all that for the project, but once you get into a serious programming frenzy, the tendency is to push it as far as you can.

The only thing worrying me was the debugging part, there is nothing in the world more helpless, irresponsible and depraved than a men in the depth of a debugging binge. I noticed that most of my guys aren't using the real powers of the IRB god, and even more, they are not even aware to the hell that can be unleashed when using...

the script/console..


Ok Ok, how?
The console is a simple, command lined interface to your application on any desired environment (specified as a parameter). Using the console you can try your relation in a less pre-defined way then the Testing mechanisem.
simply run script/console in your application root, you will get a very innocent looking command line entry marker, just waiting for you to unleash the beast within... so... why keep it waiting?

Tough love
One thing about the console, it's output is simply readable once used right. Example.

>> first = "i like"
"i like"
>> second = "bananas!"
"bananas!"
>> first + " " + second
"i like bananas!"


gee, this was fun... no?
never mind, the console is primarily used (by me at least) on Models, it enables you to play with your smelly database creatures, without hitting the refresh button on your browser like some kind of a disordered coffee machine... do i smell an example again?

>> elad = SuperHero.find(:first, :conditions => "very_hansom is TRUE")

#"elad", "IQ"=>"7"}>
>> elad.respond_to?(:fly_to) # Just checking if our superhero can fly
true # alright! my very own flying superhero!
>> elad.fly_to("Liverpool") # Mercy-side here i come!
>> elad.location
"Liverpool"

ok, you got the idea.
I Consider myself to be rather a lazy programmer and in fact a person. I found a few other situations where the Rails console might come handy.

Going on a date!

i wanted to check when will one of my packages arrives to it's destination, the nice clerk told me that it will take about 3 weeks before it will get there.
damn, my mathematical skills are weaker even when being compared to my x-ray vision skills. so i immediatly called my favorite problem solver... the console

>> when will my package arrive to destination? # it will never work, just trying :)
SyntaxError: compile error
(irb):1: syntax error, unexpected kWHEN
when will my package arrive to destination?
^
from (irb):1 # Damn :)
>> 3.weeks.from_now
Thu Apr 05 17:54:22 IDT 2007


yippie!, i immediatly called my Samoan clients and to wait for their next gun shipment on april 5th.

Just kidding :), but isn't it beautiful?


The Web Ask eizesus.com

Subscribe

    follow me on Twitter

    Twiters Around

    About Me

    My photo
    I am a web developer for more than 9 years, managed, cried, coded, designed and made money in this industry. now trying to do it again.

    Labels