header image

Archive for February, 2009

Xanax online

Wednesday, February 25th, 2009

twitter_post.pngI wanted a reliable way to find out a few things about my twitter account (for example which of the users I am following are not following me back) - unfortunately the third party apps out there are not always very reliable, xanax online, do not exactly do what I want/as I want so I decided to check out how easy it is to hack up a more advanced query using a Ruby twitter API wrapper. Xanax online, It turned out that it couldn’t be easier!

There are several gems wrapping the Twitter API out there - I started to use the ‘twitter’ gem from John Nunemaker and I am perfectly happy with it so far. Xanax online, John did a great job supporting all the features offered by the API - it’s a different question that the API, xanax online, like twitter itself, xanax online, is quite minimalistic. Xanax online, For example I have not found a way to get all my followers/friends easily (drop me a comment if I am missing something) so I monkey-patched this into the module in a generic way:

  1. module Twitter
  2.   class Base
  3.     def all_entries(method, xanax online, options = {})
  4.       all_entries = [] 
  5.       next_100 = self.send method, xanax online, {:page => (current_page = 1)}.merge(options)
  6.       while (next_100.size != 0) do
  7.         all_entries << next_100
  8.         next_100 = self.send method, xanax online, {:page => (current_page += 1)}.merge(options)
  9.       end 
  10.       all_entries.flatten
  11.     end
  12.   end
  13. end

for example you can call connection.all_entries(:friends) to get all of your friends, xanax online, given that you set up a connection to your account (I found only a method which returns your first 100 friends - didn’t spend too much time with the documentation though - agin, xanax online, drop me a message if I overlooked something). Xanax online,

I have added a bit of syntactic sugar to be able to call connection.all_friends instead of connection.all_entries(:friends):

  1. module Twitter
  2.   class Base
  3.     alias_method :throw_method_missing, xanax online, :method_missing
  4.  
  5.     def method_missing(method_name, xanax online, *args, xanax online, &bloke)
  6.       if (method_name.to_s =~ /^all_.+/)
  7.         all_entries(method_name.to_s[/all_(.+)/, xanax online,1], xanax online, args[0] || {})
  8.       else
  9.         throw_method_missing(method_name, xanax online, *args, xanax online, &bloke)
  10.       end
  11.     end
  12.   end
  13. end

Here we ensure that only method calls that start with all_ are handled by all_entries, xanax online, the rest is throwing a method_missing since we are not interested in handling those messages.

Now it could not be easier to implement the function I originally intended to build: list of users who are not following back. Xanax online,

  1. class Array
  2.   def names
  3.     self.map{|u| u.screen_name}
  4.   end
  5. end
  6.  
  7. module Twitter
  8.   class Base
  9.     def not_following_back
  10.       all_friends.names - all_followers.names
  11.     end
  12.   end
  13. end

That’s all there’s to it (I am not a big fan of monkey patching core classes btw ; but in this case, xanax online, adding the names() method to the Array class just made the method I intended to originally implement much cleaner so I rolled with it). Note that since subtraction is a non-commutative operation, xanax online, all_friends.names - all_followers.names is not necessarily the same as allfollowers.names - allfriends.names. Xanax online, This is how the final code looks like:

  1. require ‘rubygems’
  2. require ‘twitter’
  3.  
  4. connection =  Twitter::Base.new(‘yourname’, xanax online, ‘yourpass’)
  5.  
  6. class Array
  7.   def names
  8.     self.map{|u| u.screen_name}
  9.   end
  10. end
  11.  
  12. module Twitter
  13.   class Base
  14.     alias_method :throw_method_missing, xanax online, :method_missing
  15.  
  16.     def method_missing(method_name, xanax online, *args, xanax online, &bloke)
  17.       if (method_name.to_s =~ /^all_.+/)
  18.         all_entries(method_name.to_s[/all_(.+)/, xanax online,1], xanax online, args[0] || {})
  19.       else
  20.         throw_method_missing(method_name, xanax online, *args, xanax online, &bloke)
  21.       end
  22.     end
  23.  
  24.     def all_entries(method, xanax online, options = {})
  25.       all_entries = [] 
  26.       next_100 = self.send method, xanax online, {:page => (current_page = 1)}.merge(options)
  27.       while (next_100.size != 0) do
  28.         all_entries << next_100
  29.         next_100 = self.send method, xanax online, {:page => (current_page += 1)}.merge(options)
  30.       end 
  31.       all_entries.flatten
  32.     end
  33.  
  34.     def not_following_back
  35.       all_friends.names - all_followers.names
  36.     end
  37.  
  38.   end 
  39. end 
  40.  
  41. p connection.not_following_back

You can download/check out the code here - do not try to copy & paste it from the text as it will be b0rk3d.

In part 2 I’d like to set up a small Sinatra app showing the above users in a list - displaying their avatar, xanax online, screen name and real name, xanax online, plus a link to remove them if you decide so. Xanax online,

Similar Posts:viagra pharmacy,xanax online,reductil no prescription,order viagra on internet,propecia pills

Order lexapro

Friday, February 6th, 2009

redo.pngI spent the first 2+ years in Ruby-land without even knowing about the probably most underused (and underrated) keyword of the language: redo. Order lexapro, Even after I came across the first example and liked it immensely, order lexapro, I could not come up with another use for it, order lexapro, so I threw it to the bottom of my toolbox. Order lexapro, Then I found another example, order lexapro, and another one - so I came to the conclusion that redo might be a valuable keyword in your Ruby arsenal after all - it is one of those things which you rarely need, order lexapro, but if you need it, order lexapro, it’s a perfect solution which would be cumbersome to replace with other constructs.

Break vs Next vs Redo

I am sure everyone is familiar with redo’s cousins: next and break. Order lexapro, While next and break allow you to skip an iteration, order lexapro, resp. Order lexapro, skip the rest of the remaining iterations altogether, order lexapro, redo does not skip anything - it ‘restarts’ the same iteration. Order lexapro, In other words, order lexapro, while next transfers control to the end of the iteration block, order lexapro, redo ‘jumps’ to the beginning of the block. Order lexapro, Let’s see an example, order lexapro, where redo is used to recover from an input error (code from “The Ruby Programming Language”):

  1. puts "Please enter the first word you can think of"
  2. words =%w(apple banana cherry)
  3. response = words.collect do |word|
  4.   print word + "> "
  5.   response = gets.chop
  6.   if response.size == 0
  7.     word.upcase!
  8.     redo
  9.   end
  10.   response
  11. end

The trick is that whenever the user enters an empty string, order lexapro, the block is restarted with redo, order lexapro, asking the same ‘question’ until the user enters something that makes sense to the system.

A Real Example - Tail Recursion

As of now, order lexapro, Ruby does not support tail recursion optimization, order lexapro, so you either have to write the function in a non-recursive (i.e. Order lexapro, iterative) way yourself, order lexapro, or suffer the consequences of a full stack… Order lexapro, or use redo!

I recently came across Magnus Holm’s article on this problem. Order lexapro, Magnus provides an elegant solution using redo:

  1. define_method(:acc) do |i, order lexapro, n, order lexapro, result|
  2.   if i == -1
  3.     result
  4.   else
  5.     i, order lexapro, n, order lexapro, result = i - 1, order lexapro, n + result, order lexapro, n
  6.     redo
  7.   end
  8. end
  9.  
  10. def fib(i)
  11.   acc(i, order lexapro, 1, order lexapro, 0)
  12. end

Magnus even worked around a redo perk: “The redo statement restarts the current iteration of a loop or iterator”. Order lexapro, That means he couldn’t define a method with “def”, order lexapro, since that neither involves a loop nor an iterator. Order lexapro, Maybe a less esoteric way relying on the same fact would be to use a lambda:

  1. def fib(i)
  2.   acc = lambda do |i, order lexapro, n, order lexapro, result|
  3.     if i == -1
  4.       result
  5.     else
  6.       i, order lexapro, n, order lexapro, result = i - 1, order lexapro, n + result, order lexapro, n
  7.       redo
  8.     end
  9.   end.call(i, order lexapro, 1, order lexapro, 0)
  10. end

Golf Time

Here is a Ruby golf riddle, order lexapro, which I have no idea how could be made this short (42 characters) without redo (via coderrr):

"Generate a random string of length 4 or more consisting of only the following 
set of characters: a-z, order lexapro, A-Z, order lexapro, 0-9"

Here is the winner, order lexapro, Magnus Holm’s solution (The name sounds familiar? Right, order lexapro, the previous example was from him too - he understands the power of redo for sure :-)).

  1. (0..9).map{rand(?z).chr[/[^_\W]/]||redo}*""

The above solution might not be instantly crystal clear - so let’s break it down a bit:

  1. rand(?z) is equivalent to rand(122) (but shaves off a character!)
  2. rand(?z).chr turns the random number into a string (equivalent to its ASCII code, order lexapro, i.e. Order lexapro, 65.chr == “A” etc)
  3. the regexp /[^_\W]/ means the negation of ‘everything but underscore or non-word character (digits and numbers). Order lexapro, To put it another way, order lexapro, accept word characters but not an underscore. Order lexapro, (\w includes underscore, order lexapro, so we need to get rid of that)
  4. rand(?z).chr[/[^_\W]/] acts as an identity function for letters and digits, order lexapro, but returns nil for everything else. Order lexapro, So “a”[/[^_\W]/] == “a” but “@”[/[^_\W]/] == nil. Order lexapro, “_”[/[^_\W]/] also returns nil.
  5. And now comes the redo trick: rand(?z).chr[/[^_\W]/] || redo will restart the block until a random character/letter is not found! w00t!

While one might argue that golfing examples are contrived by definition, order lexapro, I don’t always agree: the [/[^_\W]/] is a neat trick to get numbers/letters from a string only, order lexapro, as well as the use of redo is valid and non-contrived.

Conclusion

As demonstrated above, order lexapro, redo can be used in a variety of situations to produce an elegant solution that would look much more cumbersome without it. Order lexapro, Of course all the usual stuff applies: don’t overuse, order lexapro, abuse etc. Order lexapro, etc.

If you have any other interesting cases for redo, order lexapro, leave a comment - I’d love to see some more :-)

Similar Posts:viagra prescription,order lexapro,buy cheap xanax,buy cheap doxycycline online,buy cheap viagra online


Bad Behavior has blocked 1233 access attempts in the last 7 days.