Silence is Foo Mental notes on Ruby, Git, Rails and whatever geeky thing

30Jan/10Off

My friend the Rails Console

NOTE: There's a second part of this post.

Ok, I know this post could be stupid for some smart people, but, c'mon I just discovered a very good "trick" and I'd like to share it, besides, I'd like to share some others I use as a daily basis.

first, fire the console: $ruby script/console:

Examining objects


>>  user = User.find_by_name('john') # user whatever model you want

=> #<User id: 876, name: "john", email: "john@raflabs.com", etc, etc, etc...
if you want to see the object in a better way, you can use:
>> puts user.to_yaml
now, the shortcut I just discovered:
  >> y user 

Accessing the last returning value

Let me introduce you a magic variable: _

_ returns the last value, so:


>> user.roles

=> [#<Role id: 1, name: "admin" etc, etc, etc...

>> _

=> [#<Role id: 1, name: "admin" etc, etc, etc...

>> (2 + 5 ) * (10 / 2)

=> 35

>> _

=> 35

Calling controller actions


>> app.get 'controller/action?parameters'

>> app.get 'sessions/new?login=john&password=john'

=> 200 # this is the response (an http status code)

>> app.response.body # the html

>> app.response.cookies # the cookies

IRB sub-sessions (one of my favorites)

You can create irb (Interactive Ruby) sessions inside irb sessions, you just need to use the following commands:

irb start a new subsession

jobs list subsessions

fg # switch to a subsession

kill # kill a subsession


>> user = User.first
>> irb
>> user # undefined local variable
>> user = User.last
>> irb
>> user # undefined local variable
>> user  = User.find 4
>> jobs
=> #0->irb on main (#<Thread:0x100170358>: stop)
#1->irb#1 on main (#<Thread:0x102f1ca18>: stop)
#2->irb#2 on main (#<Thread:0x102e0cf88>: running)
>> fg 0
>> user
=> #<User id: 1, name: "john" ...
>> fg 1
>> user
=> #<User id: 587, name: "peter" ...
>> fg 2
>> user
=> #<User id: 7, name: "other" ...
>> kill 2
>> kill 1
>> jobs
=> #0->irb on main (#<Thread:0x100170354>: running)
ok, let's go a little bit further, let's say you just opened a rails console and write the following
>> irb_context.workspace.main #it can be context.workspace.main or even self
=> #<Object:<strong>0x100177298</strong> @helper_proxy=#<Object:0x102fb42f0>, @controller=#<ApplicationController:0x102fb3ff8>>
>> irb #create a new subsession
>> self
=> #<Object:<strong>0x100177298</strong> @helper_proxy=#<Object:0x102fb42f0>, @controller=#<ApplicationController:0x102fb3ff8>>

nice!, it's the same object (0x100177298) in all the sessions, so we can share methods along all the sessions because we would be working in the same object,
but, could I change the object I am working on? YES!

>> user = User.first
>> irb user
>> name
=> "john"
>> email
=> "john@raflabs.com"
>> exit
>> name
=> NameError: undefined local variable or method `name' for #<Object:0x100177298>
wooow, I'm like inside the object!!!

Ruby documentation in the console

For people like me, with a -100 IQ brain which doesn't have a very good memory this is AMAZING!

You may know that the Ruby Array class has an "each" method to iterate collections and that if you write in the terminal (bash console, for example):


$ri Array#each

you'll be presented with the documentation of the Array#each method, but what if you were able to do this in the console:


>> my_array = [3,5,6]

>my_array.ri_each

=> array.each { |item| block }

Calls _block_ once for each element in _self_, passing that element as a parameter

Well, now you can do it, just go to this page an see how to: http://eigenclass.org/hiki/irb+ri+completion

This is not a trick

as you may know, if you do this:


users = User.all

ruby returns the whole array of all the existing users (it may be thousands of them), but sometimes you don't want to see it, so you can do this:


users = User.all; nil
as you may remember, ruby returns the last evaluated expression, so if nil is the last one, it returns nil.
That's it
Filed under: rails 8 Comments
8Jan/10Off

iPhone 3GS: the bad, the good and the beauty (or Apple and Telcel Mexico against you)

I just got an iPhone some weeks ago, I must say it's totally impressive and elegant, but, mmm, let's face it, it's an Apple product and that means thousands of stupid restrictions, here I want to mention the most stupid ones (the order doesn't actually cares):

NOTE: I know there some ways to overpass some of the restrictions I'm about to mention, but to mention those solutions is out of the scope of this post.

====THE BAD (about restrictions)

1. You cannot use a song (or part of it) in your library as an alarm tone, ringtone, new SMS or new mail notification, you have to stick to 5 or 6 built-in tones.

2. The only way to install applications is through iTunes store (App store)

3. Apple should accept an application in order to make it available in the App store, which means they decide what's good and what's not good for them (not for us), if Apple thinks an application is gonna affect its earnings they don't accept the application.

4. Phone completely blocks free software. Developers must pay a tax to Apple, who becomes the sole authority over what can and can't be on everyone's phones.

5. Internet tethering (sharing internet from your iPhone to you laptop) is not activated in Telcel Mexico, but at the end Apple made it possible because they made possible the carriers to decide this kind of things, which means they let another company to decide what's good for the company itself, not the customer, so Apple wins, Telcel wins, Customer loses. Yes, I know it sounds too Marxist :D

6. iPhone endorse Digital Restrictions Management (DRM) technology, so it won't play patent- and DRM-free formats like Ogg Vorbis and Theora

7. You cannot even change the battery in your own phone (well, with all theses restrictions it seems it's not actually yours,). iPhone should be free of charge, gratis, btw.

8. You can only sync your iPhone with ONE computer and that computer must have iTunes, and I hate iTunes, I really do... (there are some tricks to sync with more than one computer and with software other than iTunes, but you can't sync notes, calendar, bookmarks, etc, only music)

9. You cannot use bluetooth to share your photos or music between your iPhone and other mobiles (there are some apps to do so between iPhones), the built-in bluetooth it's only and only to use with a headset. crap. It's really shameful when someone ask you to share a photo with him/her after showing all those great and impressive iPhone apps.

10. No support for SD cards (to add more capacity of storage)

11. No support for Adobe Flash Player.

12. Apple doesn't allows third-party applications to run in background!!!!! every time you push the Home button (the one with the white square on it) you close the current application, the only apps which runs in background are Phone, Mail and iPod. Oh, and yes, Phone is an application to use your iPhone as a...mmmm... phone :D

Well there are other things I don't like, but I don't remember them.

======THE GOOD

1. The iPhone 3Gs is really fast, its responsiveness is impressive.

2. The iPhone 3GS overwhelmingly beats other smartphones in terms of the availability of applications and widgets.

3. The touchscreen is amazing, and it has a fingerprint-resistant oleophobic coating (less fingerprints, less scratches)

4. The iPhone OS 3.0 is amazing! iPod or iPhone 3g owners will be able to install it though

5. The digital compass which rotates maps to always match the direction you’re facing. This is very good for the integrated A-GPS.

6. Really fast web browser (Safari Mobile)

7. Push notifications. Applications like twitter and facebook clients and instant messaging applications can take advantage of it, that way the there's no need the application is running to get the new messages, well, you can get push notification of almost anything using the amazing Prowl

And there are hundreds of good things about the iPhone, maybe I will update this posts later.

====THE BEAUTY

well, after the bads, and the goods the only thing I can say is: the iPhone is gorgeous, elegant, beautiful and iLoveIt, but I need to do something to unleash its real power... so I'll jailbreak it!!!

Thanks for reading!

Filed under: iPhone Leave a comment