I've used Virtual attributes in the past but I can't seem to get past this, and I know the answer is probably staring me in the face.
I have a model like so:
model Confirmation.rb
class Confirmation < ActiveRecord::Base
#attr_accessible :confirmation, :confirmation_token
#attr_accessible :confirmation_token
def confirmation_to...
I have a select Menu with a few options
<p>
<%= f.label :reason %><br />
<%= f.select :reason, Confirmation.reasons.collect {|p| [ p[:name], p[:id] ] }, { :include_blank => true } %>
</p>
The last being id "5" and name = "Other"
If and only if they choose other I want a hidden text_area with a div id = "other" to be shown...
I haven't used Rails, but I'm somewhat familiar with it. I'm more familiar with CakePHP. I get the idea that CakePHP is modeled after Rails, and they seem to have a lot in common.
But, basically, I'd like to know if patterning itself after Rails is, or has ever been, one of the goals of CakePHP? I understand it's not a port, but is R...
What is the approach to get SQLite-ruby working in IronRuby?
...
I can't work out why the following generates a "String can't be coerced into Fixnum".
Any ideas?
def branch_image
image_tag("24white/branch.png")
end
def spacing_images
i = 2
i.times do
image_tag("24white/blank.png")
end
end
def insert_branch(prefix, type)
content_tag(:li, spacing_images + branch_i...
I usually do something like
array.sort{|a,b| a.something <=> b.something}
How should I DRY this up?
...
I have a legacy database where there are objects called "notes". Each pad has an entry in the notes table with information about the note, such as the UUID, owner and date created. Each version of the note's actual content is stored in its own table in a separate database, where the table's name is the note's UUID. Each record in a parti...
If I create a Here document:
myheredoc = <<HTMLOUTPUT
<div>This is the div</div>
HTMLOUTPUT
Can I use 'myheredoc' to manipulate this Here document like a regular string?
...
C:\>irb
irb(main):001:0> s = Proc.new { puts "Hello" }
=> #<Proc:0x04051780@(irb):1>
irb(main):002:0> s.call
Hello
=> nil
What causes the nil?
ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]
...
According to this post, requiring rubygems is an antipattern.
require 'rubygems'
The argument seems to boil down to this:
When I use your library, deploy your
app, or run your tests I may not want
to use rubygems. When you require
'rubygems' in your code, you remove my
ability to make that decision. I
cannot unrequire ru...
Shoes wraps it's own Ruby install, right?
I can't use Fiber which is a Ruby1.9 feature. And, I want to use a Fiber for creating a generator.
Here's my code (so you can make sure the problem isn't with my code):
class BrownianGenerator
def initialize
@x = 0
@fiber = Fiber.new do
loop do
@x = @x+rand;
F...
In this example from a blog post,
class Array
def each
i = 0
while(i < self.length) do
yield(self[i])
i += 1
end
end
end
my_array = ["a", "b", "c"]
my_array.each {|letter| puts letter }
# => "a"
# => "b"
# => "c"
Is it necessary to use self in the statement:
yield(self[i])
Or would it be ok to simply sa...
s = Proc.new {|x|x*2}
puts s.call(5)
-> 10
def foo(&a)
a.call(5)
end
puts "test foo:"
foo(s)
When I try to call the proc above, I get:
foo: wrong number of arguments (1 for 0) (ArgumentError)
My expectation was that I can pass a proc to a method if the method is defined with this type of signature:
def foo(&a)
and then I can e...
Hi people, i will be short.
As far as i know watir library provides two methods for getting html elements.
Almost for each element (div, button, table, li, etc) watir provides two methods:
. One is the 'singular' method which gets only one specific element. For example:
watir_instance.div(:id,'my_div_id')
watir_instance.link(:href,'m...
I wonder why ruby give and, or less precedence than &&, || , and assign operator? Is there any reason?
...
s = Proc.new {|x|x*2}
puts "proc:" + (s.call(5)).to_s
def foo(&a)
a.call(5)
end
foo{|x| puts "foo:" + (x*3).to_s}
Running this program produces the output:
proc:10
foo:15
How does the value 3 from the foo block get passed to the proc? I expected this output:
proc:10
foo:10
The proc is always called with the value 5 as the ar...
Update: for the record, here's the implementation I ended up using.
Here's a trimmed down version of a parser I'm working on. There's still some code, but it should be quite easy to grasp the basic concepts of this parser.
class Markup
def initialize(markup)
@markup = markup
end
def to_html
@html ||= @markup.split(/(\r\n...
s = Proc.new {|x|x*2}
def one_arg(x)
puts yield(x)
end
one_arg(5, &s)
How does one_arg know about &s?
...
def foo
f = Proc.new { return "return from foo from inside proc" }
f.call # control leaves foo here
return "return from foo"
end
def bar
b = Proc.new { "return from bar from inside proc" }
b.call # control leaves bar here
return "return from bar"
end
puts foo # prints "return from foo from inside proc"
puts bar # prints ...
I am using the ruby gem "Chronic" to parse four digit strings as DateTime objects. I am using time in military format (ie: "0800") which seems from the documentaion to be a valid format.
In most cases, Chronic parses time in this format correctly - however it always parses a four digit string beginning with "12" as 00:XX AM of the ne...