I've been following the exercises on RubyMonk and at least 2 exercises so far use a question mark. This symbol is not at the end of empty? or include? etc. It's not even being used in a regex. When I can't figure something out, I get frustrated. I've given up on a few of these exercises and had to hit the option "See the solution". It was even more frustrating when the solution uses syntax not introduced in the lesson or even hinted at on the page asking you to solving a problem. RubyMonk is not the only site that has this problem. Had to vent...sorry.
Some Ruby beginners but most intermediate and all advanced will know that what I speak of above is the ternary or conditional operator. I have been googling Ruby question mark and adding words like symbol and comparison. Right now, I'm working on the problem named Number Shuffle. I had to click on the "See the solution" option which is the second time I'm seeing a question mark. Here's the specific line...
no_of_combinations = number.to_s.size == 3 ? 6 : 24
Searching for this kept sending me to pages that discussed array/string elements ending with a question mark or pages discussing regular expressions. I finally found a page that talks about [value] ? [value] : [value] I guess I wasn't using the right search terms or something.
I found the explanation on this page...
http://ruby.about.com/od/beginningruby/a/The-Ternary-Or-Conditional-Operator.htm
Here is the text that set me straight and answered my question...
The general format of this expression is as follows: conditional ? true : false. If the conditional expression is true, then the operator will evaluate as the true expression, otherwise it will evaluate as the false expression.
Now that I found out that this is a ternary operator, I can google and find all kinds of explanations on it. It would be nice if online tutorials explained or gave references to all syntax used.
Saturday, December 27, 2014
Wednesday, December 17, 2014
Learning Regex con't (xxxxxxx-xxxx where x's are digits)
This one (regex) didn't take me as long to figure out given all that I read about regular expressions.
As part of my homework for Learning Ruby the Hard Way, I'm still working on my editor that takes 8 pieces of information and inserts it all into a tab delimited format .txt file. The second column needs to be in the format of xxxxxxx-xxxx where the x's are digits and the hyphen needs to be included. This is an account id in the system I'm working with where I'm going to eventually create a script to combine rows from files a 3rd party agency is giving to us.
Back to the editor, I created a regex to only accept an account id that has 7 digits, a hyphen followed by 4 more digits. This one was easy compared to the online tutorials I was reading over. Just inserting the regex here so I'll have it for future reference.
/^\d{7}\-\d{4}$/
Maybe by the time I'm finished with the 2 projects I want to complete, I'll have a cook book of regex's.
As part of my homework for Learning Ruby the Hard Way, I'm still working on my editor that takes 8 pieces of information and inserts it all into a tab delimited format .txt file. The second column needs to be in the format of xxxxxxx-xxxx where the x's are digits and the hyphen needs to be included. This is an account id in the system I'm working with where I'm going to eventually create a script to combine rows from files a 3rd party agency is giving to us.
Back to the editor, I created a regex to only accept an account id that has 7 digits, a hyphen followed by 4 more digits. This one was easy compared to the online tutorials I was reading over. Just inserting the regex here so I'll have it for future reference.
/^\d{7}\-\d{4}$/
Maybe by the time I'm finished with the 2 projects I want to complete, I'll have a cook book of regex's.
Tuesday, December 16, 2014
Learning Regex (file name with .txt extension)
I've gone through 6-7 web pages and 2 tutorials. I did an initial search for a regex for a file name. I didn't search but a minute or two then decided I need to learn a little more about regular expressions than just how to come up with one to recognize a file name.
After reading through a lot of documentation, I believe I came up with what I needed but first a little background.
I'm on lesson 36 of Learn Ruby the Hard Way. I'm not making a game like the home suggests to. Instead, I'm making a script that will ask for eight pieces of information and it will create a tab delimited file with each piece of information. I'm giving the user the option to name the file. The requirements will be that it must only contain letters (upper/lower case), digits, a hyphen and an underscore character and the file extension must end with a ".txt" (without the double quotes). If the user types in anything else, it won't be accepted.
So I'm still at the beginning of creating this script and had to stop for a few days to learn how to put together regular expressions. This is what I have so far for an acceptable file name.
I chose this type of project over creating a game because I need to ultimately create a script that parses a tab delimited file and combines rows/records based on values in 2 columns. That will be my next project.
Here's the regex that I came up with to accept a file name with the above mentioned requirements.
/^[^\W+]\w+\-?(.txt$)/
I'm always open for suggestions on simplifying my code. I'm in no way an expert but the above expression works for what I need it for. I'll be studying regular expressions in more depth as I continue on with my projects.
After reading through a lot of documentation, I believe I came up with what I needed but first a little background.
I'm on lesson 36 of Learn Ruby the Hard Way. I'm not making a game like the home suggests to. Instead, I'm making a script that will ask for eight pieces of information and it will create a tab delimited file with each piece of information. I'm giving the user the option to name the file. The requirements will be that it must only contain letters (upper/lower case), digits, a hyphen and an underscore character and the file extension must end with a ".txt" (without the double quotes). If the user types in anything else, it won't be accepted.
So I'm still at the beginning of creating this script and had to stop for a few days to learn how to put together regular expressions. This is what I have so far for an acceptable file name.
I chose this type of project over creating a game because I need to ultimately create a script that parses a tab delimited file and combines rows/records based on values in 2 columns. That will be my next project.
Here's the regex that I came up with to accept a file name with the above mentioned requirements.
/^[^\W+]\w+\-?(.txt$)/
I'm always open for suggestions on simplifying my code. I'm in no way an expert but the above expression works for what I need it for. I'll be studying regular expressions in more depth as I continue on with my projects.
Sunday, November 30, 2014
User input stored as a string ($stdin.gets.chomp)
Following along with LRtHW on exercise 31 and I was wondering why the author was testing the user input as a string.
There was an exercise that covered gets.chomp but I never thought about it returning a string. Then I found this documentation...
I was assuming that it would take the user input as is but the above link states...
- chomp is a string method (String#chomp).
- gets returns (gives you back) a string.
puts "You enter a dark room with two doors. Do you go through door #1 or door #2?"
print "> "
door = $stdin.gets.chomp
if door == "1"
puts "There's a giant bear here eating cheese cake. What do you do?"
end
...to...
puts "You enter a dark room with two doors. Do you go through door #1 or door #2?"
print "> "
door = $stdin.gets.chomp.to_i
if door == 1
puts "There's a giant bear here eating cheese cake. What do you do?"
end
..it worked as expected.
Ruby naming
Following along with the online LRTHW exercises and lesson 25. I copied the code as it was stated from the web page. Well, I typed it in manually rather than doing a copy and paste. Just to take a shortcut to type it in as fast as possible, I didn't capitalize the module name and I got the following error...
2.1.3 :001 > require "./ex25.rb"
SyntaxError: /home/.../ruby/ex25.rb:1: class/module name must be CONSTANT
from /home/.../.rvm/rubies/ruby-2.1.3/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/.../.rvm/rubies/ruby-2.1.3/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from (irb):1
from /home/.../.rvm/rubies/ruby-2.1.3/bin/irb:11:in `<main>'
After going back and capitalizing the module name, function names and references to the functions, I was able to get it run without error.
2.1.3 :001 > require "./ex25.rb"
=> true
Found official documentation that discusses naming. The exercise didn't cover the topic of naming...
http://www.ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html
http://www.ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html
Friday, November 28, 2014
Escape sequence r
While trying out the escape sequence 'r', I again didn't get the results I expected.
I thought that there would be a blank line after the word return when keying this in...
2.1.3 :001 > puts "return\r"
return
=> nil
After finding out about \b, I decided to put a space after \r and the double quotation mark and this was what I got...
2.1.3 :002 > puts "return\r "
eturn
=> nil
This confused me even more until a few minutes later when I remembered that a carriage return was not what I thought it was. I remembered reading some documentation some time ago that \r returned the "focus", if you will, to the beginning of the line and that the line feed was a separate escape sequence altogether. That wasn't the terminology I read but that's how I remembered it.
2.1.3 :003 > puts "return\f"
return
=> nil
This is what I was expecting at first when using \r. I do know that the \f switch/option just moves the "focus" down a line in the same spot...kind of like using a typewriter and moving the page up one line. The type writer is going to type right where it's at unless it's moved to the beginning of that line. Necessary to know if using Ruby to look through strings or text files etc.
Starting at the beginning of the next line (just like you would with a typewriter), I'd need to use \r\f options like so...
2.1.3 :004 > puts "return\r\f"
return
=> nil
I thought that there would be a blank line after the word return when keying this in...
2.1.3 :001 > puts "return\r"
return
=> nil
After finding out about \b, I decided to put a space after \r and the double quotation mark and this was what I got...
2.1.3 :002 > puts "return\r "
eturn
=> nil
This confused me even more until a few minutes later when I remembered that a carriage return was not what I thought it was. I remembered reading some documentation some time ago that \r returned the "focus", if you will, to the beginning of the line and that the line feed was a separate escape sequence altogether. That wasn't the terminology I read but that's how I remembered it.
2.1.3 :003 > puts "return\f"
return
=> nil
This is what I was expecting at first when using \r. I do know that the \f switch/option just moves the "focus" down a line in the same spot...kind of like using a typewriter and moving the page up one line. The type writer is going to type right where it's at unless it's moved to the beginning of that line. Necessary to know if using Ruby to look through strings or text files etc.
Starting at the beginning of the next line (just like you would with a typewriter), I'd need to use \r\f options like so...
2.1.3 :004 > puts "return\r\f"
return
=> nil
Escape sequence b
I'm currently reading the website (online e-book) Learn Ruby the Hard Way and I'm on exercise 10.
It covers using the escape sequence with the various options.
I was stuck for a moment on why it wasn't working like I thought it should.
2.1.3 :001 > puts "backspace\b"
backspace
=> nil
I felt the entry should have removed the 'e' but the 'e' still showed up. On another attempt, I got the result I was looking for.
2.1.3 :002 > puts "backspace\b "
backspac
=> nil
Putting a space between the letter 'b' and the double quote worked. I have no technical explanation for this. My guess is that the backslash b is not given any space to "do it's thing". I've googled but couldn't find anything on this topic. Any comments welcome. If I do find the explanation, I'll post what I find.
It covers using the escape sequence with the various options.
I was stuck for a moment on why it wasn't working like I thought it should.
2.1.3 :001 > puts "backspace\b"
backspace
=> nil
I felt the entry should have removed the 'e' but the 'e' still showed up. On another attempt, I got the result I was looking for.
2.1.3 :002 > puts "backspace\b "
backspac
=> nil
Putting a space between the letter 'b' and the double quote worked. I have no technical explanation for this. My guess is that the backslash b is not given any space to "do it's thing". I've googled but couldn't find anything on this topic. Any comments welcome. If I do find the explanation, I'll post what I find.
Subscribe to:
Comments (Atom)