This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| > 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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| =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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]" |