我在Windows机器上运行Ruby和MySQL.
我有一些需要连接MySQL数据库的Ruby代码执行select.要连接到数据库,我需要提供密码等.
Ruby代码可以显示请求密码的提示,用户输入密码并按Enter键.我需要的是密码,因为它是键入的,显示为一行星号.
如何让Ruby在'dos box'中将键入的密码显示为一行星号?
我不确定这里的最佳实践是什么,但我经常看到缩写变量名称,尤其是当范围很小时.所以(使用简单的Ruby示例)而不是def add_location(name, coordinates),我看到类似的东西def add_loc(name, coord)- 我甚至可能会看到类似的东西def add_loc(n, x, y).我想,当他们习惯于看到缩写词时,较长的名字可能会让一个人厌倦.
冗长是否有助于提高可读性,还是只会伤害每个人的眼睛? - 人们更喜欢缩写和缩短名称吗?
我正在寻找一种从Ruby中的URL字符串中可靠地提取主机名的方法.
例如 http://www.mglenn.com/directory = www.mglenn.com或 http://www.mglenn.com?param=x = www.mglenn.com
这已经困扰了我很长一段时间.
>> nil.id
(irb):2: warning: Object#id will be deprecated; use Object#object_id
=> 4
Run Code Online (Sandbox Code Playgroud)
为什么nil.id会是4?(或者如果你想对弃用方面挑剔,则为nil.object_id)
我是Ruby的新手.我想要的只是生成一个简单的XML文件.
<?xml version="1.0" encoding="ASCII"?>
<product>
<name>Test</name>
</product>
Run Code Online (Sandbox Code Playgroud)
而已.
我正在尝试检查我的RESTful控制器中的新操作是否设置了所需Object类型的实例变量.看起来非常典型,但执行它时遇到了麻烦
客户控制器
def new
@client = Client.new
end
Run Code Online (Sandbox Code Playgroud)
测试
describe "GET 'new'" do
it "should be successful" do
get 'new'
response.should be_success
end
it "should create a new client" do
get 'new'
assigns(:client).should == Client.new
end
end
Run Code Online (Sandbox Code Playgroud)
结果是...
'ClientsController GET 'new' should create a new client' FAILED
expected: #,
got: # (using ==)
这可能是因为它试图比较两个不同的活动记录实例.那么,我如何检查控制器是否设置了一个包含Client模型新实例的实例变量.
我有两个我需要合并的数组,使用Union(|)运算符是PAINFULLY慢..有没有其他方法来完成数组合并?
此外,数组中充满了对象,而不是字符串.
数组中对象的示例
#<Article
id: 1,
xml_document_id: 1,
source: "<article><domain>events.waikato.ac</domain><excerpt...",
created_at: "2010-02-11 01:32:46",
updated_at: "2010-02-11 01:41:28"
>
Run Code Online (Sandbox Code Playgroud)
source是一小段XML.
编辑
抱歉! 通过'merge'我的意思是我不需要插入重复项.
A => [1, 2, 3, 4, 5]
B => [3, 4, 5, 6, 7]
A.magic_merge(B) #=> [1, 2, 3, 4, 5, 6, 7]
Run Code Online (Sandbox Code Playgroud)
理解整数实际上是Article对象,而Union运算符似乎永远占用
我更新了我用Homebrew安装的所有软件包.MySQL升级到5.6.12(从5.5.27左右):
$ mysql --version
mysql Ver 14.14 Distrib 5.6.12, for osx10.8 (x86_64) using EditLine wrapper
Run Code Online (Sandbox Code Playgroud)
现在mysql2 gem不再编译了:
$ gem install mysql2
Building native extensions. This could take a while...
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.
/Users/pupeno/.rvm/rubies/ruby-1.9.3-p429-perf/bin/ruby extconf.rb
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for mysql.h... no
checking for mysql/mysql.h... no
-----
mysql.h is missing. please check your installation of mysql and try again.
-----
*** extconf.rb failed ***
Could not create Makefile …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Windows 7上安装mysql2 gem我从mysql站点下载了连接器并将libmysql.dll放在ruby200\bin中
然后做gem安装mysql2
这些是我昏暗的结果吗?
Temporarily enhancing PATH to include DevKit...
Building native extensions. This could take a while...
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.
C:/Ruby200/bin/ruby.exe extconf.rb
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided …Run Code Online (Sandbox Code Playgroud) 我有一个现有用户,它有一个序列化字段,我希望能够将最近的消息添加到数组/序列化字段.
class User < ActiveRecord::Base
serialize :recent_messages
end
Run Code Online (Sandbox Code Playgroud)
在控制器中我试过了
@user = current_user
@user.recent_messages << params[:message]
@user.save
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
NoMethodError (undefined method `<<' for nil:NilClass):
Run Code Online (Sandbox Code Playgroud)
在我的架构中,我有:
create_table "users", :force => true do |t|
t.text "recent_messages"
end
Run Code Online (Sandbox Code Playgroud)
关于我哪里出错的任何想法?