Hidden features of Ruby
Asked Answered
W

46

159

Continuing the "Hidden features of ..." meme, let's share the lesser-known but useful features of Ruby programming language.

Try to limit this discussion with core Ruby, without any Ruby on Rails stuff.

See also:

(Please, just one hidden feature per answer.)

Thank you

Wenonawenonah answered 15/9, 2008 at 15:34 Comment(1)
should be community wikiOast
C
80

From Ruby 1.9 Proc#=== is an alias to Proc#call, which means Proc objects can be used in case statements like so:

def multiple_of(factor)
  Proc.new{|product| product.modulo(factor).zero?}
end

case number
  when multiple_of(3)
    puts "Multiple of 3"
  when multiple_of(7)
    puts "Multiple of 7"
end
Chockfull answered 15/9, 2008 at 15:34 Comment(1)
I actually wrote an gem at one point to do this, but my code was (a) a mess, and (b) slow. I'm very glad that the functionality has made it into core.Thales
T
76

Peter Cooper has a good list of Ruby tricks. Perhaps my favorite of his is allowing both single items and collections to be enumerated. (That is, treat a non-collection object as a collection containing just that object.) It looks like this:

[*items].each do |item|
  # ...
end
Thales answered 15/9, 2008 at 15:34 Comment(4)
A more explicit (and thus nicer) form of this is Array(items).eachUigur
If items is a string you don't have to enclose it with [*…]. String.each doesn't iterate over characters as some may expect. It just returns itself to the block.Khorma
What use would this ever serve? Just curious.Corum
@Ed: it's nice if you're writing a method and want to allow the user of the method to either pass a varargs list or an Array.Thales
J
63

Don't know how hidden this is, but I've found it useful when needing to make a Hash out of a one-dimensional array:

fruit = ["apple","red","banana","yellow"]
=> ["apple", "red", "banana", "yellow"]

Hash[*fruit]    
=> {"apple"=>"red", "banana"=>"yellow"}
Jarl answered 15/9, 2008 at 15:34 Comment(1)
Note that Hash[ [["apple","red"], ["banana","yellow"] ] produces the same result.Dodger
V
54

One trick I like is to use the splat (*) expander on objects other than Arrays. Here's an example on a regular expression match:

match, text, number = *"Something 981".match(/([A-z]*) ([0-9]*)/)

Other examples include:

a, b, c = *('A'..'Z')

Job = Struct.new(:name, :occupation)
tom = Job.new("Tom", "Developer")
name, occupation = *tom
Vinous answered 15/9, 2008 at 15:34 Comment(5)
Incidentally, for the curious, this works by implicitly calling to_a on the target of the splat.Singleness
If you're not interested in the match, you can have text, number = *"text 555".match(/regexp/)[1..-1].Calypso
text, number = "Something 981".scan(/([A-z]*) ([0-9]*)/).flatten.map{|m| Integer(m) rescue m}Demp
Both good tricks, but there's got to be a point where it's too much magic, right?!Vinous
@Andrew, did you consider, that match can return nil? nil does not have method []Retired
F
51

Wow, no one mentioned the flip flop operator:

1.upto(100) do |i|
  puts i if (i == 3)..(i == 15)
end
Funambulist answered 15/9, 2008 at 15:34 Comment(4)
Right... someone's gonna have to explain this one to me. It works, but I can't figure out why.Singleness
The flip flop operator is a statefull if. Its state switches to true as soon as i == 3 and switches to false after i != 3 and i == 15. Similar to a flip-flop: en.wikipedia.org/wiki/Flip-flop_%28electronics%29Funambulist
I wouldn’t exactly call this a hidden feature, so much of an annoyance. I remember the first time I was introduced to it in #Ruby on Freenode, years ago; I’ve used basically every single feature of Ruby at some point except this one.Hold
I wouldn't call it an annoyance, it's just something you haven't used. I use it and it can reduce code nicely, especially when I am grabbing blocks of lines from files based on some criteria.Linguistician
S
49

One of the cool things about ruby is that you can call methods and run code in places other languages would frown upon, such as in method or class definitions.

For instance, to create a class that has an unknown superclass until run time, i.e. is random, you could do the following:

class RandomSubclass < [Array, Hash, String, Fixnum, Float, TrueClass].sample

end

RandomSubclass.superclass # could output one of 6 different classes.

This uses the 1.9 Array#sample method (in 1.8.7-only, see Array#choice), and the example is pretty contrived but you can see the power here.

Another cool example is the ability to put default parameter values that are non fixed (like other languages often demand):

def do_something_at(something, at = Time.now)
   # ...
end

Of course the problem with the first example is that it is evaluated at definition time, not call time. So, once a superclass has been chosen, it stays that superclass for the remainder of the program.

However, in the second example, each time you call do_something_at, the at variable will be the time that the method was called (well, very very close to it)

Sausa answered 15/9, 2008 at 15:34 Comment(5)
Note: Array#rand is provided by ActiveSupport which you can use outside of Rails as easily as require 'activesupport'Suffer
Array#choice is 1.8.7 only! Don't use it, it's gone in 1.9 and will be gone in 1.8.8. Use #sampleDodger
python: class DictList([dict,list][random.randint(0,1)]): passContractive
def do_something_at(something, at = lambda{Time.now}) at.call #now dynamically assign time endMattox
You can also call methods on the object you're calling the method on in the default parameter list.Phenice
V
47

Another tiny feature - convert a Fixnum into any base up to 36:

>> 1234567890.to_s(2)
=> "1001001100101100000001011010010"

>> 1234567890.to_s(8)
=> "11145401322"

>> 1234567890.to_s(16)
=> "499602d2"

>> 1234567890.to_s(24)
=> "6b1230i"

>> 1234567890.to_s(36)
=> "kf12oi"

And as Huw Walters has commented, converting the other way is just as simple:

>> "kf12oi".to_i(36)
=> 1234567890
Vinous answered 15/9, 2008 at 15:34 Comment(1)
And for completeness, String#to_s(base) can be used to convert back to an integer; "1001001100101100000001011010010".to_i(2), "499602d2".to_i(16) etc all return the original Fixnum.Leguminous
E
40

Hashes with default values! An array in this case.

parties = Hash.new {|hash, key| hash[key] = [] }
parties["Summer party"]
# => []

parties["Summer party"] << "Joe"
parties["Other party"] << "Jane"

Very useful in metaprogramming.

Epstein answered 15/9, 2008 at 15:34 Comment(1)
yeah true. Ruby hash can accept '<<' operator if there's already default value assign with '=' (don't care even if it's empty assignment) otherwise the hash wont accept '<<'. CMIIWGeneric
C
38

Another fun addition in 1.9 Proc functionality is Proc#curry which allows you to turn a Proc accepting n arguments into one accepting n-1. Here it is combined with the Proc#=== tip I mentioned above:

it_is_day_of_week = lambda{ |day_of_week, date| date.wday == day_of_week }
it_is_saturday = it_is_day_of_week.curry[6]
it_is_sunday = it_is_day_of_week.curry[0]

case Time.now
when it_is_saturday
  puts "Saturday!"
when it_is_sunday
  puts "Sunday!"
else
  puts "Not the weekend"
end
Chockfull answered 15/9, 2008 at 15:34 Comment(0)
K
37

Download Ruby 1.9 source, and issue make golf, then you can do things like this:

make golf

./goruby -e 'h'
# => Hello, world!

./goruby -e 'p St'
# => StandardError

./goruby -e 'p 1.tf'
# => 1.0

./goruby19 -e 'p Fil.exp(".")'
"/home/manveru/pkgbuilds/ruby-svn/src/trunk"

Read the golf_prelude.c for more neat things hiding away.

Karttikeya answered 15/9, 2008 at 15:34 Comment(0)
B
35

Boolean operators on non boolean values.

&& and ||

Both return the value of the last expression evaluated.

Which is why the ||= will update the variable with the value returned expression on the right side if the variable is undefined. This is not explicitly documented, but common knowledge.

However the &&= isn't quite so widely known about.

string &&= string + "suffix"

is equivalent to

if string
  string = string + "suffix"
end

It's very handy for destructive operations that should not proceed if the variable is undefined.

Bankbook answered 15/9, 2008 at 15:34 Comment(1)
More precisely, string &&= string + "suffix" is equivalent to string = string && string + "suffix". That && and || return their second argument is discussed in the PickAx, p. 154 (Part I - Facets of Ruby, Expressions, Conditional Execution).Chatav
C
29

The Symbol#to_proc function that Rails provides is really cool.

Instead of

Employee.collect { |emp| emp.name }

You can write:

Employee.collect(&:name)
Colotomy answered 15/9, 2008 at 15:34 Comment(5)
This is, apparently, an "order of magnitude slower" than using a block. igvita.com/2008/07/08/6-optimization-tips-for-ruby-mriPepperandsalt
I just tried it out, and found there was no significant difference between the two. I'm not sure where this "order of magnitude" stuff came from. (Using Ruby 1.8.7)Mascot
Doing this outside of Rails is also handy and can be done with require 'activesupport' since that's actually where most of these helpers are from.Suffer
this used to be slow because of active_support's implemenetation, i.e. it accepted multiple arguments so you could do cool shit like (1..10).inject &:*, but the main use case was often only calling a method on each member of a collection e.g. %w(the quick brown fox).map &:upcase. as of 1.8.7 it's core ruby and performance is reasonable.Bullins
@thenduks: And it can be done without activesupport's help in ruby 1.8.7 and 1.9.Calypso
V
28

One final one - in ruby you can use any character you want to delimit strings. Take the following code:

message = "My message"
contrived_example = "<div id=\"contrived\">#{message}</div>"

If you don't want to escape the double-quotes within the string, you can simply use a different delimiter:

contrived_example = %{<div id="contrived-example">#{message}</div>}
contrived_example = %[<div id="contrived-example">#{message}</div>]

As well as avoiding having to escape delimiters, you can use these delimiters for nicer multiline strings:

sql = %{
    SELECT strings 
    FROM complicated_table
    WHERE complicated_condition = '1'
}
Vinous answered 15/9, 2008 at 15:34 Comment(2)
not any character, but it's still pretty cool. It also works with other literals: %() / %{} / %[] / %<> / %|| %r() / %r{} / %r[] / %r<> / %r|| %w() / %w{} / %w[] / %w<> / %w||Nerve
There's also the herenow doc syntax: <<BLOCK ... BLOCK , which I like to use for things like multiline SQL statements etc.Phenice
C
26

Use a Range object as an infinite lazy list:

Inf = 1.0 / 0

(1..Inf).take(5) #=> [1, 2, 3, 4, 5]

More info here: http://banisterfiend.wordpress.com/2009/10/02/wtf-infinite-ranges-in-ruby/

Carrew answered 15/9, 2008 at 15:34 Comment(2)
The lazy_select in the linked article is very neat.Repress
This is really awesome. I like how Infinity is a float, that way when I tried this: (-Inf..Inf).take(4) it raised a (logically consistent) can't iterate from float error. :DBluebird
L
25

I find using the define_method command to dynamically generate methods to be quite interesting and not as well known. For example:

((0..9).each do |n|
    define_method "press_#{n}" do
      @number = @number.to_i * 10 + n
    end
  end

The above code uses the 'define_method' command to dynamically create the methods "press1" through "press9." Rather then typing all 10 methods which essentailly contain the same code, the define method command is used to generate these methods on the fly as needed.

Lomasi answered 15/9, 2008 at 15:34 Comment(1)
The only problem with define_method is that it doesn't allow blocks to be passed as parameters in ruby 1.8. See this blog post for a workaround.Calypso
B
23

Short inject, like such:

Sum of range:

(1..10).inject(:+)
=> 55
Berardo answered 15/9, 2008 at 15:34 Comment(3)
Worth noting you need Ruby 1.9 or Rails with Ruby 1.8 for this to work.Khorma
@Max Howell: or require 'backports' :-)Dodger
Isn't this a duplicate of hoyhoy's answer?Calypso
F
23

module_function

Module methods that are declared as module_function will create copies of themselves as private instance methods in the class that includes the Module:

module M
  def not!
    'not!'
  end
  module_function :not!
end

class C
  include M

  def fun
    not!
  end
end

M.not!     # => 'not!
C.new.fun  # => 'not!'
C.new.not! # => NoMethodError: private method `not!' called for #<C:0x1261a00>

If you use module_function without any arguments, then any module methods that comes after the module_function statement will automatically become module_functions themselves.

module M
  module_function

  def not!
    'not!'
  end

  def yea!
    'yea!'
  end
end


class C
  include M

  def fun
    not! + ' ' + yea!
  end
end
M.not!     # => 'not!'
M.yea!     # => 'yea!'
C.new.fun  # => 'not! yea!'
Frenchify answered 15/9, 2008 at 15:34 Comment(3)
If you just want to declare private methods in modules, just use the private keyword. In addition to making the method private in classes that include the module, module_function copies the method to the module instance. In most cases this is not what you want.Vinous
I know you can just use private. But this is a question on Ruby's hidden features. And, I think most people have never heard of module_function (myself included) until they see it in the doc and start to play around with it.Frenchify
An alternative to using module_function (2nd way) is to just use extend self (which looks pretty nice :D)Ibis
C
21

Warning: this item was voted #1 Most Horrendous Hack of 2008, so use with care. Actually, avoid it like the plague, but it is most certainly Hidden Ruby.

Superators Add New Operators to Ruby

Ever want a super-secret handshake operator for some unique operation in your code? Like playing code golf? Try operators like -~+~- or <--- That last one is used in the examples for reversing the order of an item.

I have nothing to do with the Superators Project beyond admiring it.

Canst answered 15/9, 2008 at 15:34 Comment(0)
E
19

Auto-vivifying hashes in Ruby

def cnh # silly name "create nested hash"
  Hash.new {|h,k| h[k] = Hash.new(&h.default_proc)}
end
my_hash = cnh
my_hash[1][2][3] = 4
my_hash # => { 1 => { 2 => { 3 =>4 } } }

This can just be damn handy.

Equiangular answered 15/9, 2008 at 15:34 Comment(1)
I would wrap it in a module to have same feeling like native hash init: module InfHash; def self.new; Hash.new {|h,k| h[k] = Hash.new(&h.default_proc)}; end; endWickliffe
R
19

I'm late to the party, but:

You can easily take two equal-length arrays and turn them into a hash with one array supplying the keys and the other the values:

a = [:x, :y, :z]
b = [123, 456, 789]

Hash[a.zip(b)]
# => { :x => 123, :y => 456, :z => 789 }

(This works because Array#zip "zips" up the values from the two arrays:

a.zip(b)  # => [[:x, 123], [:y, 456], [:z, 789]]

And Hash[] can take just such an array. I've seen people do this as well:

Hash[*a.zip(b).flatten]  # unnecessary!

Which yields the same result, but the splat and flatten are wholly unnecessary--perhaps they weren't in the past?)

Roulers answered 15/9, 2008 at 15:34 Comment(1)
This was indeed undocumented for a long while (see redmine.ruby-lang.org/issues/show/1385 ). Note that this new form is new to Ruby 1.8.7Dodger
C
16

Destructuring an Array

(a, b), c, d = [ [:a, :b ], :c, [:d1, :d2] ]

Where:

a #=> :a
b #=> :b
c #=> :c
d #=> [:d1, :d2]

Using this technique we can use simple assignment to get the exact values we want out of nested array of any depth.

Carrew answered 15/9, 2008 at 15:34 Comment(0)
O
15

Class.new()

Create a new class at run time. The argument can be a class to derive from, and the block is the class body. You might also want to look at const_set/const_get/const_defined? to get your new class properly registered, so that inspect prints out a name instead of a number.

Not something you need every day, but quite handy when you do.

Ole answered 15/9, 2008 at 15:34 Comment(3)
MyClass = Class.new Array do; def hi; 'hi'; end; end seems to be equivalent to class MyClass < Array; def hi; 'hi'; end; end.Farcical
Probably more true than I had thought about. It even appears that you can inherit from a variable rather than only a constant. However, the sugared version (second) doesn't appear to work if you need to construct the class name at run time. (Baring eval, of course.)Ole
This technique is pretty well described in the book Metaprogramming Ruby.Propellant
C
13

create an array of consecutive numbers:

x = [*0..5]

sets x to [0, 1, 2, 3, 4, 5]

Carrew answered 15/9, 2008 at 15:34 Comment(3)
Yep,but it's not as short and sweet ;)Carrew
terseness is objective, readability is matter of taste and experienceRetired
The splat (*) operator basically calls to_a anyway.Vining
C
13

A lot of the magic you see in Rubyland has to do with metaprogramming, which is simply writing code that writes code for you. Ruby's attr_accessor, attr_reader, and attr_writer are all simple metaprogramming, in that they create two methods in one line, following a standard pattern. Rails does a whole lot of metaprogramming with their relationship-management methods like has_one and belongs_to.

But it's pretty simple to create your own metaprogramming tricks using class_eval to execute dynamically-written code.

The following example allows a wrapper object to forwards certain methods along to an internal object:

class Wrapper
  attr_accessor :internal

  def self.forwards(*methods)
    methods.each do |method|
      define_method method do |*arguments, &block|
        internal.send method, *arguments, &block
      end
    end
  end

  forwards :to_i, :length, :split
end

w = Wrapper.new
w.internal = "12 13 14"
w.to_i        # => 12
w.length      # => 8
w.split('1')  # => ["", "2 ", "3 ", "4"]

The method Wrapper.forwards takes symbols for the names of methods and stores them in the methods array. Then, for each of those given, we use define_method to create a new method whose job it is to send the message along, including all arguments and blocks.

A great resource for metaprogramming issues is Why the Lucky Stiff's "Seeing Metaprogramming Clearly".

Cissy answered 15/9, 2008 at 15:34 Comment(3)
I wish to dive head first into metaprogramming in ruby. Could you provide some references to get started with it (Other than the given link)? Books will do too. Thanks.Rosenblum
PragProg's videocasting serie "The Ruby Object Model and Metaprogramming" its a good introduction to meta programming using ruby: pragprog.com/screencasts/v-dtrubyom/…Sagacity
@Chirantan, have a look at Metaprogramming Ruby.Propellant
T
12

use anything that responds to ===(obj) for case comparisons:

case foo
when /baz/
  do_something_with_the_string_matching_baz
when 12..15
  do_something_with_the_integer_between_12_and_15
when lambda { |x| x % 5 == 0 }
  # only works in Ruby 1.9 or if you alias Proc#call as Proc#===
  do_something_with_the_integer_that_is_a_multiple_of_5
when Bar
  do_something_with_the_instance_of_Bar
when some_object
  do_something_with_the_thing_that_matches_some_object
end

Module (and thus Class), Regexp, Date, and many other classes define an instance method :===(other), and can all be used.

Thanks to Farrel for the reminder of Proc#call being aliased as Proc#=== in Ruby 1.9.

Thales answered 15/9, 2008 at 15:34 Comment(0)
K
11

The "ruby" binary (at least MRI's) supports a lot of the switches that made perl one-liners quite popular.

Significant ones:

  • -n Sets up an outer loop with just "gets" - which magically works with given filename or STDIN, setting each read line in $_
  • -p Similar to -n but with an automatic puts at the end of each loop iteration
  • -a Automatic call to .split on each input line, stored in $F
  • -i In-place edit input files
  • -l Automatic call to .chomp on input
  • -e Execute a piece of code
  • -c Check source code
  • -w With warnings

Some examples:

# Print each line with its number:
ruby -ne 'print($., ": ", $_)' < /etc/irbrc

# Print each line reversed:
ruby -lne 'puts $_.reverse' < /etc/irbrc

# Print the second column from an input CSV (dumb - no balanced quote support etc):
ruby -F, -ane 'puts $F[1]' < /etc/irbrc

# Print lines that contain "eat"
ruby -ne 'puts $_ if /eat/i' < /etc/irbrc

# Same as above:
ruby -pe 'next unless /eat/i' < /etc/irbrc

# Pass-through (like cat, but with possible line-end munging):
ruby -p -e '' < /etc/irbrc

# Uppercase all input:
ruby -p -e '$_.upcase!' < /etc/irbrc

# Same as above, but actually write to the input file, and make a backup first with extension .bak - Notice that inplace edit REQUIRES input files, not an input STDIN:
ruby -i.bak -p -e '$_.upcase!' /etc/irbrc

Feel free to google "ruby one-liners" and "perl one-liners" for tons more usable and practical examples. It essentially allows you to use ruby as a fairly powerful replacement to awk and sed.

Kiangsu answered 15/9, 2008 at 15:34 Comment(0)
L
10

The send() method is a general-purpose method that can be used on any Class or Object in Ruby. If not overridden, send() accepts a string and calls the name of the method whose string it is passed. For example, if the user clicks the “Clr” button, the ‘press_clear’ string will be sent to the send() method and the ‘press_clear’ method will be called. The send() method allows for a fun and dynamic way to call functions in Ruby.

 %w(7 8 9 / 4 5 6 * 1 2 3 - 0 Clr = +).each do |btn|
    button btn, :width => 46, :height => 46 do
      method = case btn
        when /[0-9]/: 'press_'+btn
        when 'Clr': 'press_clear'
        when '=': 'press_equals'
        when '+': 'press_add'
        when '-': 'press_sub'
        when '*': 'press_times'
        when '/': 'press_div'
      end

      number.send(method)
      number_field.replace strong(number)
    end
  end

I talk more about this feature in Blogging Shoes: The Simple-Calc Application

Lomasi answered 15/9, 2008 at 15:34 Comment(2)
Sounds like a great way to open a security hole.Condensate
I'd use symbols wherever possible.Dyne
J
9
private unless Rails.env == 'test'
# e.g. a bundle of methods you want to test directly

Looks like a cool and (in some cases) nice/useful hack/feature of Ruby.

Jedediah answered 15/9, 2008 at 15:34 Comment(0)
C
9

Defining a method that accepts any number of parameters and just discards them all

def hello(*)
    super
    puts "hello!"
end

The above hello method only needs to puts "hello" on the screen and call super - but since the superclass hello defines parameters it has to as well - however since it doesn't actually need to use the parameters itself - it doesn't have to give them a name.

Carrew answered 15/9, 2008 at 15:34 Comment(0)
O
9

Fool some class or module telling it has required something that it really hasn't required:

$" << "something"

This is useful for example when requiring A that in turns requires B but we don't need B in our code (and A won't use it either through our code):

For example, Backgroundrb's bdrb_test_helper requires 'test/spec', but you don't use it at all, so in your code:

$" << "test/spec"
require File.join(File.dirname(__FILE__) + "/../bdrb_test_helper")
Octagonal answered 15/9, 2008 at 15:34 Comment(2)
Does this fix problems where gem A requires foo-1.0.0, and gem B requires foo-1.0.1?Calypso
No because "something"'s code won't be available: this only simulates that "something" is required, but it is really doesn't require it. $" is an array containing the module names loaded by require (it's used by require to prevent loading modules twice). So, if you use this to fool gems, that will produce a crash when the gems try to use the actual "something" code, because it won't exist. You may instead want to force laoding a concrete version of a gem (eg foo-1.0.0), instead of the latest one: docs.rubygems.org/read/chapter/4#page71Octagonal
I
8

To combine multiple regexes with |, you can use

Regexp.union /Ruby\d/, /test/i, "cheat"

to create a Regexp similar to:

/(Ruby\d|[tT][eE][sS][tT]|cheat)/
Ibis answered 15/9, 2008 at 15:34 Comment(0)
S
8

Fixnum#to_s(base) can be really useful in some case. One such case is generating random (pseudo)unique tokens by converting random number to string using base of 36.

Token of length 8:

rand(36**8).to_s(36) => "fmhpjfao"
rand(36**8).to_s(36) => "gcer9ecu"
rand(36**8).to_s(36) => "krpm0h9r"

Token of length 6:

rand(36**6).to_s(36) => "bvhl8d"
rand(36**6).to_s(36) => "lb7tis"
rand(36**6).to_s(36) => "ibwgeh"
Saccule answered 15/9, 2008 at 15:34 Comment(0)
P
8

I find this useful in some scripts. It makes it possible to use environment variables directly, like in shell scripts and Makefiles. Environment variables are used as fall-back for undefined Ruby constants.

>> class <<Object
>>  alias :old_const_missing :const_missing
>>  def const_missing(sym)
>>   ENV[sym.to_s] || old_const_missing(sym)
>>  end
>> end
=> nil

>> puts SHELL
/bin/zsh
=> nil
>> TERM == 'xterm'
=> true
Powered answered 15/9, 2008 at 15:34 Comment(0)
L
8

How about opening a file based on ARGV[0]?

readfile.rb:

$<.each_line{|l| puts l}

ruby readfile.rb testfile.txt

It's a great shortcut for writing one-off scripts. There's a whole mess of pre-defined variables that most people don't know about. Use them wisely (read: don't litter a code base you plan to maintain with them, it can get messy).

Likker answered 15/9, 2008 at 15:34 Comment(1)
ARGF is a little more memorable than $<, IMHO.Pahoehoe
L
5

I'm a fan of:

%w{An Array of strings} #=> ["An", "Array", "of", "Strings"]

It's sort of funny how often that's useful.

Legator answered 15/9, 2008 at 15:34 Comment(1)
I use it a fair bit as well. You can use a delimiter %w{A two\ word example} if you need to avoid splitting on a certain space.Calypso
O
4

Multiple return values

def getCostAndMpg
    cost = 30000  # some fancy db calls go here
    mpg = 30
    return cost,mpg
end
AltimaCost, AltimaMpg = getCostAndMpg
puts "AltimaCost = #{AltimaCost}, AltimaMpg = #{AltimaMpg}"

Parallel Assignment

i = 0
j = 1
puts "i = #{i}, j=#{j}"
i,j = j,i
puts "i = #{i}, j=#{j}"

Virtual Attributes

class Employee < Person
  def initialize(fname, lname, position)
    super(fname,lname)
    @position = position
  end
  def to_s
     super + ", #@position"
  end
  attr_writer :position
  def etype
     if @position == "CEO" || @position == "CFO"
         "executive"
     else
         "staff"
     end
  end
end
employee = Employee.new("Augustus","Bondi","CFO")
employee.position = "CEO"
puts employee.etype    =>  executive
employee.position = "Engineer"
puts employee.etype    =>  staff

method_missing - a wonderful idea

(In most languages when a method cannot be found and error is thrown and your program stops. In ruby you can actually catch those errors and perhaps do something intelligent with the situation)

class MathWiz
  def add(a,b) 
    return a+b
  end
  def method_missing(name, *args)
    puts "I don't know the method #{name}"
  end
end
mathwiz = MathWiz.new
puts mathwiz.add(1,4)
puts mathwiz.subtract(4,2)

5

I don't know the method subtract

nil

Outshoot answered 15/9, 2008 at 15:34 Comment(1)
That's not so much "virtual attributes" as it is the fact that Ruby doesn't require parenthesis on method calls and thus there's no syntactical difference in calling a method on an object or retrieving an attribute of one.Aurelia
D
4

Calling a method defined anywhere in the inheritance chain, even if overridden

ActiveSupport's objects sometimes masquerade as built-in objects.

require 'active_support'
days = 5.days
days.class  #=> Fixnum
days.is_a?(Fixnum)  #=> true
Fixnum === days  #=> false (huh? what are you really?)
Object.instance_method(:class).bind(days).call  #=> ActiveSupport::Duration (aha!)
ActiveSupport::Duration === days  #=> true

The above, of course, relies on the fact that active_support doesn't redefine Object#instance_method, in which case we'd really be up a creek. Then again, we could always save the return value of Object.instance_method(:class) before any 3rd party library is loaded.

Object.instance_method(...) returns an UnboundMethod which you can then bind to an instance of that class. In this case, you can bind it to any instance of Object (subclasses included).

If an object's class includes modules, you can also use the UnboundMethod from those modules.

module Mod
  def var_add(more); @var+more; end
end
class Cla
  include Mod
  def initialize(var); @var=var; end
  # override
  def var_add(more); @var+more+more; end
end
cla = Cla.new('abcdef')
cla.var_add('ghi')  #=> "abcdefghighi"
Mod.instance_method(:var_add).bind(cla).call('ghi')  #=> "abcdefghi"

This even works for singleton methods that override an instance method of the class the object belongs to.

class Foo
  def mymethod; 'original'; end
end
foo = Foo.new
foo.mymethod  #=> 'original'
def foo.mymethod; 'singleton'; end
foo.mymethod  #=> 'singleton'
Foo.instance_method(:mymethod).bind(foo).call  #=> 'original'

# You can also call #instance method on singleton classes:
class << foo; self; end.instance_method(:mymethod).bind(foo).call  #=> 'singleton'
Dissimilate answered 15/9, 2008 at 15:34 Comment(0)
K
4

James A. Rosen's tip is cool ([*items].each), but I find that it destroys hashes:

irb(main):001:0> h = {:name => "Bob"}
=> {:name=>"Bob"}
irb(main):002:0> [*h]
=> [[:name, "Bob"]]

I prefer this way of handling the case when I accept a list of things to process but am lenient and allow the caller to supply one:

irb(main):003:0> h = {:name => "Bob"}
=> {:name=>"Bob"}
irb(main):004:0> [h].flatten
=> [{:name=>"Bob"}]

This can be combined with a method signature like so nicely:

def process(*entries)
  [entries].flatten.each do |e|
    # do something with e
  end
end
Kiangsu answered 15/9, 2008 at 15:34 Comment(0)
M
3

There are some aspects of symbol literals that people should know. One case solved by special symbol literals is when you need to create a symbol whose name causes a syntax error for some reason with the normal symbol literal syntax:

:'class'

You can also do symbol interpolation. In the context of an accessor, for example:

define_method :"#{name}=" do |value|
  instance_variable_set :"@#{name}", value
end
Miffy answered 15/9, 2008 at 15:34 Comment(0)
G
3

each_with_index method for any enumarable object ( array,hash,etc.) perhaps?

myarray = ["la", "li", "lu"]
myarray.each_with_index{|v,idx| puts "#{idx} -> #{v}"}

#result:
#0 -> la
#1 -> li
#2 -> lu

Maybe it's more well known than other answers but not that well known for all ruby programmers :)

Generic answered 15/9, 2008 at 15:34 Comment(1)
With 1.9, you get #with_index on every Enumerator, aka myarray.each.with_index or even myarray.map.with_index.Ferric
S
3

I just love the inline keyword rescue like this:
EDITED EXAMPLE:

@user #=> nil (but I did't know)
@user.name rescue "Unknown"
link_to( d.user.name, url_user( d.user.id, d.user.name)) rescue 'Account removed'

This avoid breaking my App and is way better than the feature released at Rails .try()

Shea answered 15/9, 2008 at 15:34 Comment(5)
Yep, also a great way to silently break the app.. one must be really careful using!Offwhite
How is it better than the NilClass#try method?. try specifically is for this case and is explicit that you are protecting against unexpected nils instead of any exception. Inline rescue does have its place, but it isn't here, imo. A better way to solve your problem would be: @user.try(:name) || "Unknown". Same result, but much clearer and safer.Nerve
In the comments of a duplicate answer, Joe Martinez suggests using the "andand" gem.Calypso
Sometimes I use .try, but all it does is return nil, sometimes, it is not enoughOffwhite
The inline rescue can be subtly different than try. .try is only defined on nil. So the inline rescue can be used on multiple chained calls ( try.try.try is a bit ugly) but it can also handle other exceptions besides simply a nil object exception.Pratique
S
2

I just read all the answers... one notable omission was destructuring assignment:

> (a,b),c = [[1,2],3]
=> [[1,2],3]
> a
=> 1

It also works for block parameters. This is useful when you have nested arrays, each element of which represents something distinct. Instead of writing code like "array[0][1]", you can break that nested array down and give a descriptive name to each element, in a single line of code.

Suffumigate answered 15/9, 2008 at 15:34 Comment(0)
R
2
class A

  private

  def my_private_method
    puts 'private method called'
  end
end

a = A.new
a.my_private_method # Raises exception saying private method was called
a.send :my_private_method # Calls my_private_method and prints private method called'
Rosenblum answered 15/9, 2008 at 15:34 Comment(11)
you can also use instance_eval to get to private methods and variables.Boman
Right. But the question is, should it be allowed? It completely violates the principle of encapsulation.Rosenblum
There's a ton of libraries out there that've taken advantage of this "feature" I'm afraid.Singleness
The purpose of private methods is to hide them from general use so that if/when they change, code doesn't break. If you know what you are doing - go ahead and call a private method. The fact that Ruby forces you to use the quite different looking call (with .send) makes it obvious that this call is special. Most dynamic languages let you circumvent private method definitions because sometimes it's handy.Suffer
@Sporkmonger if thats true then, all those libraries would break from Ruby 1.9 onwards.Rosenblum
Visibility rules in most languages are meant to protect you from mistakes, not fraud, as Stroustrop said.Wally
Add this to "Ruby Worst Practices" list.Fireworm
@everyone who hates this: its obviously a hack. you shouldn't use it except as a last resort, just like all other hacks, but when you do need it, you will be pretty glad its there, again, just like all other hacks.Malaise
@macek: I want to ask a question on abusing ruby for fun and profit!Calypso
yeah they should really require you to call "send_private" or something, to make this more obvious...Sheliasheline
@rogerdpack: There's send_public if you only want to send it to a public method. There don't seem to be equivalents for private or protected though...Calypso
E
2

Ruby has a call/cc mechanism allowing one to freely hop up and down the stack.

Simple example follows. This is certainly not how one would multiply a sequence in ruby, but it demonstrates how one might use call/cc to reach up the stack to short-circuit an algorithm. In this case, we're recursively multiplying a list of numbers until we either have seen every number or we see zero (the two cases where we know the answer). In the zero case, we can be arbitrarily deep in the list and terminate.

#!/usr/bin/env ruby

def rprod(k, rv, current, *nums)
  puts "#{rv} * #{current}"
  k.call(0) if current == 0 || rv == 0
  nums.empty? ? (rv * current) : rprod(k, rv * current, *nums)
end

def prod(first, *rest)
  callcc { |k| rprod(k, first, *rest) }
end

puts "Seq 1:  #{prod(1, 2, 3, 4, 5, 6)}"
puts ""
puts "Seq 2:  #{prod(1, 2, 0, 3, 4, 5, 6)}"

You can see the output here:

http://codepad.org/Oh8ddh9e

For a more complex example featuring continuations moving the other direction on the stack, read the source to Generator.

Elroyels answered 15/9, 2008 at 15:34 Comment(1)
One shouldn't use callcc. From Matz' book: Implementation difficulties prevent other implementations of Ruby (such as JRuby, the Java-based implementation) from supporting continuations. Because they are no longer well supported, continuations should be considered a curiosity, and new Ruby code should not use them.Dodger
A
1

The sprintf shortcut

My favourite ruby feature. Syntax is format_string % argument

"%04d"  % 1         # => "0001"
"%0.2f" % Math::PI  # => "3.14"

Works as well for arrays (format_string % array_of_arguments)

"%.2f %.3f %.4f" % ([Math::PI]*3) 
# => "3.14 3.142 3.1416"
Anking answered 15/9, 2008 at 15:34 Comment(0)
T
1
@user #=> nil (but I did't know)
@user.name rescue "Unknown"
Threap answered 15/9, 2008 at 15:34 Comment(1)
-1: Exact copy of Fabiano PS's answer.Calypso

© 2022 - 2024 — McMap. All rights reserved.