Ruby scope of DATA after __END__
DATA is the same. You would get the same output if instead of doing Foo.new you would do DATA.read a second time.
This is because after the first read DATA
(which is an IO) has reached the end of the stream, so any further reads will return an empty string, unless you append additional data to DATA or rewind DATA
to the beginning of the stream.
Edit: To seek back to the point right after the __END__
you have to read DATA.pos
before performing any reading on DATA
and then restore pos
to that value after reading. The easiest solution is probably just doing FOO = DATA.read
at the beginning of the script and then using FOO
.
Scope with days period works from the last deploy date
This is from the Rails 3.1.0 documentation:
Note that scopes defined with scope will be evaluated when they are defined, rather than when they are used. For example, the following would be incorrect:
class Post < ActiveRecord::Base
scope :recent, where('published_at >= ?', Time.now - 1.week)
end
The example above would be ‘frozen’ to the Time.now value when the Post class was defined, and so the resultant SQL query would always be the same. The correct way to do this would be via a lambda, which will re-evaluate the scope each time it is called:
class Post < ActiveRecord::Base
scope :recent, lambda { where('published_at >= ?', Time.now - 1.week) }
end
So, you can try updating your scope to use a lambda as the second argument instead of just the where
:
scope :current_availables, -> { where(date: Date.current.beginning_of_day..Date.current+14.days) }
(Not sure if you can use ->
if not do lambda
)
You can also use 14.days.from_now
instead of adding the 14 days to Date.current
.
Scope to query last year's data
There are a few optimizations I'd make here.
Since you already have ActiveSupport loaded and you're using it, make use of the ago
method, and write something that's a little more flexible. You can also use a range and ActiveRecord can handle writing the appropriate query for your DB, no string substitution needed.
scope :from_year, ->(date) { where(transfer_date: date.beginning_of_year..date.end_of_year) }
# usage
Record.from_year(1.year.ago)
This is a lot less rigid. You can now easily query for records that are from 5 years ago without writing any new code. If you find yourself using last year in a lot of places, make it a convenience method:
def self.last_year
from_year 1.year.ago
end
When a variable goes out of scope does that mean it doesn't exist?
If you are using managed language then you don't allocate and unallocate memory so as far as you are concerned it no longer exists.
Technically it does but GCs tend not to be deterministic so technically it's hard to say when it actually vanishes.
Ruby method with block variable scope?
Yes it's possible, but you need to pass your variables into your block:
channel.on_data do |ch,data|
stdout_data+=data
yield(ch, data) if block_given?
end
And your block needs to accept data:
block = Proc.new do |ch, data|
puts "Yo I'm in the block"
puts data.inspect
if data.inspect.include? "Proceed?"
puts "here"
ch.send_data("Y\n")
end
end
The scope of a proc is where it was declared, not where it is invoked.
For example...
def my_method1
local = "inside"
block = Proc.new { puts local }
block.call
end
my_method1 # writes "inside"
def my_method2(&block)
local = "inside"
block.call
end
local = "outside"
block = Proc.new { puts local }
my_method2(&block) # writes "outside"
Howto: Model scope for todays records
Since "created_at" column contains date and time, but you need compare only date, you have two ways (I assume you use MySQL) :
- use BETWEEN:
scope :today, lambda { WHERE("created_at BETWEEN '#{DateTime.now.beginning_of_day}' AND '#{DateTime.now.end_of_day}'") }
- use DATE() function:
scope :today, lambda { WHERE('DATE(created_at) = ?', Date.today)}
also, you can add "created_on" column to the table with date only.
Updated:
def self.up
add_column table_name, :created_on, :date
add_column table_name, :updated_on, :date
end
scope :today, lambda { where(created_on: Date.today) }
Related Topics
Irb: How to Start an Interactive Ruby Session with Pre-Loaded Classes
How to Convert Activerecord Table Name to Model Class Name
What's the "Ruby Way" to Parse a String for a Single Key/Value
Alphabetize Arabic and Japanese Text That Is in Unicode
Drop-Down-Menu for Many-To-Many Relation in Rails Using Nested Attributes
Shorter Way to Pass Every Element of an Array to a Function
Using S3 Presigned-Url for Upload a File That Will Then Have Public-Read Access
How to Handle Utf-8 Email Headers (Like Subject:) Using Ruby
Detecting If This Is an Iframe Load or Direct
Ruby Mailer Is Coming Up with an Eoferror
Is It Necessary to Close Stringio in Ruby
Rails: Wkhtmltopdf Runtimeerror (Location of Wkhtmltopdf Unknown)
Pg_Config, Ruby Pg, Postgresql 9.0 Problem After Upgrade, Centos 5
Yielding in an Anonymous Block
How to Modify a Text File in Ruby
How to Evaluate a Date Difference in Years, Months and Days (Ruby)