December 13, 2007 at 1:04 am
· Filed under Guess Method, Rails, Ruby, irb
I was looking at the code for Giles Bowkett’s utility_belt gem, and came across something I wish I had known about ages ago. There’s a way to execute some code in an .irbrc after everything else when the irb session starts (technically, when the context changes). The problem with GuessMethod and Rails was that GuessMethod can’t load first. Problem solved.
So now my .irbrc has this line to include GuessMethod in a way that works with script/console:
IRB.conf[:IRB_RC] = Proc.new { require 'guessmethod' }
Of course, if you’re using that IRB.conf[:IRB_RC] for something else (like using utility_belt), something’s going to overwrite something.
Anyway, I’ve finally just given up and wrapped my entire .irbrc in a big unless:
unless $0 == 'script/server'
I’m confused as to why script/server loads .irbrc for every request, but at least this mitigates that issue a bit. (As an aside, can someone tell me why it does that?)
Permalink
November 29, 2007 at 6:16 pm
· Filed under 1.9, Ruby, irb
Wirble, which is awesome, uses syntax in its case blocks that break Ruby 1.9.0. It’s too bad. Wirble makes irb so much nicer to look at and use (although, I do have to say that I don’t use its prompt or history features, but maybe only through old stubbornness).
Anyway, if you’re using 1.9 alongside 1.8, it’s nice when everything works the same. And so, here’s a simple patch for wirble.rb (the only file when you install the gem). Apply it to your gems/1.9/gems/wirble-0.1.2/wirble.rb (usually in /usr/local/lib/ruby/) and there you go.
Permalink
November 13, 2007 at 12:27 am
· Filed under Guess Method, Ruby, irb
I’m starting to see a pattern. I make one change to GuessMethod, release it, then think of something else almost immediately and release again. And such brings us to 0.1.2.
I almost called this one 0.2 because I think I finally figured out how GuessMethod should really work, which is this way:
- Call the original method_missing or const_missing and let it go through unless…
- it raises a NameError or NoMethodError, in which case…
- capture the error and…
- try to guess.
If the guess is successful, apply the guess. If not, raise the original error.
Now this all seems so obvious. But so much is in hindsight.
Anyway, don’t let anyone try to trick you into thinking this is somehow something to be used in production, or even outside of irb. This doesn’t belong anywhere except in irb.
I’ve been trying to think of how to put this in your .irbrc so that things go the best way possible. I think this is it:
require 'guessmethod' if defined?(IRB) and not ENV.has_key?('RAILS_ENV')
Go get it. I swear I wouldn’t work on this unless it made irb more fun, which it totally does.
Permalink
September 2, 2007 at 11:41 pm
· Filed under Ruby, Ruby Mailing List, irb
Recently a user by the name of 7stud has been asking questions about Hash on comp.lang.ruby. In some curiosity fueled by his, I was playing around with this:
class Hash
alias_method :old_get, '[]'
def [](key)
puts "#{key} => #{old_get(key)}"
old_get(key)
end
end
I was expecting little more than (assuming a = {:b => ‘c’}):
003:0> a[:b]
b => c
"c"
But what I got (besides a whole bunch of irb config values and Wirble related output) included this:
a =>
a =>
[ => #<IRB::SLex::Node:0x13dce34>
: =>
: => #<IRB::SLex::Node:0x13dd514>
b =>
b =>
b =>
] => #<IRB::SLex::Node:0x13dd6cc>=>
] => RubyToken::TkRBRACK
=> #<IRB::SLex::Node:0x13df7b0>
It’s too late on a Sunday to look further, but it’s nice to see how much is right there for the gleaning.
Permalink
July 17, 2007 at 7:53 pm
· Filed under Guess Method, Ruby, irb
GuessMethod is probably a bad idea:
mvb:~ cms$ irb
001:0> load 'mm.rb'
true
002:0> class Object; include GuessMethod; end
Object
003:0> ['1', '2', '3'].mp {|x| x.toi}
attention: sending map instead of mp to ["1", "2", "3"]:Array
attention: sending to_i instead of toi to "1":String
attention: sending to_i instead of toi to "2":String
attention: sending to_i instead of toi to "3":String
[1, 2, 3]
004:0> ['1','2','3'].mp {|x| x.to_}
attention: sending map instead of mp to ["1", "2", "3"]:Array
ambiguous method to_, possible matches to_a, to_i, to_s, to_f for "1":String
NoMethodError: undefined method `to_' for "1":String
from ./mm.rb:9:in `old_method_missing'
from ./mm.rb:34:in `method_missing'
from (irb):4
from ./mm.rb:25:in `map'
from ./mm.rb:25:in `send'
from ./mm.rb:25:in `method_missing'
from (irb):4
from (irb):3
005:0> eixt
attention: sending exit instead of eixt to main:Object
13 seconds
mvb:~ cms$
It’s using the method_missing pre-handling trick I talked about earlier. It’s nasty. I’m not actually actively using this, but I’ve enjoyed toying around with it.
You can see after line 4 that the ambiguous ‘to_’ gets passed on to String’s method_missing, which would be cool if String had a method_missing that did something interesting. But this is a bad idea anyway. The whole thing is one giant bad idea.
Permalink
July 6, 2007 at 8:55 am
· Filed under Ruby, irb
I haven’t seen very many .irbrc files lying around on the internet. Examples, sure, I’ve seen examples, but nothing anyone would actually use. Maybe no one has taught me that things like .irbrc files are not interesting. I think they would be. I want to see more.
Here’s mine:
IRB_START_TIME = Time.now
ARGV.concat [ "--readline" ]
require 'pp'
require 'rubygems'
require 'duration'
require 'wirble'
require 'irb/completion'
require 'irb/ext/save-history'
IRB.conf[:PROMPT][:SHORT] = {
:PROMPT_C=>"%03n:%i* ",
:RETURN=>"%sn",
:PROMPT_I=>"%03n:%i> ",
:PROMPT_N=>"%03n:%i> ",
:PROMPT_S=>"%03n:%i%l "
}
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"
IRB.conf[:PROMPT_MODE] = :SHORT
Wirble.init(:skip_prompt => true, :skip_history => true)
Wirble.colorize
at_exit { puts Duration.new(Time.now - IRB_START_TIME) }
def clear
print "e[He[2J"
end
A couple of notes:
- Wirble colorizes return values in irb, and does a pretty good job at it. It also changes the prompt and the behavior of irb’s history in ways I don’t like; the
Wirble.init line takes care of those problems.
- I was always trying to clear the screen with
clear. I had to make it happen on my own. If I could suppress the nil that gets displayed afterwards, I would.
- The short prompt I make there is sort of a halfway between the standard prompt
irb(main)001:0> and the simple prompt >>. I want line numbers and indent levels! I want to know where I am.
- I also want to know when I am. So I set
IRB_START_TIME and use the Duration gem and at_exit to tell me how long my irb sessions are. It is totally worthless, but I love it. It makes me feel productive. Have a look:
mvb:~ cms$ irb
001:0> "wait a minute"
"wait a minute"
002:0> exit
20 seconds
mvb:~ cms$
I couldn’t wait a full minute.
Permalink