Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I use ruby infrequently - usually it adds up to writing a script once in two months or more. I do most of my programming with C++, which is very different from ruby.
with such wide gaps between my brushes with ruby I keep forgetting basic aspects of the language (like parsing a text file and other simple stuff).
I would like to do a daily drill of the basic stuff and I was wondering if there is some site I can subscribe to and will send me the Ruby question of the day or something similar.
anyone knows of such a site / Internet service?
It's not daily, but you might be interested in Ruby Quiz.
You might also subscribe to ruby-talk and look over the posts there each day.
Check out Jim Weirich's ruby koans. It's a set of ruby scripts organized by topic that guides you through the different parts of the language by unit testing your knowledge.
def method_with_block
result = yield
result
end
def test_methods_can_take_blocks
yielded_result = method_with_block { 1 + 2 }
assert_equal __, yielded_result
end
The game is to go through these and fill in the __ blanks. Running rake will check your answers.
What about a general problem site like Project Euler( http://projecteuler.net/ ) or the ACM programming competition problem sets ( http://www.inf.bme.hu/contest/tasks/ ) and just restrict yourself to using ruby?
A more arduous (but awesome) task is the google code jam. Just assign yourself one of the problems, and set a time per week to put an hour into it. Quit while you are still working. That way, you hunger for more, and think about it in the interim time.
http://code.google.com/codejam
TL;DR: Find a large task, that is interesting. Work on it in bite-size pieces, leaving yourself hungry for more.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
This is more of a vague/general question, so for that I do apologize in advance. I'm building a simple Rails app with Twilio integration that will allow users to send messages such as "how are you" and receive a quasi-intelligent response back from the app such as, "Good, how about yourself, John?".
I have everything wired up and it works but I was wondering if anyone could point me in the right direction of writing an algorithm in Ruby that would make this "bot" smarter. Right now I'm using a pretty straight forward if/elsif/else chain to parse the payload and deliver the proper response but this does not seem maintainable once I get past 5-10 keywords.
Would I be better off with a case statement (at least for readability) or is there a better OOP design pattern that would help me match my keywords and deliver a certain response?
It depends. If you want to write a real chat bot, prepare for 5+ years of reading papers on neural networks. Might as well just give up now :)
However, if you reduce your requirements (make the bot recognize only a few selected keywords, with predefined response(s) for each), then a simple dictionary approach could suffice.
You're right, storing the dictionary in code is not scalable. It's better to store the knowledge in a data file (YAML, JSON or whatever you prefer) or a database. Then your code will load the file and will be able to look up responses by keywords.
Something like this:
def reply(input)
# you load this from a storage, so that when you add new keywords,
# your code doesn't have to be touched.
knowledge = [
{ keyword: 'how are you', response: 'Good, how about yourself, %{name}?' },
{ keyword: 'bye', response: 'Ciao!' },
]
response = knowledge.detect do |pair|
input.downcase.include?(pair[:keyword].downcase)
end
response && response[:response]
end
reply('How are you doing, machine?') # => "Good, how about yourself, %{name}?"
reply('gotta go, bye') # => "Ciao!"
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
So, I am getting into Ruby. I'm learning it everyday. And just like Spanish. I am getting able to read it, but not write it.
I am doing "Ruby the Hard Way" and I understand WHY things work, but the more I move through it the more I realize that I could not mimic the code if someone just came up to me and said "I need you to do so and so in Ruby."
I know that it is going to depend largely on what the task is, is how my code will be set up. But are there any tips and/or tricks for Ruby. (i.e. "Always write your variables first or strings before arrays" or something like that.
I'm unclear on when to write which lines of code or WHY something has to be below another block of code. I am aware of the nebulous nature of this question, but I'm looking for a bit of more broad rules for Ruby.
I think the WHY in your question has to do with learning the concepts, but not really applying them in a way that's meaningful to you.
I'd recommend finding a task you can accomplish with Ruby. Eg: write to a file, get content out of a webpage, get the date/time in a specific country, whatever. Start small, but challenge yourself. Look at StackOverflow ruby tag, or Github to see what other people are doing with Ruby.
That will get you thinking about the problem you're solving, and not the code alone; which is what programming is all about.
Then come back here and ask about the specifics you're struggling with.
Small disclaimer: yes, all of the above is my opinion; and as others have commented, this question is too broad.
The question is really too broad. I'll suppose that you know of another language already. If you are looking for code convention, look at the Ruby Style Guide. Taking courses at Code School could get you an idea of the good practice (and make you learn useful frameworks like Rails. Reading "The Ruby Way" can give you a feeling of the "pulse" of the language. Building the blog application used by many Rails book could also help.
Finally, checkout sample Rails or Ruby projects from GitHub.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
What do you think fellow programmers about using short functions vs using inline code?
Example with function:
//Check if all keys from $keys exist in $array
function functionName(array $array, array $keys) {
return array_diff($keys, array_keys($array));
}
functionName($mas,$keys);
vs. using just the code:
array_diff($keys, array_keys($mas));
I think that in your example, it's superfluous. There's no need to create an extra function call and add bytes to the filesize without good reason.
Also, the inline array_diff($keys, array_keys($mas)); is a lot easier to debug for fellow programmers, than looking through your code to find out exactly what functionName() does and where it is located.
It depends on what functionName actually is.
If you're using customerDetailsAreValid throughout your code and you suddenly have to add validation of $array['email'], you're going to be grateful for the separation of intent and implementation.
If on the other hand you're wrapping array_diff in the function diffArray there isn't much point.
I think clarity is a prime concern when writing logic you hope will be around for any amount of time.
In general, I abhor inline functions. I think they are lazy, promote spaghetti code, and in general exude a complete lack of concern for style/readability/clarity on the part of the developer.
Filesize - I find this argument very arbitrary. The js files are transmitted once and then cahced. In many cases, you find descriptive names, etc, (hopefully comments) that all add to file size. If size is very important , use a file minimizer that makes a file as tiny as possible.
Looking for a function? How about trying to figure out exactly what is going on in a voluminous docReady. CTL-F usually invokes a find facility.
I will grant that there can be simple cases where an inline function detracts little from the readability of the code. However, the inline approach will never be MORE CLEAR than the alternate separation of reference and implementation.
my two cents
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How does Google or amazon implement the auto-suggestion at their search box. I am looking for the algorithm with technology stack.
PS: I have searched over the net and found this and this and many many more. But I am more interested in not what they do but how they do it. NoSQL database to store the phases? or is it sorted or hashed according to keyword's? So to rephrase the question: Given the list of different searches ignoring personalization, geographic-location etc, How do they store, manage and suggest it so well.
This comes under the domain of stastical language processing problems. Take a look at spelling suggestion article by Norvig. Auto - completion will use a similar mechanism.
The idea is that from past searches, you know the probability of phrases (or better called bigram, trigram, ngram). For each such phrase, the auto complete selects the one having max value of
P(phrase|word_typed) = P(word_typed|phrase) P(phrase) / P(word_typed)
P(phrase|word_typed) = Probability that phrase is right phrase if word typed
so far is word_typed
Norvig's article is a very accessible and great explanation of this concept.
Google takes your input and gives TOP4 RESULTS ACCORDING TO THE RANK IDs [if results are less it returns parameters as empty strings] given to different keywords which differ dynamically by the hit and miss count.
Then, they make a search query and returns 4 fields with url, title, and 2 more fields in Json, omnibox then populates the data using prepopulate functions in Chrome trunk.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I'd like to write a code beautifier and i thought of using Ruby to do it. Could someone show me a place to get started? I've seen a lot of code beautifiers online but I've never come across any tutorials on how to write one. Is this a very challenging task for someone who's never undertaken any projects such as writing a compiler, parser, etc. before?
(Is there another langauge which would be more well suited for this kind of task, excluding C/C++?)
Python has an interesting feature - it exposes its own parser to scripts. There are examples that use the AST - abstract syntax tree - and do the pretty printing.
I'm not aware that Ruby exposes its own parser to its scripts in such a way, but there are parsers for Ruby written in Ruby here.
Well... I think the initial steps are what you'd do for any project.
Write a list of requirements.
Describe a user interface to your program, that you like and won't prevent you meeting those requirements.
Now you can write down more of a "code" design, and pick the language that would be easiest for you to meet that design.
Here's some requirements off the top of my head:
Supports code beautifying of these languages: Ruby, Python, Perl
Output code behaves identically to input
Output has consistent use of tabs/spaces
Output has consistent function naming convention
Output has consistent variable naming convention
Output has matching braces and indentation
Make as many as you want, it's your program. ;p I was kidding about the Perl, but I think every language you support is going to add a more work.