I am working with ruby 1.8.6 (2007-03-13 patchlevel 0) [x86_64-linux] and I get
undefined method `bytes' for #<String:0x2a95ec2268> (NoMethodError)
even though my code works on ruby 1.8.7. patchlevel 249
I saw somewhere that you need to add require "jcode" for a similar method not defined error with each_byte. I tried adding that but it still does not work. Any suggestions are very appreciated.
In Ruby 1.8.6, you can use my backports gem:
require 'backports/1.8.7/string/bytes'
Ta-da, you now have access to String#bytes.
You also have all the many other changes introduced in 1.8.7. And most of 1.9.1, and all of the upcoming 1.9.2, etc...
Ruby 1.8.6 doesn't have String#bytes. That's a 1.9 addition that was backported to 1.8.7.
You can roughly implement it yourself like this:
class String
require 'enumerator'
def bytes(&block)
return to_enum(:each_byte) unless block_given?
each_byte &block
end
end unless ''.respond_to?(:bytes)
[Note: I haven't checked whether this actually fulfills the contract of String#bytes 100%, but it is close enough for my use.]
Related
I have been using the SimpleDelegator class for various things. But I noticed, Ruby 2.7 (ArchLinux x86_64) doesn't come with the SimpleDelegator class (no Delegator either).
My program:
#!/usr/bin/ruby -w
class OutputDecorator < SimpleDelegator
def puts(*args)
STDOUT.write "Hello #{args.join}... It's Ruby #{RUBY_VERSION} #{RUBY_PLATFORM}\n"
end
end
$stdout = OutputDecorator.new($stdout)
$stdout.puts('Sourav')
$stdout = $stdout.__getobj__
$stdout.puts('Sourav')
Running with:
Ruby 2.4.6:
> ~/.rvm/rubies/ruby-2.4.6/bin/ruby p.rb
Hello Sourav... It's Ruby 2.4.6 x86_64-linux
Sourav
Ruby 2.5.5:
> ~/.rvm/rubies/ruby-2.5.5/bin/ruby p.rb
Hello Sourav... It's Ruby 2.5.5 x86_64-linux
Sourav
Ruby 2.6.3:
> ~/.rvm/rubies/ruby-2.6.3/bin/ruby p.rb
Hello Sourav... It's Ruby 2.6.3 x86_64-linux
Sourav
Ruby 2.7.0:
> ruby p.rb
Traceback (most recent call last):
p.rb:2:in `<main>': uninitialized constant SimpleDelegator (NameError)
Is there any new alternatives to SimpleDelegator in Ruby 2.7?
The Delegator and SimpleDelegator classes aren't core classes like Array or Mutex. They're part of the delegate standard library which needs to be loaded first: require 'delegate'.
It happened to work in older Ruby versions as they came with an older RubyGems version by default. RubyGems is automatically loaded since Ruby 1.9 and until 3.1.0 that meant delegate was loaded indirectly. Updating RubyGems or running ruby with --disable=gems should cause the exact same issue with Ruby <= 2.6 too. irb also loads several standard libraries: delegate but also timeout and many more.
Programming languages with a similar mechanism like C++ also have this issue: instead of load/require there's #include, including a standard library header might include another one, then a newer version might not include the other header anymore and user code relying on the old behavior fails to compile.
I want to use gcd function of the Integer class. Using the example from Ruby Doc as a test it fails:
irb(main):001:0> 72.gcd 168
NoMethodError: undefined method `gcd' for 72:Fixnum
from (irb):1
I have the windows one click installer ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]. On other PCs with the same version of ruby this works correctly.
Any ideas?
Try
require 'rubygems'
72.gcd 168
require 'rational'
I am trying to use the Enumerable#each_slice. It doesn't work on my computer, stating that method is not found.
I am running ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]
API: http://ruby-doc.org/core/classes/Enumerable.html#M003142
Example:
(1..10).each_slice(3) {|a| p a} # I get NoMethodError: undefined method `each_slice' for 1..10:Range
What am I doing wrong?
In ruby 1.8.6 you have to require 'enumerator' (which is part of stdlib and has been merged into core in 1.8.7+) before using each_slice.
Sadly the ruby-doc lists methods that are added to core classes by stdlib without mentioning where the methods are from.
just compared 1.8.6 to 1.9 and it looks like
(1..10).respond_to? :each_slice
is true in 1.9 and false in 1.8.6. So, the doc you are using is not for 1.8.6. if you can upgrade to a newer version of Ruby easily it should give you that method on the Range.
The following code snippet works fine in 1.8.7 on Mac OS X, but not in 1.8.6 on Ubuntu. Why? Is there a workaround?
Works in 1.8.7:
$ ruby --version
ruby 1.8.7 (2009-06-08 patchlevel 173) [universal-darwin10.0]
ltredgate15:eegl leem$ irb
>> 6.times.map {'foo'}
=> ["foo", "foo", "foo", "foo", "foo", "foo"]
>>
But not in 1.8.6:
# ruby --version
ruby 1.8.6 (2008-08-11 patchlevel 287) [i686-linux]
Ruby Enterprise Edition 20090610
# irb
irb(main):001:0> 6.times.map {'foo'}
LocalJumpError: no block given
from (irb):1:in `times'
from (irb):1
irb(main):002:0>
Why is there a difference? What's the workaround for 1.8.6?
In 1.8.7+ iterator methods like times return an enumerator if they are called without a block. In 1.8.6 you have to do
require 'enumerator'
6.enum_for(:times).map {...}
Or for this specific use case you could simply do (0...6).map {...}
In Ruby 1.9, the library was changed so functions that did iteration would return an Enumerator object if they were called without a block. A whole host of other language features were also changed, and it was widely known that compatibility would be broken between Ruby 1.8.x and Ruby 1.9 in the interests of improving the language as a whole. Most people didn't find this too distressing.
The Ruby development team decided that Ruby 1.8.7 should be a transition release adding some of the library features that Ruby 1.9 introduced. They took a lot of criticism for the decision, and many enterprise Ruby users remained (and many still remain) running Rails on Ruby 1.8.6, because they feel the changes introduced 1.8.7 are just too large, and too risky. But nevertheless, 1.8.7 remains, and having iteration functions return Enumerators is one of the features that was incorporated.
It is this migration feature that you're seeing in 1.8.7, which is not present in 1.8.6.
sepp2k's answer gives a good workaround. There's not much for me to add on that count.
Because 1.8.6 #times yields on the given block, while 1.8.7 returns an Enumerator object you can keep around and implements Enumerable.
Ruby 1.8.7 introduces many changes. If you want to use them in Ruby 1.8.6, simply
require 'backports'
That's it. This gives you many methods of 1.9.1 and the upcoming 1.9.2 as well, although it's possible to require 'backports/1.8.7' for just the changes of 1.8.7, or even just the backports you need, e.g. require 'backports/1.8.7/integer/times'
I'm running Ruby 1.8.6.
I installed the minitest 1.3.1 gem, which is the new defacto replacement for the Test::Unit framework in Ruby 1.9 The API is supposed to be the same.
I wrote a small test to get things rolling:
require 'rubygems'
gem 'minitest'
require 'minitest/unit'
MiniTest::Unit.autorun
class CategoryMiniTest < MiniTest::Unit::TestCase
def test_twoCategoriesCannotHaveSameName
assert_equals(2,2)
end
end
Which leads to:
>ruby test\unit\category_mini_test.rb
l:/ruby_home/lib/ruby/1.8/pathname.rb:709:in `relative_path_from': different prefix: "l:/" and "L:/Gishu/Ruby/Rails/ShowMeTheMoney" (ArgumentError)
from l:/ruby_home/lib/ruby/gems/1.8/gems/minitest-1.3.1/lib/minitest/unit.rb:17
What gives?
I can't see anything wrong with your code. It looks almost exactly the same as the Ruby 1.8.6 & MiniTest example in my blog post: Test::Unit and MiniTest with different Ruby versions.
So I wonder if it is:
something to do with your environment,
something to do with how you are running the test, or
a bug in MiniTest.
Looking at the error message, I wonder whether the problem is with case-sensitivity - the upper-case and lower-case L drive letters may not match.