irb

Rails check if IRB console or webpage

99封情书 提交于 2019-12-06 03:02:40
问题 In my model I would like to check if the app is running inside IRB consol or as a website? class MyModel < ActiveRecord::Base def xmethod if !isIRBconsol self.user_id = UserSession.find.user.id end end end 回答1: This is a bit of a hack, but it should work: class MyModel < ActiveRecord::Base def am_i_in_irb? self.private_methods.include? 'irb_binding' end end But as Kathy Van Stone said above, this is probably something that has a better solution. 回答2: Why not just if defined?(IRB) ? 回答3:

Reload rubygems in irb?

て烟熏妆下的殇ゞ 提交于 2019-12-05 19:37:37
问题 I've this script right now. def r(this) require this puts "#{this} is now loaded." rescue LoadError puts "The gem '#{this}' is missing." puts "Should I install it? [y/n]" data = gets if data =~ /yes|y/i puts "Installing #{this}, hold on." if `gem install #{this}` =~ /Successfully/i load this end else puts "Okey, goodbye." end end That makes it possible to require libs on the fly. Like this: r "haml" . The problem is that I can't load the gem after it has been installed. Using load this or

How to get irb and rails console to work properly in gitbash?

倾然丶 夕夏残阳落幕 提交于 2019-12-05 13:36:34
I'm using gitbash ver 2.9.0, 64-bit, on Windows 7. It uses mintty version 2.0.3. The gitbash shell most of the time seems to work fine. You can use the arrow keys, etc, as with any bash shell and they perform as expected, being able to scroll through prior commands, etc. However, when using irb or rails console (which runs irb ) it is very flakey. Rather than scroll through prior commands, the up arrow just moves the cursor up the screen and enters what are probably up arrow control codes into the input buffer. In addition, irb must be exited with ^C rather than ^D. The ^D does nothing except

How do I get the “irb(main):001:0>” prompt instead of “>>”

为君一笑 提交于 2019-12-05 12:08:06
Ruby is preinstalled on my Mac and so I wanted to have a look at it. First thing I noticed, is that irb prompts >> instead of irb(main):001:0>. I can't find anything on how to change this with Google because everyone is using irb(main):001:0> in their code ;-) Can you help me out? PS: It's not that I think Ruby is broken, but I want to look more nerdy while programming ;-) troelskn $ irb --help Usage: irb.rb [options] [programfile] [arguments] --prompt prompt-mode --prompt-mode prompt-mode Switch prompt mode. Pre-defined prompt modes are `default', `simple', `xmp' and `inf-ruby' $ irb --prompt

In IRB, can I view the source of a method I defined earlier?

╄→гoц情女王★ 提交于 2019-12-05 11:09:44
问题 If I define a method in IRB, is there any way to review its source later in the session? > def my_method > puts "hi" > end Several screens of output later I'd like to be able to write something like > source my_method and get back: => def my_method; puts "hi"; end; Is this possible? 回答1: Try pry. There is a railscast about it (released this same week!) and it shows you how to show the code by using show-method . 回答2: Not in IRB but in Pry this feature is built-in. Behold: pry(main)> def hello

File.open and blocks in Ruby 1.8.7

瘦欲@ 提交于 2019-12-05 08:17:09
I'm pretty new to ruby and I'm currently reading the Pickaxe book to get familiar with everything. I came across the File.open section where it discusses taking a block as a parameter to a File.open call then guaranteeing that the file is closed. Now this sounds like an absolutely brilliant way to avoid shooting yourself in the foot and as I'm dangerously low on toes, I figure I'll give it a go. Here is what I wrote (in irb if that matters): File.open('somefile.txt', 'r').each { |line| puts line }`` My expectation was that the file somefile.txt would get opened, read, printed and closed, right

Accessing the irb in a modular Sinatra application

孤街醉人 提交于 2019-12-05 06:42:42
问题 I am building an application which subclasses Sinatra like so: require 'rubygems' require 'sinatra/base' require 'sinatra/assetpack' class App < Sinatra::Base ... run! end How can I access irb? Options are not parsed when executing sinatra this way, how do I programmatically open an irb shell? 回答1: I'm a little confused whether you want to open an IRB session from within your app (?) or use IRB to debug your Sinatra project? For debugging Rack-based apps (such as Sinatra), I like using the

How do I compile Readline support into Ruby

萝らか妹 提交于 2019-12-05 02:44:51
My version of ruby was compiled with editline (on os x) and I miss the features of readline in irb . How do I recompile ruby with readline support? Install readline to /usr/local Recompile ruby from scratch and use the --with-readline-dir=/usr/local switch or if you have downloaded the ruby sources earlier and built it by hand, Go to the ext/readline folder of your ruby source tree Type ruby extconf.rb and then run the make && make install procedure for ruby. rogerdpack There's also a pure ruby readline . 来源: https://stackoverflow.com/questions/1781518/how-do-i-compile-readline-support-into

Explanation of Ruby code for building Trie data structures

醉酒当歌 提交于 2019-12-05 00:03:01
问题 So I have this ruby code I grabbed from wikipedia and I modified a bit: @trie = Hash.new() def build(str) node = @trie str.each_char { |ch| cur = ch prev_node = node node = node[cur] if node == nil prev_node[cur] = Hash.new() node = prev_node[cur] end } end build('dogs') puts @trie.inspect I first ran this on console irb, and each time I output node , it just keeps giving me an empty hash each time {} , but when I actually invoke that function build with parameter 'dogs' string, it actually

Pasting text into IRB is incredibly slow. Readline issue?

一世执手 提交于 2019-12-04 23:53:40
When I paste the following text into IRB or PRY running under ruby-enterprise-2011.03, it takes 13 seconds. # Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Pasting isn't slow when running irb with other ruby installations on the same computer. jruby-1.5.6 jruby-1.6.3 ruby-1.8.6-p420 ruby-1.8.7-p352 ruby-1.9.1-p431 ruby-1.9.2-p290 ruby-1.9.3-preview1 or Mac OS X's default system install of 1.8.7-p249 This question is related to Rails console running incredibly slowly when editing text , but I'm not using rvm, and