String usage

M

Maung Aung

Hi all,
I have a question about string usage. I assign @test_string class
variable which is assigned the name of a text file.
@test_string = 'file.txt'
When I was trying to use it as a parameter in a statement
info=File.open(@test_string) {|f| f.read.split},
it said "TypeError: can't convert nil into String". I check the type of
@test_string and it said String object. What am I missing?

Then I try to use to_s method, and it said "Errno::ENOENT: No such file
or directory - "

Could someone explain what is going here? Pls kind on me as I am new to
Ruby.

Thanks,
Maung
 
M

Maung Aung

never mind, I figured that out because I was using it as class variable
and I didn't instantiate the object.
Thanks.
Maung
 
S

sohksyber

never mind, I figured that out because I was using it as class variable
and I didn't instantiate the object.
Thanks.
Maung

5. Write a ruby program that accepts integer X from the keyboard and
use it to create an inverted triangle with X levels

Example:

Input: 5 Output: *****
****
***
**
*

7. Horizontal Histogram:

Write a program that accepts a set of digits (0 to 9) as input and
prints a horizontal histogram representing the occurrences of each
digit.

Example:

Input: Enter a Number : 12 Output: 0
Enter 12 digits: 1 **
1,7,2,9,6,7,1,3,7,5,7,9 2 *
3 *
4
5 *
6 *
7 ****
8
9 **

7. Round off:

As a new ruby programmer write a program to accept numbers from the
keyboard round off to the nearest integer number.

Example:
Input: Enter a number 123.32 Output: 123
Input: Enter a number 324.7 Output: 325

8. Write a Ruby program that has the following menu options:
1. Area of a Triangle

2. Area of a Rectangle

3. Area of a circle

4. Exit

The user selects the number corresponding to an option. Upon selecting
a menu option, the program takes the appropriate input values from the
user and computes the result. The program is terminated if menu option
4 is selected.

Useful formulas:
Area of a Triangle = 1/2*base height
Area of a Rectangle = Length Width
Area of a Circle = r2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,311
Members
47,981
Latest member
satome

Latest Threads

Top