I trying to do like
#array_object.factets
this #array_object belongs to Class A
class A
factets = ['c','d','e']
end
Class A is not inheriting any Active record
if not necessary calling variable then you can easily do:
class A
def factets
['c','d','e']
end
end
and then:
#array_object = A.new
#array_object.factets
OR for single line of code:
#array_object = A.new.factets
Related
I have a ToDo list program I'm writing for practice. My problem is that by separating concerns and making each list be a class while each task is also a class, I would like to be able to call the name of the list which the new task is being added to without having to pass the list name into the task class (either upon creation or later):
class HigherClass
def initialize
#higher_class_variable = unique_value
#instance_of_lower_class #keep variable empty for now
end
end
class LowerClass
def intitialize
#lower_class_variable = unique_value #this should be the same unique value as above
end
end
instance_of_higher_class = HigherClass.new
instance_of_higher_class.#instance_of_lower_class = LowerClass.new
instance_of_higher_class.#instance_of_lower_class.#lower_class_variable
#this should return the unique_value from the HigherClass instance
If you want to access attributes:
attr_reader :lower_class_variable
Then you can just do this:
#instance_of_lower_class.lower_class_variable
Same principle applies here for higher class:
class HigherClass
attr_writer :instance_of_lower_class
end
Then:
instance_of_higher_class.instance_of_lower_class = LowerClass.new
That all seems rather clunky considering you can do this:
class HigherClass
attr_accessor :unique
def initialize
#unique = unique_value
end
end
class LowerClass < HigherClass
def initialize
# Call superclass initialization
super
# Anything else you want here.
end
def something_using_unique
call_method(unique)
end
end
I have a class which declares a number of instance methods
class User < Group
def get_name
end
def show_profile
end
def get_task(task_id)
#some logic
end
end
And I want to call some of these methods from within a module. I can do it with Include statement.
include Users brings in all of the methods from User class. In this case I only want get_task and explicitly do not want get_name, show_profile etc..
I have a Tasks module which also has set of methods and one of those methods calls get_task method of User class.
module Tasks
class Project
def design
tid = 12
design_task = get_task(tid)
end
end
end
How this can be achieved without including other unnecessary methods of that class. Please help.
If the get_task method isn't depending on the user, you can use a class method like below. If it is depending on the user, you will have to get the server before you can run the method.
class User < Group
def get_name
end
def show_profile
end
def self.get_task(task_id)
#some logic
end
end
class Project
def design
tid = 12
design_task = User.get_task(tid)
end
end
I have some problem with extending class with instance method after separate module is included into separate class
module ActsAsCommentable
def self.included(commentable)
Thread.class_eval do
def commentable
p "disqusable is #{commentable}"
p "disqusable class is #{commentable}"
end
end
end
end
class Thread
#some code...
end
class Asset
include ActsAsCommentable
end
And now I want to call this method somelike this:
thread = Thread.new
thread.commentable
The problem is, of course is that there is no binding with include method for class eval, and I could save variables that I want to pass into class eval in ActsAsCommentable module, but I dont want to. Is there a better way?
I tried to do instead
module ActsAsCommentable
def self.included(commentable)
class << Thread
define_method :commentable do
p "disqusable is #{commentable}"
p "disqusable class is #{commentable}"
end
end
end
end
But As I guessed this creates instance method for singletone object of class and therefore I can call it only through
Thread.commentable
And again, no binding...
If I understand you correctly, you need to be able to access the commentable variable inside your Thread extension, right?
If so, just change this:
Thread.class_eval do
To this:
Thread.class_exec(commentable) do |commentable|
And it should work.
I'm using:
Ruby 1.9.2
Rails 3.1.10
This is my code:
class Report::ExpectedHour
def initialize(user, options = {})
#user = user
#date_start = options[:start]
#date_end = options[:end]
end
def expected_hours_range
previous = ExpectedHour.previous_dates(#user, #date_start).first
hours_range = ExpectedHour.between_dates(#user, #date_start, #date_end)
unless hours_range.include?(previous)
hours_range << previous
end
hours_range
end
end
Every time I call expected_hours_range from my instance I get this error:
NameError: uninitialized constant Report::ExpectedHour::ExpectedHour
from /home/edelpero/.rvm/gems/ruby-1.9.2-p180#titi/gems/aws-s3-0.6.2/lib/aws/s3/extensions.rb:206:in `const_missing_from_s3_library'
from /opt/lampp/htdocs/titi/app/models/report/expected_hour.rb:10:in `expected_hours_range'
I'm not sure why Report::ExpectedHour::ExpectedHour is called because I'm calling ExpectedHour which is an actual existing ActiveRecord class. Also Report::ExpectedHour::ExpectedHour doesn't exist.
When calling classes inside your class methods, ruby expects it to be either a class nested inside you class itself or a constant. Try this:
class MyClass
def some_method
use_external_class = ::ExternalClass::CONSTANTB.bla
# Use the '::'
end
end
I have the following method:
class Store < ActiveRecord::Base
def my_func(str)
puts str
end
end
I can't seem to call it from outside the class like this:
Store::my_func("hi")
Any idea why?
What you have defined is an instance method. Basically this means you can only call it on instances of that class.
store = Store.new
store.my_func("hi")
If you want a class method, you need to define it a little differently. Either:
class Store < ActiveRecord::Base
def self.my_func(str)
puts str
end
end
Or (more useful if you're defining a lot of class methods):
class Store < ActiveRecord::Base
class << self
def my_func(str)
puts str
end
end
end
The above two work because classes are also instances of the class Class, so the implicit receiver self in the above two examples is that instance (the class itself).
You call a class method like this:
Store.my_func("hi")