Port and IP set up

ruby

#1

use ENV[PORT] as the port and ENV[IP] as the host in your scripts!

I keep receiving this message when trying to have my c9 run
how do i fix this
I chose the custom workspace and it is using the ruby default

how and where do I fix this?


#2

Can you provide more details? Are you trying to run a Rails server?


#3

Ruby(default). This is a message I get when I start it up, if it helps figure out the issue:

Important: use ENV[PORT] as the port and ENV[IP] as the host in your scripts!

/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in require': cannot load such file -- 02_timer (LoadError) from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:inrequire’
from /home/ubuntu/workspace/w1d1/spec/02_timer_spec.rb:10:in `’


#4

It would be better if you could show us how you are trying to start the server.

Nevertheless, try something like this, it should work:

rails s -p $PORT -b $IP

Exactly as it is.


#5

Question for you @BigChief45 Where would you put the $PORT and $IP as you’ve written above in to make it work?

Thanks!


#6

I am not sure if I understand your question. If you run that command in your terminal, it will spawn a Rails server.

The variables above are variables set up in the work space by Cloud9 and they simply store the values that Cloud9 recommends using when running server. In this case $PORT's value is 8080 and $IP's value is 0.0.0.0. Keep in mind that you can still use a few other ports as well.

You can easily confirm these values by using these commands in your terminal:

echo $IP
echo $PORT

#7

Hey thanks for the reply @BigChief45 i’m new to ruby and trying to run some rspec files alongside a some simple function. I’ve been trying to set up rspec locally and / or on c9 for about 10 hours over 2 days… Fml.

When i follow instructions - the terminal returns this info, of which i cannot make heads or tails:
Your code is running at https://rspec1-almondmatter.c9users.io.
Important: use ENV[PORT] as the port and ENV[IP] as the host in your scripts!

/home/ubuntu/workspace/spec/00_hello_spec.rb:7: trailing `_’ in number
bundle exec rspec spec/00_hello_spec.rb
^
/home/ubuntu/workspace/spec/00_hello_spec.rb:7: syntax error, unexpected tIDENTIFIER, expecting end-of-input
bundle exec rspec spec/00_hello_spec.rb
^

if I get into the terminal and type $ bundle exec rspec spec/00_hello_spec.rb
i get the error:
bash: bundle: command not found
but after combing the support page i see plenty of users having the same problem but not a lot of solutions. Any ideas are greatly appreciated. Thanks!


#8

When i follow instructions - the terminal returns this info, of which i cannot make heads or tails:
Your code is running at https://rspec1-almondmatter.c9users.io.
Important: use ENV[PORT] as the port and ENV[IP] as the host in your scripts!

The above message is simply saying that your server is running on the address above. ENV[PORT] and ENV[IP] simply refer to the $PORT and $IP variables mentioned before. You can simply ignore this message, it just serves as a reminder.


Can you share the code? It might be possible that you have errors in your code.

Also you can try simply running rspec <your_file.rb>.


#9

you rock - the language for the coding example to learn from is as follows i’ll put my rspec file after and then my code:

## Run the test

#bundle exec rspec spec/00_hello_spec.rb

## Watch it fail

You should see an error. Don’t get scared! Try to read it and

figure out what the computer wants to tell you. Somewhere on the

first line it should say something like

the hello function

says hello (FAILED - 1)

Failures:

1) the hello function says hello

Failure/Error: hello.should == “Hello!”

NameError:

undefined local variable or method `hello’ for #RSpec::Core::ExampleGroup::Nested_1:0x000001009b8808

# ./hello/hello_spec.rb:5:in `block (2 levels) in <top (required)>’


the RSPEC

require “00_hello”

describe “the hello function” do
it “says hello” do
expect(hello).to eq(“Hello!”)
end
end

describe “the greet function” do
it “says hello to someone” do
expect(greet(“Alice”)).to eq(“Hello, Alice!”)
end

it “says hello to someone else” do
expect(greet(“Bob”)).to eq(“Hello, Bob!”)
end
end

my code from 00_hello.rb

def hello()
puts (“Hello!”)
end

hello()


#10

when i run the function with good code it prints
Your code is running at https://rspec1-almondmatter.c9users.io.
Important: use ENV[PORT] as the port and ENV[IP] as the host in your scripts!

Hello!

Process exited with code: 0

without any code in the 00_hello.rb file. the spec file outputs:

/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in require': cannot load such file -- 00_hello (LoadError) from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:inrequire’
from /home/ubuntu/workspace/spec/00_hello_spec.rb:103:in `’

while it should say something like

the hello function

says hello (FAILED - 1)

Failures:

1) the hello function says hello

Failure/Error: hello.should == “Hello!”

NameError:

undefined local variable or method `hello’ for #RSpec::Core::ExampleGroup::Nested_1:0x000001009b8808

# ./hello/hello_spec.rb:5:in `block (2 levels) in <top (required)>’

does this seem right to you? Am I just wasting your time? Sorry if that is the case… :confused:


#11

When posting code in the forum, it’s better if you make use of some Markdown to style it for better reading. See this guide (Syntax Highlighting part).


So, this is your 00_hello.rb file:

def hello()
  puts ("Hello!")
end

hello()

And this is your Rspec test file 00_hello_spec.rb:

require "00_hello"

describe "the hello function" do
  it "says hello" do
    expect(hello).to eq("Hello!")
  end
end

describe "the greet function" do
  it "says hello to someone" do
    expect(greet("Alice")).to eq("Hello, Alice!")
  end

  it "says hello to someone else" do
    expect(greet("Bob")).to eq("Hello, Bob!")
  end
end

Some things I see that might be wrong in your Rspec code:

  1. Your require clause might not be correctly pointing to 00_hello.rb in the file system. Make sure you are using the correct path.

If everything else in your Rspec is fine. Then you should try executing it with:

rspec spec/00_hello_spec.rb

You should see one passed green spec and two red failing specs.


#12

thanks so much for the guidance @BigChief45 I imagine that everything in the path is correct because it’s copied directly from a well used / patrolled git. So you’re execute suggestion would be:

rspec spec/00_hello_spec.rb
require "00_hello"

describe "the hello function" do
  it "says hello" do
    expect(hello).to eq("Hello!")
  end
end

describe "the greet function" do
  it "says hello to someone" do
    expect(greet("Alice")).to eq("Hello, Alice!")
  end

  it "says hello to someone else" do
    expect(greet("Bob")).to eq("Hello, Bob!")
  end
end

and not bundle exec rspec spec/00_hello_spec.rb ?
i’m so new to rspec i spent the last 40 min looking up executing it with rspec spec/00_hello_spec.rb when i do that I get the error:

/home/ubuntu/workspace/spec/00_hello_spec.rb:102: trailing `_’ in number
rspec spec/00_hello_spec.rb
^
/home/ubuntu/workspace/spec/00_hello_spec.rb:102: syntax error, unexpected tIDENTIFIER, expecting end-of-input
rspec spec/00_hello_spec.rb

without rspec spec/00_hello_spec.rb i get:

/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in require': cannot load such file -- 00_hello (LoadError) from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:inrequire’
from /home/ubuntu/workspace/spec/00_hello_spec.rb:103:in `’


#13

To clarify, rspec spec/00_hello_spec.rb should be run in your terminal, and should not be included in your Ruby code.

Let’s say that you 00_hello.rb file is in your workspace home directory. And your spec file is inside a directory called spec. Then the spec file can require the 1st file like this:

require "./00_hello"

describe "the hello function" do
  it "says hello" do
    expect(hello).to eq("Hello!")
  end
end

describe "the greet function" do
  it "says hello to someone" do
    expect(greet("Alice")).to eq("Hello, Alice!")
  end

  it "says hello to someone else" do
    expect(greet("Bob")).to eq("Hello, Bob!")
  end
end

Read this very good StackOverflow answer on using require in Ruby.

Then I run the spec file like this:

rspec spec/00_hello_spec.rb

I get the following output:

FFF

Failures:

  1) the hello function says hello
     Failure/Error: expect(hello).to eq("Hello!")

       expected: "Hello!"
            got: nil

       (compared using ==)
     # ./spec/hello_spec.rb:5:in `block (2 levels) in <top (required)>'

  2) the greet function says hello to someone
     Failure/Error: expect(greet("Alice")).to eq("Hello, Alice!")

     NoMethodError:
       undefined method `greet' for #<RSpec::ExampleGroups::TheGreetFunction:0x000000025e7818>
     # ./spec/hello_spec.rb:11:in `block (2 levels) in <top (required)>'

  3) the greet function says hello to someone else
     Failure/Error: expect(greet("Bob")).to eq("Hello, Bob!")

     NoMethodError:
       undefined method `greet' for #<RSpec::ExampleGroups::TheGreetFunction:0x000000025e3178>
     # ./spec/hello_spec.rb:15:in `block (2 levels) in <top (required)>'

Finished in 0.01861 seconds (files took 0.13858 seconds to load)
3 examples, 3 failures

Failed examples:

rspec ./spec/hello_spec.rb:4 # the hello function says hello
rspec ./spec/hello_spec.rb:10 # the greet function says hello to someone
rspec ./spec/hello_spec.rb:14 # the greet function says hello to someone else

In theory, your first test should pass. The reason it is not passing is because you are using puts in your hello function:

def hello
  puts "Hello!"
end

puts does not return a value, it simply prints a string. To make the test pass, make the function return the string:

def hello
  return "Hello!"
end

In Ruby, the return statement can and is usually omitted. The following is equivalent:

def hello
  "Hello!"
end

Run the spec again and you now have your first test passing:

.FF

Failures:

  1) the greet function says hello to someone
     Failure/Error: expect(greet("Alice")).to eq("Hello, Alice!")

     NoMethodError:
       undefined method `greet' for #<RSpec::ExampleGroups::TheGreetFunction:0x00000002016768>
     # ./spec/hello_spec.rb:11:in `block (2 levels) in <top (required)>'

  2) the greet function says hello to someone else
     Failure/Error: expect(greet("Bob")).to eq("Hello, Bob!")

     NoMethodError:
       undefined method `greet' for #<RSpec::ExampleGroups::TheGreetFunction:0x00000002013d88>
     # ./spec/hello_spec.rb:15:in `block (2 levels) in <top (required)>'

Finished in 0.00237 seconds (files took 0.16015 seconds to load)
3 examples, 2 failures

Failed examples:

rspec ./spec/hello_spec.rb:10 # the greet function says hello to someone
rspec ./spec/hello_spec.rb:14 # the greet function says hello to someone else

Some Ruby Tips

  1. In Ruby we don’t usually add parenthesis to functions when defining them unless they receive parameters. Same thing goes when calling the functions.