ruby

Convert a string of 0-F into a byte array in Ruby

I am attempting to decrypt a number encrypted by another program that uses the BouncyCastle library for Java. In Java, the sets the key like this: key = Hex.decode("5F3B603AFCE22359"); I am trying to figure out how to represent that same step in Ruby. ...

Qt4 Designer and Ruby, what am I doing wrong?

So I've been trying to make ruby read my Qt4 design and it's not working, at all. I've created a design in Qt4 Designer and saved it as a .ui file. Then I used rbuic4 to convert it to a design_ui.rb and Qt4. My current ruby main file looks like this: require 'gui/design_ui.rb' require 'Qt4' class AppMain < Ui_MainWindow def initialize...

Simple Rdoc Question about documentation modifiers.

I have this method in a class I am using def binary_search(a,x) # ... end and I want in the documentation for the parameters to appear as def binary_search(array, key) and not binary_search(a,x). I have tried to use the documentation modifier # :binary_search: array, key with no success. I know this a little thing, but if somebody k...

how do i remove leading whitespace chars from ruby heredoc

Hi All, I'm having a problem with a Ruby heredoc i'm trying to make. It's returning the leading whitespace from each line even though i'm including the - operator, which is supposed to suppress all leading whitespace characters. my method looks like this: def distinct_count <<-EOF \tSELECT \t CAST('#{name}' AS V...

twitter retweets api

I'm using twitter gem as a library to work with twitter api. I've noticed a weird issue with getting retweets for the tweet. Whenever I make a call I get only retweets from my mutual connections. So, if someone retweets my post I'll never be able to get that retweet using proper api call. However, search function might be a solution for...

How can Nokogiri extract the Charset encoding of a scraped HTML document?

Found a snippet that works for HTML Simple Dom Parser. $el=$html->find('meta[http-equiv=Content-Type]',0); $fullvalue = $el->content; preg_match('/charset=(.+)/', $fullvalue, $matches); echo $matches[1]; Can somebody help me to convert this so that this suits for Ruby and Nokogiri? ...

deleting blank lines after loop

ok, I have the following, very simple code: f = "String1\n\n\nString2\n\n\n" f.each_line do |t| t.delete! "\n" puts t.inspect end It removes the \n, but leaves the spaces "String1" nil nil "String2" nil nil I want it to look like this: "String1" "String2" Thanks in advance for help. ...

rspec error 'report_activate_error': Could not find RubyGem rspec-core (>=0) (Gem:LoadError)

Hey all, I have a seemingly simple error but I can't seem to figure out what to do to resolve it. I have tried uninstalling and reinstalling but that hasn't helped me out. I am new to the terminal and to ruby/ruby on rails, so forgive my potentially stupid question. Does anyone know how to resolve this issue? This is the exact error me...

How do you access sessions and cookies from within a Rails gem or lib?

Im trying to create what will eventually be a rails gem (right now I have the files within the /lib directory) and I need to be able to read/write to and from cookies and sessions. My methods will be called from within either the controller or views, so access to the cookies and sessions SHOULD be possible from within my gem but I cant ...

Eliminating nil lines

I have the following code: f = File.open("/log/mytext.txt") f.each_line do |i| i = i.delete("\n") puts i.inspect The delete gets rid of the \n, but the result looks like this: "#<MatchData \"line1\">" "" "" "" "#<MatchData \"line2\">" Want it to return : line1 line2 Been fighting this problem all day. Thanks for your help ...

Split string suppressing all null fields

I want to split a string suppressing all null fields Command: ",1,2,,3,4,,".split(',') Result: ["", "1", "2", "", "3", "4", ""] Expected: ["1", "2", "3", "4"] How to do this? Edit Ok. Just to sum up all that good questions posted. What I wanted is that split method (or other method) didn't generate empty strings. Looks lik...

How do I remove unwanted from an array?

Ok so I have an array that looks like this. ["Enter Sandman", "One", "Nothing Else Matters", "Master of Puppets", "The Unforgiven", "The Day That Never Comes", "For Whom the Bell Tolls", "Fade to Black", "Sad But True", "Wherever I May Roam", "Turn the Page", "I Disappear", "Fuel", "Cyanide", "Seek & Destroy", "Whiskey In the Jar", "All...

Finding unique hash value within array of hashes (Ruby)

a[0] = {:id => '1234', :value => '37'} a[1] = {:id => '4321', :value => '50'} a[2] = {:id => '1122', :value => '50'} From here I want to be able to check to see if a hash exists with :id => '4321' without having to loop through the array manually. Is there anything where I can do something like this: a.exists?(:id => '4321') ? I've t...

How to use multiline strings in Cucumber's tabular data sets?

Hi, I am building a cucumber feature that looks like this: Feature: Parsing of license files As a developer I want to know which requirements, prohibitions and permissions a license gives me So I can know my rights and duties. Scenario: Analyzing a known license Given I run local executable "licc" with arguments "gpl...

Creating Objects at Run-time in Ruby

PHP <?php $dynamicProperties = array("name" => "bob", "phone" => "555-1212"); $myObject = new stdClass(); foreach($dynamicProperties as $key => $value) { $myObject->$key = $value; } echo $myObject->name . "<br />" . $myObject->phone; ?> How do I do this in ruby? ...

How do I get a Ruby CGI program that requires a gem to run properly?

Hello! I have configured my Apache installation to run Ruby CGI scripts. I am now trying to run a simple Ruby CGI script that requires a gem. When I run this script from the command line, it outputs correctly. But when I call it as an Apache CGI script, it generates an Apache Internal Server Error. The script looks like this: #!/Ruby/...

Ruby's "foo = true if !defined? foo" won't work as expected

When it is in fact not defined, it gets the value nil just because it was "touched": $ irb ruby-1.9.2-p0 > foo = true if !defined? foo => nil ruby-1.9.2-p0 > foo => nil ruby-1.9.2-p0 > if !defined? bar ruby-1.9.2-p0 ?> bar = true ruby-1.9.2-p0 ?> end => true ruby-1.9.2-p0 > bar => true so the if ... end works as expected, ...

Ruby's "foo = true if !defined? foo || foo.nil?" won't work

I thought in the following, foo should be true $ irb ruby-1.9.2-p0 > foo = true if !defined? foo || foo.nil? => nil ruby-1.9.2-p0 > foo => nil because foo was at first not defined, but the foo = true part make it temporarily has a nil value, so the !defined didn't catch it, but the foo.nil? should catch it, and make it true... b...

How do you convert an ISO 8601 date to a UNIX timestamp in Ruby?

I've been trying Time.parse() and Date.parse(), but can't see to figure it out. I need to convert a date in the form "2007-12-31 23:59:59" to a UNIX timestamp. Something like PHP's strtotime() function would be perfect. ...

How to write unit tests for STI associations Ruby on Rails

What steps should you use when in a need to write unit tests for STI associations. I am completely confused. Please provide some suggestions or links to some tutorials. Thanks in advance ...