sprintf broken in RubyMotion - ruby

Ruby noob, just got and installed RubyMotion, created my first program and I'm having trouble getting the Time (sample) app to work.
Whenever I try to format a string using either sprint or the other form (sorry don't know the name) I just get my format string back.
i.e. (from the console while my app is running in Simulator)
Build ./build/iPhoneSimulator-5.1-Development
Simulate ./build/iPhoneSimulator-5.1-Development/Timer.app
(main)>> #time = 0.1
=> 0.0999999940395355
(main)>> string = sprintf("%.1f", #time)
=> "%.1f"
(main)>> string
=> "%.1f"
(main)>> "%.1f" % #time
=> "%.1f"
(main)>>
The same result in the actual app in the Simulator.
I do have the default ruby installed on my Mac but if I try running a test ruby file (print "%05d" % 123) I get expected results.
/usr/bin/ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin11.0]
/Library/RubyMotion/bin/ruby -v
MacRuby 0.12 (ruby 1.9.2) [universal-darwin11.0, i386]
Running on Lion 10.7.3, any advice or ideas appreciated.
Thanks.

As of 5/9/12, this has been fixed. Make sure you're on RubyMotion 1.3 (run sudo motion update) and string formatting will work again.
Update 5/8/12: Confirmed bug, should be fixed this evening or tomorrow.
Per my comments above, there is definitely a bug with this. An ugly hack to get this working could be something like this (since %d still works fine for sprintf):
#time = 0.1
sprintf("%d", (#time * 10).round).insert(-2, '.')

Related

Encoding issues with Ruby-2.4.1

I am having different output of following line with different version of ruby:
puts "#{"%c"%[254]}"
Output from ruby-2.4.1 : þ
Output from ruby-1.8.7 : �
It looks like issue with encoding, as there is no encoding in Ruby-1.8.7
Can someone please help me to get same result as Ruby-1.8.7 in Ruby-2.4.1 also?
This result is desirable because Ruby-1.8.7 gives me "254" after decoding while Ruby-2.4.1 gives me following error
`%': invalid value for Integer(): "þ" (ArgumentError)
I don't know much about Ruby 1.8.7, as I started Ruby with version 2.3, but this looks promising: https://gist.github.com/afair/2911107
But as everyone else is saying it would be best to upgrade your Ruby version if at all possible. Older versions of Ruby had limited Unicode support.

Ruby PDFLib on OSX: LoadError in require

I just downloaded and installed the Ruby for OSX version of PDFLib (from pdflib.com).
I am using the following setup:
ruby 1.9.3p429 (2013-05-15 revision 40747) [x86_64-darwin12.4.0]
OSX Yosemite 10.10.4
PDFLib 9.0.5
The require 'PDFLib' statement in my Ruby file produces the following error message:
/Users/[...]/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in 'require': dlsym(0x7ff6e3ef4b90, Init_PDFLib): symbol not found - [...]/PDFLib.bundle (LoadError)
So it seems that the Ruby interpreter cannot find the Init_PDFLib in the library. But from the output of nm I gather that this symbol seems to be present:
$ nm -g ./PDFlib.bundle | grep -i init
0000000000001200 T _Init_PDFlib
[...]
Has anyone any idea what goes wrong? Thanks in advance for your answers.
Found it!
The PDFlib file is called PDFlib.bundle (small l), and my code did a require 'PDFLib' (capitalized L).
It turns out that the require does load the bundle file regardless of the wrong capitalization, but then searches for the Init_PDFLib symbol, which is not present.
So changing the require statement to require 'PDFlib' (small l) worked.

Dino Ruby Gem - program stops responding on board att

I installed ruby 1.9.3 and the dino gem on windows, bootstrapped it (like the tutorial said, I resetted the arduino when uploading the generated and sketch and got a 'avrdude done. Thank you.' at the end) but got a problem when executing the blink code
# dino_sample.rb
require 'dino'
board = Dino::Board.new(Dino::TxRx.new)
led = Dino::Components::Led.new(pin: 13, board: board)
[:on, :off].cycle do |switch|
led.send(switch)
sleep 0.5
end
but more specifically this line
board = Dino::Board.new(Dino::TxRx.new)
the program stops responding and I can't even quit it with ^C
since it's my first attempt to use dino I have no idea of what went wrong, but I would guess the bootstrap was not done correctly

msfcli RBreadline and Readline problems

Hello my friends i have a problem with Metasploit.
OS: Fedora 20
Metasploit: Cloned from git.
The problem is:
when i run
msfconsole
I have this error:
"/usr/local/share/gems/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:240: stack level too deep (SystemStackError)"
but when i run:
msfconsole -L
Everithing goes fine.
So the issue is that i have to run
msfcli auxyliary/scanner/smb/smb_enunshares E
and i get the same error when i run "msfconsole"
I believe that the problem is for somes GEMS.
EDIT:
I was able to solve the problem, but this isn't the best way.
I had to modify the code.
This is what i do:
1) edit the file: /metasploit-framework/lib/msf/ui/console/driver.rb
2) go to line: 59
3) replace:
rl = false
rl_err = nil
begin
if(opts['RealReadline'])
require 'readline'
rl = true
end
rescue ::LoadError
rl_err = $!
end
# Default to the RbReadline wrapper
require 'readline_compatible' if(not rl)
for:
rl = true
rl_err = nil
require 'readline'
and then everithing should work. What i did is to force the "-L" option in the code.
if anyone have another solution, please post it!
:-)
The problem is an incompatibility with Metasploit's bundled rbreadline and ruby 2.x; use 1.9.3 for now or start msfconsole with -L.

Error I have gotten a few times and I don't know how to fix it

I keep seeing undefined (?...) sequence: something. For this particular one I had..
/(?<!\d)[0-3]?\d(?!\d)/
but this has happened a few times to me and I'm not sure how to fix it.
These always work in rubular, but then i get that error when I run it?
Help please!
its working here :
rituraj#rituraj:~$ irb
2.1.1 :001 > s = "somestring 23 and 34 and 233"
=> "somestring 23 and 34 and 233"
2.1.1 :002 > s.scan(/(?<!\d)[0-3]?\d(?!\d)/)
=> ["23", "34"]
check your ruby version:
Ruby's regex engine doesn't support lookbehind which is less than 1.9.
You'd need to switch to 1.9+.
optional usage : you can use oniguruma

Resources