I'm new to Ruby and I'm trying to change some of my code to have OO design using our current framework. When converting my utility file to classes I encountered the following problem:
There are two functions *create_output_file* and *write_to* are framework methods (don't belong to any classes) that I cannot touch (They specifies where to write to the framework).
And there is a class A I wrote which needs to use the *write_to* to log the time and return status of the foo().
<!-- language: lang-rb -->
def create_output_file(params)
#output_file = params["SS_output_file"]
#hand = File.open(#output_file, "a")
write_to "New Run - Shell Cmd\n\n"
return true
end
def write_to(message, newline = true)
return if message.nil?
sep = newline ? "\n" : ""
#hand.print(message + sep)
print(message + sep)
end
def this_is_dum
print("This is to show class A have access to global methods.\n")
end
class A
def foo()
# Do something that needs the status of instance A
this_is_dum()
write_to("This is the current status!")
end
end
# Main routine
params = {}
params["SS_output_file"] = "./debugging_log"
create_output_file(params) #sets the #hand file handle
A.new.foo
These are the outputs I'm getting:
New Run - Shell Cmd:
This is to show class A have access to global methods.
D:/data/weshi/workspace/RubyBRPM2/scripts/Random.rb:12:in `write_to':
private method `print' called for nil:NilClass (NoMethodError)
from D:/data/weshi/workspace/RubyBRPM2/scripts/Random.rb:18:in
`foo' from
D:/data/weshi/workspace/RubyBRPM2/scripts/Random.rb:26:in
`'
After digging a bit I found that #hand was not accessed during the foo() scope.
I'm not sure how I'll be able to access the #hand variable inside class A and what it means without assigning to a class. But I do need the write_to function to work in accordance to the whole framework. Any suggestions or instructions are welcomed and appreciated.
You can give #hand to your A instance in the constructor like that:
def create_output_file(params)
#output_file = params["SS_output_file"]
#hand = File.open(#output_file, "a")
write_to "New Run - Shell Cmd\n\n"
return true
end
def write_to(message, newline = true)
return if message.nil?
sep = newline ? "\n" : ""
#hand.print(message + sep)
print(message + sep)
end
def this_is_dum
print("This is to show class A have access to global methods.\n")
end
class A
def initialize(hand)
#hand = hand
end
def foo()
# Do something that needs the status of instance A
this_is_dum()
write_to("This is the current status!")
end
end
# Main routine
params = {}
params["SS_output_file"] = "./debugging_log"
create_output_file(params) #sets the #hand file handle
A.new(#hand).foo
The output is:
New Run - Shell Cmd
This is to show class A have access to global methods.
This is the current status!
Otherwise I found that which may be a little bit more complicated.
Related
I need to monkey patch some code and i was wondering if possible to edit a single line ( specific method variable assignment ) within super before calling it ?
class Test_class
def test
res = "AAAAAA"
puts res
end
end
module TestExtensions
def test
# do some Ruby magic here to change res = "BBBBB" ?
super
end
end
class Test_class
prepend TestExtensions
end
Test_class.new.test
---
Output is: AAAAAA
So in this silly example, can i edit the value of res and change it before calling super ? In the original class method i want to patch, I only need to change a single line, from maybe 20 lines of code in the method. Instead of copying the entire method and making a one line change i was hoping to just edit the specific line before calling super.
Update Aug 20th:
Below is the exact code i am trying to money patch.
https://github.com/chef/chef/blob/master/lib/chef/provider/remote_directory.rb#L206
Would like to change only line
res = Chef::Resource::CookbookFile.new(target_path, run_context)
to be instead
res = Chef::Resource::Template.new(target_path, run_context)
If i can not only replace this single line in the super method, maybe i can temporarily alias Chef::Resource::CookbookFile = Chef::Resource::Template so when calling super it returns the correct object, but i don't know if this is possible either.
Would like to change only line
res = Chef::Resource::CookbookFile.new(target_path, run_context)
to be instead
res = Chef::Resource::Template.new(target_path, run_context)
[...] I was just wondering if there was a more ninja Ruby way [...]
Here's a super dirty hack. Assuming that line is the only reference to Chef::Resource::CookbookFile, you could (I don't say you should) define a constant with the same name under the receiver referencing the replacement class:
class Chef
class Provider
class RemoteDirectory
class Chef
class Resource
CookbookFile = ::Chef::Resource::Template
end
end
end
end
end
Now Chef::Resource::CookbookFile within Chef::Provider::RemoteDirectory will resolve to the new constant which returns ::Chef::Resource::Template, so
res = Chef::Resource::CookbookFile.new(target_path, run_context)
effectively becomes:
res = ::Chef::Resource::Template.new(target_path, run_context)
This is the basic structure that it should respond to but I don't know how to code the class Interprete
interpreta=Interprete.new
interprete.add("a=0")
interprete.add("b=1")
interprete.add("a=b+10")
interprete.execute
interprete.value("a")#11
You can use binding. It's a way to 'store' a scope in a variable that can be reopened at will using eval. This is a good tutorial, and what I used as reference to piece together a solution Ruby’s Binding Class (binding objects)
:
class Interprete
def initialize
#commands = []
#binding = binding
end
def add(command)
#commands.push command
end
def execute
#commands.each { |command| #binding.eval command }
#commands = []
end
def value(variable_name)
#binding.eval variable_name
end
end
Usage:
i = Interprete.new
i.add "a = 1"
i.execute
i.value "a" # => 1
A note about this: binding returns a new object each time it's called; that's why it's cached in the #binding instance variable. Without doing this, each command will be executed in a different scope and the results won't be accessible.
There is something that i don't understand about ruby class instance variable or methods**.
So i have this code that keeps on giving me this error and i cant understand
Looks ruby thinks that i am trying to call for Float.in_celsius but I want to make this call within my class instance.
#-----------------------------------
def ftoc(fr)
fr = fr.to_f
if (fr == 32)
c = 0
elsif (fr == 212)
c = 100
else
c = (fr-32.0)*(5.0/9.0)
end
return c
end
def ctof (cl)
cl = cl.to_f
f = (cl*(9.0/5.0))+32.0
return f
end
#-----------------------------------
class Temperature
attr_accessor :in_celsius, :in_fahrenheit
#class metods
def self.from_celsius(cel)
puts "from celsious\n"
puts "cel: #{cel}\n"
#in_fahrenheit = cel
#in_celsius = ctof(cel)
puts "==============================\n"
return #in_celsius
end
def self.in_celsius
#in_celsius
end
end
puts "==============================\n"
puts Temperature.from_celsius(50).in_celsius
puts Temperature.from_celsius(50).in_fahrenheit
and Error is
test.rb:54: in '<main>' : undefined method 'in_celsius' for 122.0:float (noMethod Error)
enter code here
You have a fundamental misunderstanding of how classes work in Ruby. Right now all of your variables and methods are defined at class level. That means that everything you do in the methods is acting directly on the class itself. Instead, you should create instances of Temperature.
class Temperature
# special method called when creating a new instance
def initialize celsius
#in_celsius = celsius
#in_fahrenheit = celsius * 9 / 5.0 + 32
end
def self.from_celsius celsius
new celsius # built in method to create an instance, passes argument to initialize
end
# we defined initialize using celsius, so here we must convert
def self.from_fahrenheit fahrenheit
new((fahrenheit - 32) * 5 / 9.0)
end
private_class_method :new # people must use from_celsius or from_fahrenheit
# make instance variables readable outside the class
attr_accessor :in_celsius, :in_fahrenheit
end
Temperature.from_celsius(50).in_celsius
This code isn't perfect (from_fahrenheit does a redundant conversion) but it should give you the idea of how to redesign your class.
from_celsius is returning a float which doesn't have an in_celsius method. You need it to return an instance of Temperature which would have that method.
Got to say your intent is a tad confusing, unless you have some other uses for the class Temperature, so it's bit hard to say which way you should go.
Let's see the code puts Temperature.from_celsius(50).in_celsius in details:
Call to singleton method ::from_celsius of Temperature class. That is ok (with some stranges), and t returns as instance of Float class because of result of #ctof method.
Call to instance method #in_celsius of object, which is returned from the previous method. Since it was the Float, the ruby interpreter searches for its instance method #in_celsius, hasn't find it out, and throws the NoMethodError exception.
How to fix.
Since you treat ::from_celsius as a constructor of Temperature class, I believe, that you shell to pass the floating value into the new method, and return created object. You will have class code as follows:
def initialize( value )
#in_fahrenheit = cel
#in_celsius = ctof(cel)
end
def self.from_celsius(cel)
puts "from celsious\n"
puts "cel: #{cel}\n"
temp = Temperature.new( cel )
puts "==============================\n"
return temp
end
I understand that TOPLEVEL_BINDING is the Binding object for main. The following code confirms it:
def name
:outer
end
module Test
class Binder
def self.name
:inner
end
def self.test_it
eval 'name', TOPLEVEL_BINDING
end
end
end
p Test::Binder.test_it # => :outer
I got confused while looking at the source for rack. The problem was in understanding this code in the file lib/rack/builder.rb
def self.new_from_string(builder_script, file="(rackup)")
eval "Rack::Builder.new {\n" + builder_script + "\n}.to_app",
TOPLEVEL_BINDING, file, 0
end
def run(app)
end
The new_from_string method is passed the contents of a config.ru file which will be something like
run DemoApp::Application
Here it seems like TOPLEVEL_BINDING is referring to a Builder object, since the method run is defined for Builder but not for Object. However the earlier experiment establishes that TOPLEVEL_BINDING refers to main's binding. I do not understand how run method is working here. Please help me in understanding this code.
TOPLEVEL_BINDING is the top level binding.
That method is passing the string "run ..." into Builder.new { run ... }
Builder.new then does an instance_eval (https://github.com/rack/rack/blob/df1506b0825a096514fcb3821563bf9e8fd52743/lib/rack/builder.rb#L53-L55) on the block, thereby giving the code inside the block direct access to the instance's methods.
def initialize(default_app = nil,&block)
#use, #map, #run, #warmup = [], nil, default_app, nil
instance_eval(&block) if block_given?
end
run is an instance method of the Builder class, defined here -> https://github.com/rack/rack/blob/df1506b0825a096514fcb3821563bf9e8fd52743/lib/rack/builder.rb#L103-L105
def run(app)
#run = app
end
In short, "run DemoApp::Application" becomes:
Rack::Builder.new {
run DemoApp::Application
}.to_app
Edit: A simple example to illustrate the point:
class Builder
def initialize(&block)
instance_eval(&block)
end
def run(what)
puts "Running #{what}"
end
end
TOPLEVEL_BINDING.eval "Builder.new { run 10 }"
prints
Running 10
TOPLEVEL_BINDING gives you access to context in which the first file was executed:
a.rb:
a = 1
p TOPLEVEL_BINDING.local_variables #=> [:a]
require_relative 'b'
b.rb:
b = 1
p TOPLEVEL_BINDING.local_variables #=> [:a]
$ ruby a.rb
What rack developers are apparently trying to achieve is to isolate the builder line/script from all what is accessible from Rack::Builder.new_from_string (local variables, methods, you name it).
There's also a comment there these days:
# Evaluate the given +builder_script+ string in the context of
# a Rack::Builder block, returning a Rack application.
def self.new_from_string(builder_script, file = "(rackup)")
# We want to build a variant of TOPLEVEL_BINDING with self as a Rack::Builder instance.
# We cannot use instance_eval(String) as that would resolve constants differently.
binding, builder = TOPLEVEL_BINDING.eval('Rack::Builder.new.instance_eval { [binding, self] }')
eval builder_script, binding, file
return builder.to_app
end
I need to load a YAML file (I'm experimenting with SettingsLogic) and I'd like the instance to load the YAML with the same name as it. Briefly:
class MySettings < SettingsLogic
source "whatever_the_instance_is_called.yml"
# Do some other stuff here
end
basic_config = MySettings.new # loads & parses basic_config.yml
advanced_cfg = MySettings.new # loads & parses advanced_cfg.yml
...and so on...
The reason for this I don't yet know what configuration files I'll have to load, and typing:
my_config = MySettings.new("my_config.yml")
or
my_config = MySettings.new(:MyConfig)
just seems to be repeating myself.
I took a look around both Google and Stackoverflow, and the closest I came to an answer is either "Get Instance Name" or a discussion about how meaningless an instance name is! (I'm probably getting the query wrong, however.)
I have tried instance#class, and instance#name; I also tried instance#_id2ref(self).
What am I missing?!
Thanks in advance!
O.K., so with local variable assignment, there are snags, such as that assignment might occur slightly later than local variable symbol addition to the local variable list. But here is my module ConstMagicErsatz that I used to implement something similar to out-of-the box Ruby constant magic:
a = Class.new
a.name #=> nil - anonymous
ABC = a # constant magic at work
a.name #=> "ABC"
The advantage here is that you don't have to write ABC = Class.new( name: "ABC" ), name gets assigned 'magically'. This also works with Struct class:
Koko = Struct.new
Koko.name #=> "Koko"
but with no other classes. So here goes my ConstMagicErsatz that allows you to do
class MySettings < SettingsLogic
include ConstMagicErsatz
end
ABC = MySettings.new
ABC.name #=> "ABC"
As well as
a = MySettings.new name: "ABC"
a.name #=> "ABC"
Here it goes:
module ConstMagicErsatz
def self.included receiver
receiver.class_variable_set :##instances, Hash.new
receiver.class_variable_set :##nameless_instances, Array.new
receiver.extend ConstMagicClassMethods
end
# The receiver class will obtain #name pseudo getter method.
def name
self.class.const_magic
name_string = self.class.instances[ self ].to_s
name_string.nil? ? nil : name_string.demodulize
end
# The receiver class will obtain #name setter method
def name= ɴ
self.class.const_magic
self.class.instances[ self ] = ɴ.to_s
end
module ConstMagicClassMethods
# #new method will consume either:
# 1. any parameter named :name or :ɴ from among the named parameters,
# or,
# 2. the first parameter from among the ordered parameters,
# and invoke #new of the receiver class with the remaining arguments.
def new( *args, &block )
oo = args.extract_options!
# consume :name named argument if it was supplied
ɴς = if oo[:name] then oo.delete( :name ).to_s
elsif oo[:ɴ] then oo.delete( :ɴ ).to_s
else nil end
# but do not consume the first ordered argument
# and call #new method of the receiver class with the remaining args:
instance = super *args, oo, &block
# having obtained the instance, attach the name to it
instances.merge!( instance => ɴς )
return instance
end
# The method will search the namespace for constants to which the objects
# of the receiver class, that are so far nameless, are assigned, and name
# them by the first such constant found. The method returns the number of
# remaining nameless instances.
def const_magic
self.nameless_instances =
class_variable_get( :##instances ).select{ |key, val| val.null? }.keys
return 0 if nameless_instances.size == 0
catch :no_nameless_instances do search_namespace_and_subspaces Object end
return nameless_instances.size
end # def const_magic
# ##instances getter and setter for the target class
def instances; const_magic; class_variable_get :##instances end
def instances= val; class_variable_set :##instances, val end
# ##nameless_instances getter for the target class
def nameless_instances; class_variable_get :##nameless_instances end
def nameless_instances= val; class_variable_set :##nameless_instances, val end
private
# Checks all the constants in some module's namespace, recursivy
def search_namespace_and_subspaces( ɱodule, occupied = [] )
occupied << ɱodule.object_id # mark the module "occupied"
# Get all the constants of ɱodule namespace (in reverse - more effic.)
const_symbols = ɱodule.constants( false ).reverse
# check contents of these constant for wanted objects
const_symbols.each do |sym|
# puts "#{ɱodule}::#{sym}" # DEBUG
# get the constant contents
obj = ɱodule.const_get( sym ) rescue nil
# is it a wanted object?
if nameless_instances.map( &:object_id ).include? obj.object_id then
class_variable_get( :##instances )[ obj ] = ɱodule.name + "::#{sym}"
nameless_instances.delete obj
# and stop working in case there are no more unnamed instances
throw :no_nameless_instances if nameless_instances.empty?
end
end
# and recursively descend into the subspaces
const_symbols.each do |sym|
obj = ɱodule.const_get sym rescue nil # get the const value
search_namespace_and_subspaces( obj, occupied ) unless
occupied.include? obj.object_id if obj.kind_of? Module
end
end
end # module ConstMagicClassMethods
end # module ConstMagicErsatz
The above code implements automatic searching of whole Ruby namespace with the aim of finding which constant refers to the given instance, whenever #name method is called.
The only constraint using constants gives you, is that you have to capitalize it. Of course, what you want would be modifying the metaclass of the object after it is already born and assigned to a constant. Since, again, there is no hook, you have to finde the occasion to do this, such as when the new object is first used for its purpose. So, having
ABC = MySettings.new
and then, when the first use of your MySettings instance occurs, before doing anything else, to patch its metaclass:
class MySettings
def do_something_useful
# before doing it
instance_name = self.name
singleton_class.class_exec { source "#{instance_name}.yml" }
end
# do other useful things
end
Shouldn't you be able to do either
File.open(File.join(File.expand_path(File.dir_name(__FILE__)), foo.class), "r")
or
require foo.class
The first one need not be that complicated necessarily. But if I'm understanding you correctly, you can just use foo.class directly in a require or file load statement.
Adjust as necessary for YAML loading, but #class returns a plain old string.
Well if you have tons of variables to instantiate, I'd personally just create a Hash to hold them, it's cleaner this way. Now to instantiate all of this, you could do a loop other all your yaml files :
my_settings = {}
[:basic_config, :advanced_cfg, :some_yaml, :some_yaml2].each do |yaml_to_parse|
my_settings[yaml_to_parse] = MySettings.new(yaml_to_parse)
end
Make sure your initialize method in MySettings deals with the symbol you give it!
Then get your variables like this :
my_settings[:advanced_cfg]
Unfortunately, Ruby has no hooks for variable assignment, but this can be worked around. The strategy outline is as follows: First, you will need to get your MySettings.new method to eval code in the caller's binding. Then, you will find the list of local variable symbols in the caller's binding by calling local_variables method there. Afterwards, you will iterate over them to find which one refers to the instance returned by super call in your custom MySettings.new method. And you will pass its symbol to source method call.