Unable to Add a Chart to Excel Workbook - ruby

I am using the writeexcel gem.
The following is my code snippet:
workbook = WriteExcel.new("graphTest.xls")
worksheet = workbook.add_worksheet(sheetname = "Test")
chart = workbook.add_chart(
:type => "Chart::column",
:name => "Chart",
:embedded => 1
)
and I get the following error:
undefined method `new' for nil:NilClass (NoMethodError)
Full error:
/Users/guy/.rvm/gems/ruby-2.3.0/gems/writeexcel-1.0.5/lib/writeexcel/chart.rb
:79:in 'factory': undefined method 'new' for nil:NilClass (NoMethodError)
from /Users/guy/.rvm/gems/ruby-2.3.0/gems/writeexcel-1.0.5/lib/writee
xcel/workbook.rb:334:in 'add_chart'
from excelGraph.rb:18:in '<main>'
Why am I getting this error message if I am taking an example straight from the gem documentation found here?

The, "Gotcha," found within your code snippet is the capitalization of column in Chart::column:
chart = workbook.add_chart(
:type => "Chart::Column", #capitalized
:name => "Chart",
:embedded => 1
)
instead of
chart = workbook.add_chart(
:type => "Chart::column", #all lowercase
:name => "Chart",
:embedded => 1
)

Related

Define a local variable or method in a script

I'm new to ruby and I'm trying to re-structure my script which adds some servers to zabbix monitor etc.The issue that I'm facing is below:
zbx = ZabbixApi.connect(
:url => 'http://zabbixserver.net/zabbix/api_jsonrpc.php',
:user => 'admin',
:password => 'admin'
)
def createtemplate
zbx.templates.create(
:host => "RealDoc MS Template",
:groups => [:groupid => zbx.hostgroups.get_id(:name => "RealDoc")]
) ..../will create Items, graphs etc...
end
if templateid.empty?
createtemplate
else
puts "Template Exists"
end
When is accessing the createtemplate method it's throwing the following error: undefined local variable or method `zbx' for main:Object (NameError)
well zbx isn't in scope, as it isn't a global. you have a couple options.
either pass it into the method
def createtemplate(zbx)
zbx.templates.create(
:host => "RealDoc MS Template",
:groups => [:groupid => zbx.hostgroups.get_id(:name => "RealDoc")]
) ..../will create Items, graphs etc...
end
if templateid.empty?
createtemplate zbx
else
puts "Template Exists"
en
or you can make it global with a $.
$zbx = ZabbixApi.connect(
:url => 'http://zabbixserver.net/zabbix/api_jsonrpc.php',
:user => 'admin',
:password => 'admin'
)
def createtemplate
$zbx.templates.create(
:host => "RealDoc MS Template",
:groups => [:groupid => zbx.hostgroups.get_id(:name => "RealDoc")]
) ..../will create Items, graphs etc...
end
if templateid.empty?
createtemplate
else
puts "Template Exists"
end
I would do the first option, as global variables should be used sparingly, but in such a short script it probably doesn't matter that much..
It's working with adding the variable to our method def createtemplate(zbx)
, and the same thing when you are calling the methood , you will call it with zbx variable.

why can't I use create_or_update?

It's unclear to me why this isn't working:
class FastGrowers < ActiveRecord::Base
end
FastGrowers.create_or_update(:id => t.id, :ticker => ticker, :five_year_growth_rate => growth_rate)
I get this as a result:
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/dynamic_matchers.rb:55:in `method_missing': undefined method `create_or_update' for #<Class:0x8aaa264> (NoMethodError)
what gives?
It's not working because that method is not provided by ActiveRecord.
Perhaps you want FastGrowers.find_or_create_by_id?
grower = FastGrowers.find_or_create_by_id(t.id)
grower.update_attributes(:ticker => ticker, :five_year_growth_rate => growth_rate)
But if you have an id, you should know if you have a record, right? Hard to tell what you are trying to do, but something there is fishy.

How to get rid of sequel error in sequel?

here's my code:
init file:
require 'sequel'
DB = Sequel.connect('sqlite://data.db')
DB.drop_table?(:restaurants)
DB.create_table :restaurants do
primary_key :id
String :name
end
DB.drop_table?(:category)
DB.create_table :category do
primary_key :id
String :name
end
DB.drop_table?(:items)
DB.create_table :items do
primary_key :id
foreign_key :restaurant_id
foreign_key :category_name
String :name
Float :price
end
require_relative './restaurant'
require_relative './categories'
require_relative './item'
app file:
require_relative './models/init
p = Category.create(:name => 'Pizza')
c = Category.create(:name => 'Calazone')
pa = Category.create(:name => 'Pasta')
s = Category.create(:name => 'Salad')
d = Category.create(:name => 'Dessert')
dr = Category.create(:name => 'Drink')
si = Category.create(:name => 'Side')`
But I am getting this error:
/home/ben/.rvm/gems/ruby-1.9.3-p385/gems/sequel-3.45.0/lib/sequel/model/base.rb:1780:in `block in set_restricted': method name= doesn't exist (Sequel::Error)
from /home/ben/.rvm/gems/ruby-1.9.3-p385/gems/sequel-3.45.0/lib/sequel/model/base.rb:1767:in `each'
from /home/ben/.rvm/gems/ruby-1.9.3-p385/gems/sequel-3.45.0/lib/sequel/model/base.rb:1767:in `set_restricted'
from /home/ben/.rvm/gems/ruby-1.9.3-p385/gems/sequel-3.45.0/lib/sequel/model/base.rb:1278:in `set'
from /home/ben/.rvm/gems/ruby-1.9.3-p385/gems/sequel-3.45.0/lib/sequel/model/base.rb:1736:in `initialize_set'
from /home/ben/.rvm/gems/ruby-1.9.3-p385/gems/sequel-3.45.0/lib/sequel/model/base.rb:920:in `initialize'
from /home/ben/.rvm/gems/ruby-1.9.3-p385/gems/sequel-3.45.0/lib/sequel/model/base.rb:156:in `new'
from /home/ben/.rvm/gems/ruby-1.9.3-p385/gems/sequel-3.45.0/lib/sequel/model/base.rb:156:in `create'
from app.rb:4:in `<main>'"
Help Please.
Thanks
You don't post your Category model, but I'm guessing it's not actually looking at the category table (probably the categories table). You either want to rename your database table to categories or tell your Category model to use the category table. If that's not it, you probably want to post your model code.

Ruby Net:LDAP- NoMethodError for attributes that don't exist

I'm doing a simple Net:LDAP search and when I'm outputting an entry's attribute that may not exist for every entry, I get an error "NoMethodError: undefined method 'some_attribute'"
Here is the code:
require 'rubygems'
require 'net/ldap'
ldap = Net::LDAP.new
ldap.host = 'ldap.example.com'
ldap.port = 389
if ldap.bind
filter = Net::LDAP::Filter.eq( "sn", "Smith" )
treebase = "ou=people,o=company"
ldap.search( :base => treebase, :filter => filter, :return_result => false) do |entry|
puts #{entry.some_attribute}
end
end
else
puts "bind unsuccessful"
end
I tried also doing:
if entry.respond_to?(some_attribute)
puts "#{entry.some_attribute}"
end
That didn't work, it returns as false for every entry (when some entries have the attribute).
Ruby is expecting a symbol in the respond_to? method call.
ruby-1.8.7-p299 > class Foo
ruby-1.8.7-p299 ?> attr_accessor :some_attr
ruby-1.8.7-p299 ?> end
=> nil
ruby-1.8.7-p299 > Foo.new.respond_to?(some_attr)
NameError: undefined local variable or method `some_attr' for #<Object:0xb77ce950>
from (irb):4
ruby-1.8.7-p299 > Foo.new.respond_to?(:some_attr)
=> true

Problem using OpenStruct with ERB

EDIT: forgot to include my environment info... Win7x64, RubyInstaller Ruby v1.9.1-p378
EDIT 2: just updated to v1.9.1, patch 429, and still getting this same error.
Edit 3: running this same code in Ruby v1.8.7, patch 249, works fine. so it's v1.9.1 that broke it, apparently.
I'm new to using ERB and the samples i could find are... ummm... less than helpful... having played around with ERB for about an hour, I got some basic examples working (finally), but I have no idea why this doesn't work...
require 'ostruct'
require 'erb'
data = {:bar => "bar"}
vars = OpenStruct.new(data)
template = "foo "
erb = ERB.new(template)
vars_binding = vars.send(:binding)
puts erb.result(vars_binding)
this code produces the following error:
irb(main):007:0> puts erb.result(vars_binding)
NameError: undefined local variable or method `bar' for main:Object
from (erb):1
from C:/Ruby/v1.9.1/lib/ruby/1.9.1/erb.rb:753:in `eval'
from C:/Ruby/v1.9.1/lib/ruby/1.9.1/erb.rb:753:in `result'
from (irb):7
from C:/Ruby/v1.9.1/bin/irb:12:in `'
why is it looking at the main:Object binding? I told it to use the binding from the OpenStruct by passing in vars_binding
can someone fill me in on why it doesn't work, and help me get it to work?
The problem is where the binding is being executed. The 1.8.7-way obj.send(:binding) does not work anymore (see issue2161), the environment must be the object itself. So use instance_eval:
require 'ostruct'
require 'erb'
namespace = OpenStruct.new(:first => 'Salvador', :last => 'Espriu')
template = 'Name: <%= first %> <%= last %>'
ERB.new(template).result(namespace.instance_eval { binding })
#=> Name: Salvador Espriu
More about this issue in this answer.
Fix to Problem:
I stumbled upon this question when encountering the same type of error with similar code in Ruby 1.9.2.
I'm new to Ruby so I can't explain what is happening. I continued to search online and found this blog post that has an approach that seems to work. After modifying your example to incorporate this approach I end up with the following, working, code:
require 'ostruct'
require 'erb'
class ErbBinding < OpenStruct
def get_binding
return binding()
end
end
data = {:bar => "baz"}
vars = ErbBinding.new(data)
template = "foo <%= bar %>"
erb = ERB.new(template)
vars_binding = vars.send(:get_binding)
puts erb.result(vars_binding)
Additional Information:
When the code is run thru the IRB, I get:
require 'ostruct'
=> true
require 'erb'
=> true
class ErbBinding < OpenStruct
def get_binding
return binding()
end
end
=> nil
data = {:bar => "baz"}
=> {:bar=>"baz"}
vars = ErbBinding.new(data)
=> #<ErbBinding bar="baz">
template = "foo <%= bar %>"
=> "foo <%= bar %>"
erb = ERB.new(template)
=> #<ERB:0x2b73370 #safe_level=nil, #src="#coding:IBM437\n_erbout = ''; _erbout.concat \"foo \"; _erbout.concat(( bar ).to_s); _erbout.force_encoding(__ENCODING__)", #enc=#<Encoding:IBM437>, #filename=nil>
vars_binding = vars.send(:get_binding)
=> #<Binding:0x2b6d418>
puts erb.result(vars_binding)
foo baz
=> nil
What's your environment look like? This code worked for me (I just changed the string "bar" to "baz" to disambiguate in my brain, and added it to the template):
require 'ostruct'
require 'erb'
data = {:bar => "baz"}
vars = OpenStruct.new(data)
template = "foo <%= bar %>"
erb = ERB.new(template)
vars_binding = vars.send(:binding)
puts erb.result(vars_binding)
When I run it, I get:
defeateds-MacBook-Pro:Desktop defeated$ ruby erb.rb
foo baz
Under 1.8.7 on OSX:
defeateds-MacBook-Pro:Desktop defeated$ ruby -v
ruby 1.8.7 (2009-06-08 patchlevel 173) [universal-darwin10.0]
Looks like this does not work with higher ruby versions
with ruby 2.1.1
[19] pry(main)> name = "samtoddler"
=> "Suresh"
[20] pry(main)> template_string = "My name is <%= name %>"
=> "My name is <%= name %>"
[21] pry(main)> template = ERB.new template_string
=> #<ERB:0x007fadf3491c38
#enc=#<Encoding:UTF-8>,
#filename=nil,
#safe_level=nil,
#src="#coding:UTF-8\n_erbout = ''; _erbout.concat \"My name is \"; _erbout.concat(( name ).to_s); _erbout.force_encoding(__ENCODING__)">
[22] pry(main)> puts template.result
NameError: undefined local variable or method `name' for main:Object
from (erb):1:in `<main>'
with ruby 1.9.3
[2] pry(main)> name = "samtoddler"
=> "Suresh"
[3] pry(main)> template_string = "My name is <%= name %>"
=> "My name is <%= name %>"
[4] pry(main)> template = ERB.new template_string
=> #<ERB:0x007f9be2a1fdf8
#enc=#<Encoding:UTF-8>,
#filename=nil,
#safe_level=nil,
#src=
"#coding:UTF-8\n_erbout = ''; _erbout.concat \"My name is \"; _erbout.concat(( name ).to_s); _erbout.force_encoding(__ENCODING__)">
[5] pry(main)> puts template.result
My name is samtoddler
So it gives error but still works in 1.9.3 and all the versions below 1.9.3.

Resources