Can I use parameter-less functions in Ruby 1.9.x? - ruby

So I'm working through the Ruby Koans, and I've encountered an issue that I think is specific to ruby 1.9.x.
def test_calling_global_methods_without_parentheses
result = my_global_method 2, 3
assert_equal 5, result
end
I get this:
james#tristan:~/code/ruby_projects/ruby_koans$ rake
(in /home/james/code/ruby_projects/ruby_koans)
cd koans
/home/james/.rvm/rubies/ruby-1.9.2-p180/bin/ruby path_to_enlightenment.rb
/home/james/code/ruby_projects/ruby_koans/koans/about_methods.rb:21:in `eval': (eval):1: syntax error, unexpected tINTEGER, expecting keyword_do or '{' or '(' (SyntaxError)
assert_equal 5, my_global_method 2, 3
^
from /home/james/code/ruby_projects/ruby_koans/koans/about_methods.rb:21:in `test_sometimes_missing_parentheses_are_ambiguous'
from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:377:in `meditate'
from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:449:in `block in walk'
from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:460:in `block (3 levels) in each_step'
from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:458:in `each'
from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:458:in `block (2 levels) in each_step'
from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:457:in `each'
from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:457:in `each_with_index'
from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:457:in `block in each_step'
from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:455:in `catch'
from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:455:in `each_step'
from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:448:in `walk'
from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:470:in `block in <top (required)>'
rake aborted!
Command failed with status (1): [/home/james/.rvm/rubies/ruby-1.9.2-p180/bi...]
/home/james/code/ruby_projects/ruby_koans/Rakefile:86:in `block in <top (required)>'
(See full trace by running task with --trace)
james#tristan:~/code/ruby_projects/ruby_koans$
I've looked at a few different repositories on GitHub that claim to have completed the Koans recently (In the past two months), and I've only seen the answer (First code snippet) that I used. So, is it something with my code, my Ruby install or something else?

The error you're getting is not from the code you listed; it's from the code underneath it. See line 20 of the related file. The notes say:
NOTE: We are Using eval below because the example code considered to be syntactically invalid

I don't know why, but the code is being evaluated like this:
def test_calling_global_methods_without_parentheses
assert_equal 5, my_global_method 2, 3
end
The problem is that this is ambiguos, can mean assert_equal(5, my_global_method(2, 3)) or assert_equal(5, my_global_method(2), 3). In this specific case, you have to use parentheses.

Don't forget to remove the space between the method call and the first parameter.
Do this
eval "assert_equal 5, my_global_method(2,3)"
and not
eval "assert_equal 5, my_global_method (2,3)" #beware of the space!

Related

Type restriction for Volt Framework model throws error

I would like to set type restrictions for a Volt Model like so
class EventItemBlock < Volt::Model
:name, String
:items, Volt::ArrayModel
end
When I save the file, Volt throws an error behind-the-scenes but does not crash. The relevant lines are the first seven (7), which state that the comma is a syntax error.
In the Volt Framework documentation, the comma is documented as proper syntax (http://docs.voltframework.com/en/docs/models.html). Removing the comma does not resolve errors--only removing the type restriction does.
[ERROR] #<SyntaxError: /home/jg/ModelTest/app/main/models/event_item_block.rb:2: syntax error, unexpected ',', expecting keyword_end
:name, String
^
/home/jg/ModelTest/app/main/models/event_item_block.rb:3: syntax error, unexpected ',', expecting keyword_end
:items, Volt::ArrayModel
^>
/home/jg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/volt-0.9.4/lib/volt/server/rack/component_paths.rb:67:in `require'
/home/jg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/volt-0.9.4/lib/volt/server/rack/component_paths.rb:67:in `block (2 levels) in require_in_components'
/home/jg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/volt-0.9.4/lib/volt/server/rack/component_paths.rb:65:in `each'
/home/jg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/volt-0.9.4/lib/volt/server/rack/component_paths.rb:65:in `block in require_in_components'
/home/jg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/volt-0.9.4/lib/volt/server/rack/component_paths.rb:31:in `block in app_folders'
/home/jg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/volt-0.9.4/lib/volt/server/rack/component_paths.rb:30:in `each'
/home/jg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/volt-0.9.4/lib/volt/server/rack/component_paths.rb:30:in `app_folders'
/home/jg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/volt-0.9.4/lib/volt/server/rack/component_paths.rb:61:in `require_in_components'
/home/jg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/volt-0.9.4/lib/volt/volt/server_setup/app.rb:16:in `setup_paths'
/home/jg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/volt-0.9.4/lib/volt/volt/app.rb:74:in `initialize'
/home/jg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/volt-0.9.4/lib/volt/boot.rb:21:in `new'
/home/jg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/volt-0.9.4/lib/volt/boot.rb:21:in `boot'
/home/jg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/volt-0.9.4/lib/volt/server.rb:44:in `boot_volt'
/home/jg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/volt-0.9.4/lib/volt/server/forking_server.rb:73:in `start_child'
/home/jg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/volt-0.9.4/lib/volt/server/forking_server.rb:229:in `block in reload'
/home/jg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/volt-0.9.4/lib/volt/utils/read_write_lock.rb:65:in `with_write_lock'
/home/jg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/volt-0.9.4/lib/volt/server/forking_server.rb:227:in `reload'
/home/jg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/volt-0.9.4/lib/volt/server/forking_server.rb:239:in `block (2 levels) in start_change_listener'
Ok, two things going on here. 1) your syntax if wrong. You need to do:
field :something, SomeClass
Second, you can't currently restrict to Volt::ArrayModel (yet, on the todo list)

How to define Ruby Test::Unit testcase with `must`

I had a test case of the following form:
require 'test/unit'
class SomeTests < Test::Unit::TestCase
def test_should_do_action
assert true
end
end
and have re-written it using must as suggested in the book A Test::Unit Trick to Know About:
require 'test/unit'
class SomeTests < Test::Unit::TestCase
must "do action" do
assert true
end
end
And when I run it, I get an undefined method 'must' error shown as follows:
SomeTests.rb:3:in `<class:SomeTests>': undefined method `must' for SomeTests:Class (NoMethodError) from
SomeTests.rb:2:in `<top (required)>' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:10:in `block (2 levels) in <main>' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:9:in `each' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:9:in `block in <main>' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:4:in `select' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:4:in `<main>' rake aborted! Command failed with status (1): [ruby -w -I"lib" -I"/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0" "/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb" "test/**/*Tests.rb" ]
Tasks: TOP => default => test (See full trace by running task with --trace)
I thought that must might be a part of minitest, so I required 'minitest/unit' instead, but I still get an error. I also assume that must keyword isn't part of rspec, which I'm not using yet.
How do I get this to work properly?
It looks like that method is not provided out of the box, but was developed by a third party. You need to add code described here.

RSpec "Failure/Error: Unable to find matching line from backtrace" On every test

I am working through some Ruby problem sets. I'm using Ubuntu 14.04, rbenv 0.4.0-98-g13a474c, rspec 3.1.4, and ruby 2.0.0p353. I'm running into the following errors for every test I run and I'm hoping someone might be able to guide me in the right direction to a solution.
I have run rspec tests successfully in the past but I can't seem to figure out the issue with my current setup.
Here is the message I receive for every test I run:
6) age is an integer
Failure/Error: Unable to find matching line from backtrace
ArgumentError:
wrong number of arguments (0 for 1)
# /usr/lib/ruby/vendor_ruby/rspec/mocks.rb:10:in `setup'
# /var/lib/gems/1.9.1/gems/rspec-core-3.1.4/lib/rspec/core/mocking_adapters/rspec.rb:19:in `setup_mocks_for_rspec'
# /var/lib/gems/1.9.1/gems/rspec-core-3.1.4/lib/rspec/core/example.rb:366:in `run_before_example'
# /var/lib/gems/1.9.1/gems/rspec-core-3.1.4/lib/rspec/core/example.rb:150:in `block in run'
# /var/lib/gems/1.9.1/gems/rspec-core-3.1.4/lib/rspec/core/example.rb:328:in `with_around_example_hooks'
# /var/lib/gems/1.9.1/gems/rspec-core-3.1.4/lib/rspec/core/example.rb:148:in `run'
# /var/lib/gems/1.9.1/gems/rspec-core-3.1.4/lib/rspec/core/example_group.rb:500:in `block in run_examples'
# /var/lib/gems/1.9.1/gems/rspec-core-3.1.4/lib/rspec/core/example_group.rb:496:in `map'
# /var/lib/gems/1.9.1/gems/rspec-core-3.1.4/lib/rspec/core/example_group.rb:496:in `run_examples'
# /var/lib/gems/1.9.1/gems/rspec-core-3.1.4/lib/rspec/core/example_group.rb:463:in `run'
# /var/lib/gems/1.9.1/gems/rspec-core-3.1.4/lib/rspec/core/runner.rb:111:in `block (2 levels) in run_specs'
# /var/lib/gems/1.9.1/gems/rspec-core-3.1.4/lib/rspec/core/runner.rb:111:in `map'
# /var/lib/gems/1.9.1/gems/rspec-core-3.1.4/lib/rspec/core/runner.rb:111:in `block in run_specs'
# /var/lib/gems/1.9.1/gems/rspec-core-3.1.4/lib/rspec/core/reporter.rb:53:in `report'
# /var/lib/gems/1.9.1/gems/rspec-core-3.1.4/lib/rspec/core/runner.rb:107:in `run_specs'
# /var/lib/gems/1.9.1/gems/rspec-core-3.1.4/lib/rspec/core/runner.rb:85:in `run'
# /var/lib/gems/1.9.1/gems/rspec-core-3.1.4/lib/rspec/core/runner.rb:69:in `run'
# /var/lib/gems/1.9.1/gems/rspec-core-3.1.4/lib/rspec/core/runner.rb:37:in `invoke'
# /var/lib/gems/1.9.1/gems/rspec-core-3.1.4/exe/rspec:4:in `<top (required)>'
# /usr/local/bin/rspec:23:in `load'
# /usr/local/bin/rspec:23:in `<main>'
#
# Showing full backtrace because every line was filtered out.
# See docs for RSpec::Configuration#backtrace_exclusion_patterns and
# RSpec::Configuration#backtrace_inclusion_patterns for more information.
Edit: Here is an example of one of the spec files, however, the issue comes up on about 20 spec files. I know my solutions are correct and my peers aren't having the same issues I am:
first_name = "M"
last_name = "R"
age = 23
describe 'first_name' do
it "is defined as a local variable" do
expect(defined?(first_name)).to eq 'local-variable'
end
it "is a String" do
expect(first_name).to be_a String
end
end
describe 'last_name' do
it "is defined as a local variable" do
expect(defined?(last_name)).to eq 'local-variable'
end
it "be a String" do
expect(last_name).to be_a String
end
end
describe 'age' do
it "is defined as a local variable" do
expect(defined?(age)).to eq 'local-variable'
end
it "is an integer" do
expect(age).to be_a Fixnum
end
end

rspec bugs when trying to run rake

I'm trying to teach myself some ruby using the app academy tutorials and after doing the readings, installing rvm,rubygems and rspec2 when I even try to run the first most basic code (00_hello) with rake I get the whole error :
(in /home/deadpool/Documents/learn_ruby)
/home/deadpool/Documents/learn_ruby/rspec_config.rb:3:in `block in <top (required)>': undefined method `color=' for #<RSpec::Core::Configuration:0x0000000293dee0> (NoMethodError)
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core.rb:67:in `configure'
from /home/deadpool/Documents/learn_ruby/rspec_config.rb:1:in `<top (required)>'
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/configuration.rb:162:in `require'
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/configuration.rb:162:in `block in requires='
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/configuration.rb:162:in `map'
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/configuration.rb:162:in `requires='
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/configuration_options.rb:22:in `block in configure'
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/configuration_options.rb:21:in `each'
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/configuration_options.rb:21:in `configure'
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/command_line.rb:17:in `run'
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/runner.rb:55:in `run_in_process'
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/runner.rb:46:in `run'
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/runner.rb:10:in `block in autorun'
rake aborted!
ruby -S bundle exec rspec -I/home/deadpool/Documents/learn_ruby/00_hello -I/home/deadpool/Documents/learn_ruby/00_hello/solution -f documentation -r ./rspec_config "/home/deadpool/Documents/learn_ruby/00_hello/hello_spec.rb" failed
/home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/rake_task.rb:117:in `rescue in block (2 levels) in initialize'
/home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/rake_task.rb:113:in `block (2 levels) in initialize'
/home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/rake_task.rb:109:in `block in initialize'
Tasks: TOP => default => spec
(See full trace by running task with --trace)
I tried to google some of the errors, but with no success. I had previously another error with the rake file using rspec v2 and the current version is 3.0.0, so I had to install the older and I think it might be another setup problem. Thanks if someone can help me or direct me.
rspec_config.rb file :
RSpec.configure do |c|
c.fail_fast = true
c.color = true
end
hello.rb file:
def hello
"Hello!"
end
def greet(who)
"Hello, #{who}!"
end
UPDATE
Getting new error as :-
While I changed c.color = true to c.color_enabled = true
(in /home/deadpool/Documents/learn_ruby)
the hello function says hello (FAILED - 1)
Failures: 1) the hello function says hello Failure/Error:
Unable to find matching line from backtrace undefined method run_all' for []:Array
# /home/deadpool/.rvm/gems/ruby-2.1.2/gems/rspec-core-2.0.0/lib/rspec/core/hooks.rb:116:inrun_hook_filtered'
# /home/deadpool/.rvm/gems/ruby-2.1.2/gems/rspec-core-2.0.0/lib/rspec/core/example_group.rb:176:in eval_before_alls'
# /home/deadpool/.rvm/gems/ruby-2.1.2/gems/rspec-core-2.0.0/lib/rspec/core/example_group.rb:231:inrun'
# /home/deadpool/.rvm/gems/ruby-2.1.2/gems/rspec-core-2.0.0/lib/rspec/core/command_line.rb:26:in block (2 levels) in run'
# /home/deadpool/.rvm/gems/ruby-2.1.2/gems/rspec-core-2.0.0/lib/rspec/core/command_line.rb:26:inmap'
# /home/deadpool/.rvm/gems/ruby-2.1.2/gems/rspec-core-2.0.0/lib/rspec/core/command_line.rb:26:in block in run'
# /home/deadpool/.rvm/gems/ruby-2.1.2/gems/rspec-core-2.0.0/lib/rspec/core/reporter.rb:11:inreport'
# /home/deadpool/.rvm/gems/ruby-2.1.2/gems/rspec-core-2.0.0/lib/rspec/core/command_line.rb:23:in run'
# /home/deadpool/.rvm/gems/ruby-2.1.2/gems/rspec-core-2.0.0/lib/rspec/core/runner.rb:55:inrun_in_process'
# /home/deadpool/.rvm/gems/ruby-2.1.2/gems/rspec-core-2.0.0/lib/rspec/core/runner.rb:46:in run'
# /home/deadpool/.rvm/gems/ruby-2.1.2/gems/rspec-core-2.0.0/lib/rspec/core/runner.rb:10:inblock in autorun'
UPDATE
hello_spec.rb file :
require "hello"
describe "the hello function" do
it "says hello" do
hello.should == "Hello!"
end
end
describe "the greet function" do
it "says hello to someone" do
greet("Alice").should == "Hello, Alice!"
end
it "says hello to someone else" do
greet("Bob").should == "Hello, Bob!"
end
end
UPDATE
So, I updated rspec to v3.0.0 and changed Rakefile gem 'rspec', '~>3.0.0' and c.color = true back. Everything is working now(getting some deprecation warnings, but nothing critical), getting the output that is in the tutorial. Guess I just had to updae my rspec and change the version in the Rakefile. Thank you so much :)
Your error log is showing you are in rspec-core-2.0.0 version and your error is saying -
rspec_config.rb:3:in `block in <top (required)>': undefined method `color=' for
#<RSpec::Core::Configuration:0x0000000293dee0> (NoMethodError)
Now Deprecate config options confirms that below versions of 2.99.0.rc1 / 2014-05-18 or 2.99.0 methods were - #color_enabled, #color_enabled= and #color?. Which are changed since 2.99.0 to #color, #color= and #color_enabled?.
I got the information from the changelog as I linked -
Deprecate #color_enabled, #color_enabled= and #color? in favour of #color, #color= and #color_enabled? output. (Jon Rowe)
Thus you need to write as
RSpec.configure do |c|
c.fail_fast = true
c.color_enabled = true
end
Regarding your new error, I found it as a bug undefined methodrun_all' for []:Array`. Which has been fixed in this patch. Check this Rspec issue.
My suggestion use Rspec 3.0, at least you will be happy. In this case revert the color_enabled to color.
Hope this would help you.

RubyKoans: broken koan?

Probably a sign of an amateur that I'm wondering if the problem's the koan (rather than me), however, consider this koan
def test_calling_global_methods_without_parentheses
result = my_global_method 2, 3
assert_equal __, result
end
Note, my_global method is
def my_global_method(a,b)
a + b
end
This is the hint it gives me in the terminal
The answers you seek...
<"FILL ME IN"> expected but was <5>.
So I did
def test_calling_global_methods_without_parentheses
result = my_global_method 2, 3
assert_equal 5, result
end
and I got this error
Users/mm/Sites/koans/about_methods.rb:21:in `eval': (eval):1: syntax error, unexpected tINTEGER, expecting keyword_do or '{' or '(' (SyntaxError)
assert_equal 5, my_global_method 2, 3
^
from /Users/mm/Sites/koans/about_methods.rb:21:in `test_sometimes_missing_parentheses_are_ambiguous'
from /Users/mm/Sites/koans/edgecase.rb:377:in `meditate'
from /Users/mm/Sites/koans/edgecase.rb:449:in `block in walk'
from /Users/mm/Sites/koans/edgecase.rb:460:in `block (3 levels) in each_step'
from /Users/mm/Sites/koans/edgecase.rb:458:in `each'
from /Users/mm/Sites/koans/edgecase.rb:458:in `block (2 levels) in each_step'
from /Users/mm/Sites/koans/edgecase.rb:457:in `each'
from /Users/mm/Sites/koans/edgecase.rb:457:in `each_with_index'
from /Users/mm/Sites/koans/edgecase.rb:457:in `block in each_step'
from /Users/mm/Sites/koans/edgecase.rb:455:in `catch'
from /Users/mm/Sites/koans/edgecase.rb:455:in `each_step'
from /Users/mm/Sites/koans/edgecase.rb:448:in `walk'
from /Users/mm/Sites/koans/edgecase.rb:470:in `block in <top (required)>'
Does anyone know the problem or can you tell me how to skip a koan?
Oh, I tested this koan. The error is on line 21 if you noticed that, not the "test_calling_global_methods_without_parentheses" method. It's the "test_sometimes_missing_parentheses_are_ambiguous" method goes wrong as it should be. You are expected to correct that method.
def test_calling_global_methods_without_parentheses
result = my_global_method 2, 3
assert_equal 5, result # You're fine with this koan.
end
# (NOTE: We are Using eval below because the example code is
# considered to be syntactically invalid).
def test_sometimes_missing_parentheses_are_ambiguous
eval "assert_equal 5, my_global_method 2, 3" # ENABLE CHECK
# **LOOK HERE~~~ HERE IS THE ERROR YOU SEE** Just correct it.
And if there is any koan you don't know how to deal with, just comment it.

Resources