Code Answers

Ruby – “no implicit conversion from nil to integer”

I'm doing my first program, a simple to-do list. I want it to let me type a number, and delete the corresponding item from the list. Every time though, I get "no implicit conversion from nil to integer". I can't seem to work it out. Any ideas? $list = Array.new def mainmethod puts "Enter new item, or type 'view' to view the list, 'delete' to...

Ruby turn values inside a hash into local variables

Say I have this hash: entry = {"director"=>"Chris Nolan", "prducer"=>"Sum Duk", "writer"=>"Saad Bakk"} I want to extract each key into its own local variable with the associated value: director = "Chris Nolan" producer = "Sum Duk" ... By using a loop and not: director = entry["director"] Since there are a lot of values and I don't want to ...

Ruby – no implicit conversion of Array into String

I am getting an error when executing my test. Failure/Error: expect(industry_sic_code).to include page.sic_code TypeError: no implicit conversion of Array into String # ./spec/os/bal/company/company_filter_clean_harbors_industries_stub.rb:62:in `block (2 levels) in ' The Method: def sic_code subtables = @b.table(:class => 'industry-c...

Ruby syntax error, unexpected $end, expecting keyword_end

The following code is showing me the error as follows: "ruby -KU -- 'C:\Users\Ishmael\My Documents\Aptana Studio 3 Workspace\Simple\FirstFile' C:/Users/Ishmael/My Documents/Aptana Studio 3 Workspace/Simple/FirstFile:55: syntax error, unexpected $end, expecting keyword_end" I searched for the extra end(s) and the trailing '.'s but in vain and I...

Ruby – Use RbConfig instead of obsolete and deprecated Config

When I run my Rails app in WEBrick on Ubuntu, after upgrading to ruby-1.9.3-p327, I receive the following error: [rake –tasks] /home/dsilver/.rvm/gems/ruby-1.9.3-p327/gems/em-dir-watcher-0.9.4/lib/em-dir-watcher.rb:7: Use RbConfig instead of obsolete and deprecated Config. Any idea what's going on? I've seen some posts connecting this to Ima...

Ruby: nil can’t be coerced into Fixnum (TypeError)

The error is on line 12 and I'm not sure why I can't add the numbers. Any help is much appreciated. Instructions: Write a method that takes an array of numbers. If a pair of numbers in the array sums to zero, return the positions of those two numbers. If no pair of numbers sums to zero, return nil. def two_sum(nums) idx1 = 0 idx2 = 1 whil...

Ruby: Multiply all elements of an array

Let's say I have an array A = [1, 2, 3, 4, 5] how can I multiply all elements with ruby and get the result? 1*2*3*4*5 = 120 and what if there is an element 0 ? How can I ignore this element? Solution to answer: This is the textbook case for inject (also called reduce) [1, 2, 3, 4, 5].inject(:*) As suggested below, to avoid a zero, [1, 2...

Ruby Koans: Why convert list of symbols to strings

I'm referring to this test in about_symbols.rb in Ruby Koans https://github.com/edgecase/ruby_koans/blob/master/src/about_symbols.rb#L26 def test_method_names_become_symbols symbols_as_strings = Symbol.all_symbols.map { |x| x.to_s } assert_equal true, symbols_as_strings.include?("test_method_names_become_symbols") end # THINK ABOUT IT: ...

Ruby-on-rails – Routing Error uninitialized constant

I am trying to learn RoR. MY controller is class SectionController < ApplicationController def new if request.post? u=SectionMst.new( :section_name => params[:section_name]) u.save redirect_to("/section") else render end end def index @sections = SectionMst.all end def destroy u=SectionM...

Ruby each_line reads line break too

I'm trying to read data from a text file and join it with a post string. When there's only one line in the file, it works fine. But with 2 lines, my request is failed. Is each_line reading the line break? How can I correct it? File.open('sfzh.txt','r'){|f| f.each_line{|row| send(row) } I did bypass this issue with split and extra delimiter...

Ruby – Undefined method `+’ for nil:NilClass (NoMethodError)

I'm writing a simple class that is initialized with a variable called "cash" which is an integer. Below is the code. When I run this, I get the NoMethodError. I know I can easily fix this by referencing the local class variable with @cash, but a book I read on OOP recommend to almost never use the @, and instead set the attr and use simply 'cas...

Ruby – RVM – Warning! PATH is not properly set up

I am having trouble with my RVM – I keep getting error messages telling me that PATH is not correctly set up. First off, any of of this sounds like it it's coming from a complete noob, that's because it is. There have been some similar threads that I've been looking at, but nothing has helped me fix the issue. Any help would be greatly apprecia...

Ruby – warning: string literal in condition

Using the first bit of code below I receive two warning messages: warning: string literal in condition x2 if input == "N" || "n" #do this else input == "L" || "l" #do this as opposed to using this which results in no warnings if input == "N" || input == "n" #do this else input == "L" || input == "l" #do this I'm wondering why the firs...

Ruby – Open file, find and replace multiple lines

I'm new to ruby and was hoping someone could help me figure out how to open a file, then using gsub to find and replace by multiple conditions. Here's what I got so far but this doesn't seem to work as the 2nd replace var overrides the first: text = File.read(filepath) replace = text.gsub(/aaa/, "Replaced aaa with 111") replace = text.gsub(/bbb...

Ruby inject with index and brackets

I try to clean my Code. The first Version uses each_with_index. In the second version I tried to compact the code with the Enumerable.inject_with_index-construct, that I found here. It works now, but seems to me as obscure as the first code. Add even worse I don't understand the brackets around element,index in .. .inject(groups) do |group_cont...

Ruby syntax error: unexpected $end, expecting keyword_end

I am supposed to: Capitalize the first letter of string. Capitalize every word except articles (the, a, an), conjunctions (and), and prepositions (in). Capitalize i (as in "I am male."). Specify the first word of a string (I actually have no idea what this means. I'm trying to run the spec file to test other functions). Here's my code: class...

Ruby $stdin.gets without showing chars on screen

I want to ask users to type in a password, but I don't want the chars to appear on screen as they type. How do I do this in Ruby? Solution to answer: You can use the STDIN.noecho method from the IO/console module: require 'io/console' pw = STDIN.noecho(&:gets).chomp ...

Ruby: Write to stdin and read from stdout

I am writing a ruby program that is supposed to execute another program, pass values to it via stdin, read the response from its stdout, and then print the response. This is what I have so far. #!/usr/bin/env ruby require 'open3' stdin, stdout, stderr = Open3.popen3('./MyProgram') stdin.puts "hello world!" output = stdout.read errors = std...