Skip to content

Ruby Blender

  • Blog
  • About
  • Archives
  • Log in
 
Less
More
Trim
Untrim
« Older
Home
Loading
Newer »
Archive for the 'mini-saga' Category
16Feb09 Why ||=?
mini-saga programming ruby
0 Comments

||= is a very useful technique in Ruby programming and is used like:
x ||= 5
It relies on nil evaluating as false. It works like this: a variable is equal to its value xor some other value. If the variable is nil, then the other value is used. Otherwise, it keeps its value.

irb(main):001:0> x ||= 5
=> 5
irb(main):002:0> x
=> 5
irb(main):003:0> y=2
=> 2
irb(main):004:0> y ||= 7
=> 2
irb(main):005:0> z
NameError: undefined local variable or method `z' for main:Object
from (irb):5
from :0
irb(main):006:0> z=nil
=> nil
irb(main):007:0> z ||= 3
=> 3
irb(main):008:0> m
NameError: undefined local variable or method `m' for main:Object
from (irb):8
from :0
irb(main):009:0> m ||= 1
=> 1
irb(main):010:0>

Enhanced by Zemanta
15Feb09 It went (that) way
mini-saga programming ruby
0 Comments
Logical and arithmetic rotate one bit left
Image via Wikipedia

Ruby supports operator overloading — operators behave as methods of an object. So, << behaves differently based on the context. Generally for anything other than bit shifting << means to append or concatenate, such as with a string or an array. Concatenate is generally faster than addition — new objects are not created.

matt:~$ irb
>> 32 << 3 => 256
>> "cat" << 'nip' => "catnip"
>> [0,2] << 2 => [0, 2, 2]

Enhanced by Zemanta
14Feb09 Open-uri makes for developer friendly I/O
mini-saga programming ruby
0 Comments
A Venn diagram of Uniform Resource Identifier ...
Image via Wikipedia

open-uri, a part of the standard Ruby distribution, makes opening and reading from a URI easy. With it, developers use familiar methods like open for performing I/O with URI’s.

require 'open-uri'
open("http://www.ruby-lang.org/") {|f|
f.each_line {|line| p line}
}

Contrast Net::HTTP which uses numerous methods to achieve the same effect.

require 'net/http'

url = URI.parse('http://www.example.com/index.html')
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
puts res.body

An addon, SuperIO, adds more flexiblity in attempting to DTRT.

Enhanced by Zemanta
13Feb09 More on Mini-Sagas
administrivia mini-saga
0 Comments

While I’ve decided to limit the Mini Sagas to 50 words, I’ve decided to not count the words in code snippets. Why? I’ve written one and the 50 word limit, when code snippets are included is too constraining. It was extremely vague and didn’t begin to explain what was necessary.

13Feb09 Holy Ruby Methods, Batman!
mini-saga programming ruby
0 Comments

bang!Conventions are one of Ruby‘s nicest features, making it easier to read and maintain. One convention is that methods which may modify an object have a ‘!’ in their name, such as ‘chop!’, which indicates if there are no changes, then nil is returned. This can cause issues with method chaining.

Enhanced by Zemanta
 
Browse Archives »
  • administrivia (2)
  • configuration (1)
  • metaprogramming (1)
  • mini-saga (5)
  • programming (10)
  • rails (1)
  • ruby (15)
  • rubygems (2)
  • Uncategorized (1)
 

Recent Posts

  • Default values for Attributes
  • Arguments with Trollop
  • Database Paranoia — ActiveRecord Callbacks
  • Ruby Bindings and Scope
  • What’s #method_missing missing?

Search

Browse by Category

  • administrivia (2)
  • configuration (1)
  • metaprogramming (1)
  • mini-saga (5)
  • programming (10)
  • rails (1)
  • ruby (15)
  • rubygems (2)
  • Uncategorized (1)

Browse by Tag

  • ActiveRecord
  • binding
  • Command-line interface
  • gotcha
  • irb
  • irbc
  • Languages
  • Library
  • Local variable
  • metaprogramming
  • mini-saga
  • paradigm
  • philosophy
  • programming
  • rails
  • Recursion
  • ruby
  • rubygems
  • Ruby on Rails
  • scope
  • utilities

Browse by Month

  • September 2009 (1)
  • August 2009 (4)
  • April 2009 (1)
  • February 2009 (11)
 
 
  • Blog
  • About
  • Archives
  • Log in
 


Theme Design by Jay Kwong | Powered by WordPress and K2

 

Home Top Archives Entries FeedComments Feed