Skip to content

Instantly share code, notes, and snippets.

View deepak's full-sized avatar

Deepak Kannan deepak

View GitHub Profile
@deepak
deepak / proc_binding_local_vars.rb
Created December 15, 2010 07:05
how to get the local variables enclosed by a Binding
p = Proc.new { foo = 12; p local_variables }
# why is the two different?
# what is the meaning of "_" variable
p.call #["_", "p", "foo"]
p.binding.eval("local_variables") #["_", "p"]
@deepak
deepak / chdir.rb
Created December 15, 2010 08:13
Dir#chdir in ruby
# either use the cli option
# ruby -C /this/dir chdir.rb
# or explicitly set in code. this has some options -
# missing a gotcha check 'the ruby programming language' book
#puts "=== Dir.chdir: #{Dir.chdir(File.expand_path('..',__FILE__))}"
puts "=== Dir.pwd: #{Dir.pwd}"
@deepak
deepak / proc_vars_list.rb
Created December 15, 2010 08:32
proc_vars_list
# thanks to banister
# functions are better as they have a different scope - keep the scope under control
str = "hello world"
arr = (1..100).to_a
def foo(str)
Proc.new { str.size }.binding.eval("local_variables + instance_variables")
end
@deepak
deepak / ri method_added.txt
Created December 16, 2010 07:14
confusing output of method_added in 1.8 - not defined in 1.9
> ruby -v
ruby 1.8.7 (2010-04-19 patchlevel 253) [x86_64-linux], MBARI 0x6770, Ruby Enterprise Edition 2010.02
> ri method_added
More than one method matched your request. You can refine
your search by asking for information on one of:
Kernel::method_added, Kernel::method_added, Module#method_added,
Numeric#singleton_method_added,
Shell::CommandProcessor::method_added, Object::method_added,
@deepak
deepak / defined? in ruby
Created December 16, 2010 07:31
what would u call defined? in ruby
=begin
<raggi> anyone know if there's a term that describes what the defined? keyword does? 12:44 PM
<raggi> i asked my guys today if they could describe waht it does, and was ofc met mostly with silence, i know i can describe it, but i'm not sure i know of a term that means the same thing 12:44 PM
<dkannan> raggi: type of slot - not a term as such 12:46 PM
<raggi> dkannan: that's not what defined? is 12:46 PM
<raggi> dkannan: try doing: defined?((1;1)) 12:46 PM
<dkannan> raggi: ok 12:47 PM
<dkannan> raggi: some more: defined?(1); defined?(Fixnum); defined?(Object.new); defined?(o = Object.new); 12:51 PM
<dkannan> raggi: it is like how the interpreter sees the world. 12:56 PM
<dkannan> raggi: how does interpreter dry-run sound? 12:56 PM
@deepak
deepak / ruby_eval.rb
Created December 16, 2010 08:20
ruby 1.9 eval and AST creates varibales when encountered
#dkannan: raggi: just copy-pasted the whole thing 1:04 PM
#raggi: the defined?(o = something) is basically a bug i think 1:05 PM
#raggi; not 100% on that, but i think it's an AST bug, you can do a similar thing in other ways 1:06 PM
#raggi: (that is, you can make local variables come into existence without assigning to them)
# short-circuit
puts defined? foo
false && (foo = "will this be assigned")
puts defined? foo
puts foo.inspect
@deepak
deepak / array_prof.rb
Created December 20, 2010 07:47
profiling Array#push and Array#<< using ruby-prof gem and profiler in standard lib
#!/usr/bin/env ruby
# profiling Array#push and Array#<< using ruby-prof gem
# BUG: why does "Array#<<" not show-up on the profiler but "Array#push" does
begin
require "ruby-prof"
rescue LoadError
require "rubygems"
require "ruby-prof"
@deepak
deepak / objectspace_filter.rb
Created December 21, 2010 13:52
what does ObjectSpace.each_object(Module) work. what does match mean?
# in ruby 1.9.2-p0
require 'test/unit'
class Foo < String
end
class ObSpace < Test::Unit::TestCase
def setup
@m = "hello"
@n = Foo.new("hello")
@deepak
deepak / fixmum_eigenclass.rb
Created December 22, 2010 06:52
fixnum instance does not have a eigenclass
# -*- coding: utf-8 -*-
# $ ruby -vw foobar.rb
# ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]
# foobar.rb:1: syntax error, unexpected tINTEGER
# def 1.hello
# ^
# foobar.rb:3: syntax error, unexpected keyword_end, expecting $end
class Object
def metaclass
@deepak
deepak / splat_operator.rb
Created January 3, 2011 08:03
showing how splat operator works
chunk, bacon = *["chunky", "bacon"]
puts "#{chunk} #{bacon}" #=> chunky bacon
*chunk_bacon = "chunky", "bacon"
puts chunk_bacon.inspect #=> ["chunky", "bacon"]
puts Proc.new {|*_| _.inspect }.call([12,20]) #=> "[[12, 20]]"
# can be used to collect multiple arguments in one
puts Proc.new {|*_| _.inspect }.call(12,20) #=> "[12, 20]"